标签:ack new obj def etl 点击事件 image pre case
需求场景:RecyclerView的Item需要点击,或者Item中的某个控件需要点击,或者两者同时需要点击处理。
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;
}
}
}
}
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内的控件点击处理
标签:ack new obj def etl 点击事件 image pre case
原文地址:https://www.cnblogs.com/wzqnxd/p/9177077.html