Android 在回收器列表视图中设置所选项目的背景色
我想更改“回收者”列表视图中某个视图的背景颜色,并在同一列表视图中单击另一个项目时恢复为默认颜色 我的回收器视图模板如下Android 在回收器列表视图中设置所选项目的背景色,android,android-layout,android-recyclerview,Android,Android Layout,Android Recyclerview,我想更改“回收者”列表视图中某个视图的背景颜色,并在同一列表视图中单击另一个项目时恢复为默认颜色 我的回收器视图模板如下 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="w
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/list_view_click_background"
xmlns:custom="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:padding="10dp">
<com.kahoindia.dev.customclasses.KaHOTextView
android:id="@+id/txtHomework"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
style="@style/kaho_content_label_textview_style"
custom:CustomTextViewFont="@string/kaho_segoeui_regular_font"
android:layout_weight="1.5"
/>
<com.kahoindia.dev.customclasses.KaHOTextView
android:id="@+id/txtDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="10dp"
custom:CustomTextViewFont="@string/kaho_segoeui_regular_font"
style="@style/kaho_content_description_textview_style"
android:layout_weight="0.5"/>
</LinearLayout>
</LinearLayout>
和KahoiRecycleServiceItemClickListener接口
public interface KaHOIRecyclerViewItemClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
但这突出了多重立场。请帮助我尝试使用
视图
而不是mRecyclerView.getChildAt(i)
否其不起作用您是否尝试过为recycler视图项目添加单击侦听器(当您将项目绑定到recycler视图时)?是的,我尝试过。。同样不起作用的请发布RecyclerTouchListener的代码,以及KahoiRecyclerviceItemClickListener类,所以我可以帮忙。
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private KaHOIRecyclerViewItemClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final KaHOIRecyclerViewItemClickListener 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.getChildLayoutPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildLayoutPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
public interface KaHOIRecyclerViewItemClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}