import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.data.DataBean;

import java.util.List;

public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.MyViewHolder>  {
    private Context mContext;
    private final List<DataBean> dataList;
    private final LayoutInflater mLayoutInflater;

    public ChannelAdapter(Context mContext, List<DataBean> dataList) {
        this.mContext = mContext;
        this.dataList = dataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=mLayoutInflater.inflate(R.layout.channel_item,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);


        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        DataBean data =dataList.get(position);
        holder.mText.setText("1111");
        holder.mImageView.setImageResource(R.drawable.f);
        /**
         * 设置点击事件
         * */
        holder.rlContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext,"你点击了"+position,Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }



    public class MyViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rlContainer;
        TextView mText;
        ImageView mImageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            this.mText=itemView.findViewById(R.id.home_channel_item_text);
            this.mImageView=itemView.findViewById(R.id.home_channel_item_img);
            this.rlContainer=itemView.findViewById(R.id.rl_item_container);

        }
    }
}

最后修改于 2021-12-25 23:11:34
上一篇