我正在努力实现在Google Play上看到的功能,其中CardView有一个可以点击的选项菜单,您可以点击卡片查看卡上的更多细节。
我在我的RecyclerView上实现了一个OnItemTouch监听器,它可以正常工作并在触摸时响应,带我去另一个活动。但是,当我现在尝试将一个单击侦听器添加到我在RecyclerView项目(即CardView)中添加的选项图标时,将调用OnItemTouch侦听器。有人可以帮忙吗?
活动
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(getApplicationContext(),
TransactionDetailActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("transaction_key", mTransactionList.get(position));
intent.putExtras(bundle);
startActivity(intent);
}
})
); mRecyclerView.setAdapter(mAdapter);
单击“侦听器”
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildPosition(childView));
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
答案 0 :(得分:1)
如果您不想处理CardView孩子的点击,OnItemTouchListener就是这样。
但是如果你想处理cardview子项的cliks,你可能必须将OnClickListener(在Adapter类' DataObjectHolder或onBindViewHolder方法中)附加到CardView或其直接子项(可以是任何布局例如.LineLayout ,RelativeLayout等)来处理卡片点击。如果这样做,CardView中任何视图的OnClickListener都可以工作。
以下是一个例子:
<强>适配器强>
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.DataObjectHolder>{
Context context;
ArrayList<String> list;
static String TAG = "ApprovalsRV Adapter";
DeleteListener deleteListener;
public RecyclerViewAdapter(Context context, ArrayList<String> list)
{
this.context = context;
this.list = list;
deleteListener = (DeleteListener) context;
}
public static class DataObjectHolder extends RecyclerView.ViewHolder
{
TextView tName;
RelativeLayout rlName;
CardView cvName;
public DataObjectHolder(View itemView, final Context context)
{
super(itemView);
tName = (TextView) itemView.findViewById(R.id.tName);
rlName = (RelativeLayout) itemView.findViewById(R.id.rlName);
cvName = (CardView) itemView.findViewById(R.id.cvName);
cvName.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
//Handle the card click here
deleteListener.deleteName(position); // if handling from activity (explained below)
}
});
tName.setOnClickListener(new View.OnClickListener()
{ //You can have similar listeners for subsequent children
@Override
public void onClick(View view) {
//Handle the textview click here
}
});
}
}
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.card_view_layout, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view, context);
return dataObjectHolder;
}
@Override
public void onBindViewHolder(DataObjectHolder holder, int position)
{
holder.tName.setText(list.get(position));
}
@Override
public int getItemCount() {
return list.size();
}
}
如果您想从活动中处理卡片点击,则需要在此处使用界面。
<强> DeleteListener.java 强>
public interface DeleteListener {
public void deleteName(int position);
}
现在在您的活动中使用它:
<强> MyActivity.java 强>
public class MyActivity extends AppCompatActivity implements DeleteListener
{
@Override
public void deleteName(int position) {
//Your job
}
}
这将清除你的怀疑。
答案 1 :(得分:0)
不使用OnItemTouchListener来处理RecyclerView项目点击事件,而是使用OnClickListener作为您在ViewHolder中获得的itemView。
public class MyViewHolder extends RecyclerView.ViewHolder implements OnClickListener{
public MyViewHolder(View itemView){
super(itemView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v){
// handle click event
}
}
因此,当按下选项图标时,将不会调用整个itemView ClickListener。