1. 程式人生 > >RecyclerView的Item和Item內的控件點擊處理

RecyclerView的Item和Item內的控件點擊處理

man itemclick util AR from osi 處理 monit wid

需求場景:RecyclerView的Item需要點擊,或者Item中的某個控件需要點擊,或者兩者同時需要點擊處理。

一、adapter代碼如下:

package com.ldw.adapter;  
  
import android.content.Context;  
import android.support.v7.widget.RecyclerView;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.GridView;  
import android.widget.ImageView;  
import android.widget.LinearLayout;  
import android.widget.TextView;  
  
import com.ldw.test.R;  
  
import java.util.List;  
  
/**  
 *  
 * Created by ldw on 2017/12/1.  
 */  
  
public class ChapterAdapter extends RecyclerView.Adapter implements View.OnClickListener {  
  
    private List<Object> dataList;  
    private Context context;  
  
    public ChapterAdapter(Context context, List<Object> list) {  
        this.context = context;  
        dataList = list;  
    }  
  
    @Override  
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
        View itemView = LayoutInflater.from(parent.getContext())  
                .inflate(R.layout.item_chapter, parent, false);  
        return new ItemHolder(itemView);  
    }  
  
    @Override  
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {  
        ItemHolder itemHolder = (ItemHolder) holder;  
        itemHolder.itemView.setTag(position);  
        itemHolder.tvPractise.setTag(position);  
    }  
  
    @Override  
    public long getItemId(int i) {  
        return i;  
    }  
  
    @Override  
    public int getItemCount() {  
        return dataList.size();  
    }  
  
    public class ItemHolder extends RecyclerView.ViewHolder {  
          
        public TextView tvPractise;  
  
        public ItemHolder(View itemView) {  
            super(itemView);  
            tvPractise = itemView.findViewById(R.id.tv_item_chapter_practise);  
  
            //將創建的View註冊點擊事件  
            itemView.setOnClickListener(ChapterAdapter.this);  
            tvPractise.setOnClickListener(ChapterAdapter.this);  
        }  
    }  
  
  
    ////////////////////////////以下為item點擊處理///////////////////////////////  
  
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;  
  
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {  
        this.mOnItemClickListener = listener;  
    }  
  
    /** item裏面有多個控件可以點擊 */  
    public enum ViewName {  
        ITEM,  
        PRACTISE  
    }  
  
    public interface OnRecyclerViewItemClickListener {  
        void onClick(View view, ViewName viewName, int position);  
    }  
  
    @Override  
    public void onClick(View v) {  
        //註意這裏使用getTag方法獲取數據  
        int position = (int) v.getTag();  
        if (mOnItemClickListener != null) {  
            switch (v.getId()){  
                case R.id.tv_item_chapter_practise:  
                    mOnItemClickListener.onClick(v, ViewName.PRACTISE, position);  
                    break;  
                default:  
                    mOnItemClickListener.onClick(v, ViewName.ITEM, position);  
                    break;  
            }  
        }  
    }  
}  

二、Activity中的使用方式如下:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_course_chapter);  
        recyclerView.setLayoutManager(new LinearLayoutManager(CourseActivity.this));  
        ChapterAdapter chapterAdapter = new ChapterAdapter(CourseActivity.this, dataList);  
        recyclerView.setAdapter(chapterAdapter);  
        chapterAdapter.setOnItemClickListener(new ChapterAdapter.OnRecyclerViewItemClickListener() {  
            @Override  
            public void onClick(View view, ChapterAdapter.ViewName viewName, int position) {  
                //在此處理點擊事件即可,viewName可以區分是item還是內部控件
                        switch (viewName){
                      
                            case ITEM:
                                ToastUtils.showToast(context,"進入詳情");
                      
                                break;
                        }
            }  
        });  

RecyclerView的Item和Item內的控件點擊處理