设置RecyclerView的item的点击事件

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Shirly_YY/article/details/53642179

一、写一个监听事件接口:MyItemClickListener

public interface MyItemClickListener {
    void onItemClick(View view, int position);
}
二、在RecyclerView的适配器RecyclerViewAdapter中,写一个监听方法setOnItemClickListener。适配器代码如下:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    Activity activity;
    List<ClassPageDataJson> classPageData = new ArrayList<>();
    MyItemClickListener mItemClickListener;

    public RecyclerViewAdapter(Activity activity, List<ClassPageDataJson> classPageData) {
        this.activity = activity;
        this.classPageData = classPageData;
    }

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

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(activity).inflate(R.layout.grid_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view, mItemClickListener);

        viewHolder.mImg = (ImageView) view.findViewById(R.id.img);
        viewHolder.mTxt = (TextView) view.findViewById(R.id.title);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        WindowManager wm = activity.getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        ClassPageDataJson classPageDataJson = classPageData.get(i);
        String imgUrl = classPageDataJson.getClassImg();
        String typeName = classPageDataJson.getClassname();
        ImageLoader.getInstance().displayImage(imgUrl+ ConstantLabel.imageviewsize210, viewHolder.mImg, LoadingManager.getHomepager());
        viewHolder.mTxt.setText(typeName);
        viewHolder.mTxt.setTextSize(14);
    }

    public void setOnItemClickListener(MyItemClickListener listener){
        this.mItemClickListener = listener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private MyItemClickListener mListener;

        public ViewHolder(View rootView, MyItemClickListener listener) {
            super(rootView);
            this.mListener = listener;
            rootView.setOnClickListener(this);
        }

        ImageView mImg;
        TextView mTxt;

        @Override
        public void onClick(View v) {
            if(mListener != null){
                mListener.onItemClick(v,getPosition());
            }
        }
    }
}
三、在给RecyclerView设置适配器的地方,给适配器设置监听事件:

GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
//LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayout.HORIZONTAL, false);
mRecyclerView.setLayoutManager(gridLayoutManager);
adapter = new RecyclerViewAdapter(this, classPageData);
mRecyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(this);
四、在重写方法OnItemClick中写点击RecyclerView的某个item后要实现的效果,例:

@Override
public void onItemClick(View view, int position) {
    Toast.makeText(TypePageActivity.this, "你点击了RecyclerView的第" + position + "个item", Toast.LENGTH_SHORT).show();
}

展开阅读全文

没有更多推荐了,返回首页