Android RecyclerView點選事件實現的兩種方式
阿新 • • 發佈:2018-11-21
因為經常會用到RecyclerView,今天在這裡總結一下實現RecyclerView點選事件的實現方法
一、通過介面回撥的方式實現
1. 首先定義一個點選的介面(Recyclerview自身不帶有點選事件的回撥)
public interface OnRecyItemClickListener{
void onClick(View view, int position);
}
2. 在Adapter中實現點選的方法
public class ListRecycleViewAdapter extends RecyclerView.Adapter<ListRecycleViewAdapter.ListViewHolder>{ private Context mContext; private List<String> mList; public ListRecycleViewAdapter(Context context, List<String> list) { mContext = context; mList = list; } @Override public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.item_list_recycle_view, parent, false); ListViewHolder holder = new ListViewHolder(view); return holder; } @Override public void onBindViewHolder(final ListViewHolder holder, int position) { holder.tv.setText(mList.get(position)); View itemView = ((RelativeLayout) holder.itemView).getChildAt(0); if (mOnItemClickListener != null) { itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getLayoutPosition(); mOnItemClickListener.onItemClick(holder.itemView, position); } }); } } @Override public int getItemCount() { return mList.size(); } private OnRecyItemClickListener mOnItemClickListener;//宣告介面 public void setOnItemClickListener(OnItemClickListener onItemClickListener) { mOnItemClickListener = onItemClickListener; } class ListViewHolder extends RecyclerView.ViewHolder { TextView tv; public ListViewHolder(View itemView) { super(itemView); tv = (TextView) itemView.findViewById(R.id.tv_list_item); } } }
3. 在Activity中呼叫Adapter中實現的點選方法
ListRecycleViewAdapter listRecycleViewAdapter = new ListRecycleViewAdapter(SettingActivity.this, mList); listRecycleViewAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(View view, int position) { Toast.makeText(this, "這是RecyclerView的點選事件", Toast.LENGTH_SHORT).show(); } }
二、在Adapter中直接實現點選事件
public class ListRecycleViewAdapter extends RecyclerView.Adapter<ListRecycleViewAdapter.ListViewHolder>{ private Context mContext; private List<String> mList; public ListRecycleViewAdapter(Context context, List<String> list) { mContext = context; mList = list; } @Override public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.item_list_recycle_view, parent, false); ListViewHolder holder = new ListViewHolder(view); //直接在這裡實現點選事件 holder.tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("onCreateViewHolder", "點選了"); } }); return holder; } @Override public void onBindViewHolder(final ListViewHolder holder, int position) { holder.tv.setText(mList.get(position)); View itemView = ((RelativeLayout) holder.itemView).getChildAt(0); if (mOnItemClickListener != null) { itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getLayoutPosition(); mOnItemClickListener.onItemClick(holder.itemView, position); } }); } } @Override public int getItemCount() { return mList.size(); } class ListViewHolder extends RecyclerView.ViewHolder { TextView tv; public ListViewHolder(View itemView) { super(itemView); tv = (TextView) itemView.findViewById(R.id.tv_list_item); } } }
三、通過RecyclerView的addOnItemTouchListener實現點選事件
1. 首先定義類RecyclerItemClickListener實現 RecyclerView.OnItemTouchListener介面,並在內部定義一個點選監聽的介面
public static class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
GestureDetector mGestureDetector;
private View childView;
private RecyclerView touchView;
public RecyclerItemClickListener(Context context, final HomePageFragment.RecyclerItemClickListener.OnItemClickListener mListener) {
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent ev) {
if (childView != null && mListener != null) {
mListener.onItemClick(childView, touchView.getChildPosition(childView));
}
return true;
}
@Override
public void onLongPress(MotionEvent ev) {
if (childView != null && mListener != null) {
mListener.onLongClick(childView, touchView.getChildPosition(childView));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
mGestureDetector.onTouchEvent(e);
childView = rv.findChildViewUnder(e.getX(), e.getY());
touchView = rv;
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onLongClick(View view, int posotion);
}
}
2. 定義recyclerview新增方法 addOnItemTouchListener()
rc_view.addOnItemTouchListener(new RecyclerItemClickListener(this,new RecyclerItemClickListener.OnItemClickListener() {
public void onItemClick(View view, int position) {
//在此處做點選之後的邏輯處理
}
@Override
public void onLongClick(View view, int position) {
//在此處做長按之後的邏輯處理
Toast.makeText(getContext(), "長按", Toast.LENGTH_SHORT).show();
}
}