RecyclerView的Item和Item内的控件点击处理
Posted wzqnxd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView的Item和Item内的控件点击处理相关的知识,希望对你有一定的参考价值。
需求场景: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内的控件点击处理的主要内容,如果未能解决你的问题,请参考以下文章
android RecyclerView嵌套 RecyclerView 子item 和 父item点击事件如何处理
遍历recyclerview的item,使用Appium和Robot Framework选择符合条件的item