我正在构建一个简单的 RecyclerView,其中包含自定义布局(文本视图和开关)和自定义适配器。
我的问题是我无法处理开关的点击事件(使其切换状态),我已经为 RecyclerView 构建了一个触摸监听器,并且每次我点击开关时它都会触发。
经过多次尝试,开关触摸监听器也会触发,但如果点击开关,我想禁用 RecyclerView 的触摸事件。
这是我的布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60.0dip"
android:layout_weight="1.0"
android:orientation="horizontal"
android:paddingLeft="16dp">
<TextView
android:id="@+id/category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:text="Category Name"
android:textAppearance="?android:textAppearanceLarge" />
<android.support.v7.widget.SwitchCompat
android:id="@+id/category_switch"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:checked="true"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingRight="16dp" />
</RelativeLayout>
这是我的适配器代码:
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View row = layoutInflater.inflate(R.layout.single_row_category, parent, false);
CategoryViewHolder holder = new CategoryViewHolder(row);
return holder;
}
public void onBindViewHolder(CategoryViewHolder holder, final int position) {
AlarmCategory thisCat = categories.get(position);
holder.catName.setText(thisCat.getCategoryName());
holder.catStatus.setChecked(thisCat.isOn());
holder.catStatus.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.d("fpp", view.getClass().getName() + " is clicked");
return true;
}
});
}
public class CategoryViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
public TextView catName;
public SwitchCompat catStatus;
public CategoryViewHolder(View itemView) {
super(itemView);
itemView.setOnCreateContextMenuListener(this);
catName = (TextView) itemView.findViewById(R.id.category_name);
catStatus = (SwitchCompat) itemView.findViewById(R.id.category_switch);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
menu.add(0, 1001, 0, "Edit");//groupId, itemId, order, title
menu.add(0, 1002, 1, "Delete");
}
}
这是我的RecyclerView触摸处理程序:
class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
Log.d("fpp", child.getClass().getName() + " is clicked");
return false;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
在onCreate
方法中,我通过以下方式调用它:
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
Intent i = new Intent(CategoriesList.this, AlarmList.class);
i.putExtra("catID", categories.get(position).getCategoryID());
startActivity(i);
}
@Override
public void onLongClick(View view, int position) {
catPos = position;
}
}));
任何帮助都行,我已经尝试了很多解决方案,但都没有用,感到很抱歉。提前致谢。
Log.d("fopp", view.getClass().getName() + " is clicked from inner");
但是很抱歉什么也没有发生。 - FareedrecyclerView.addOnItemTouchListener()
部分和 2)手势检测器。 - Dimitar GenovonClick(View view)
是用于单击布局的方法,即itemView
。itemView
是您填充的布局视图呈现。只需将活动导航代码移至该方法中即可。 - Dimitar Genov