嵌套水平 RecyclerView
Nested Horizontal RecyclerView
我想创建一个嵌套的 RecyclerView,因为 Parent RV 是水平布局管理器,有 > 3 个项目,child 也是 horzinotl,但我无法滚动 child它仅表示 parent 手势。
Parent布局管理器
LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
listItem.setLayoutManager(layoutManager);
listItem.setAdapter(adapter);
Child布局管理器
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
list.setHasFixedSize(true);
list.setLayoutManager(layoutManager);
list.setItemAnimator(new DefaultItemAnimator());
list.setAdapter(adapter);
XML - Parent
<androidx.cardview.widget.CardView
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
</androidx.cardview.widget.CardView>
XML - Parent 项
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/roundColor_svprogresshuddefault"
android:elevation="160dp"
android:padding="@dimen/home_cell_margin">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_item"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_gravity="center"
android:nestedScrollingEnabled="false" />
</androidx.constraintlayout.widget.ConstraintLayout>
XML - Child
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listHolder">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent" ></ImageView>
</LinearLayout>
结果示例
我通过创建一个简单的自定义 RecyclerView 和下面的最终代码修复了它
public class DJHRecyclerView extends RecyclerView {
public DJHRecyclerView(Context context) {
super(context);
}
public DJHRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DJHRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
getParent().requestDisallowInterceptTouchEvent(true);
return super.onInterceptTouchEvent(event);
}
}
我想创建一个嵌套的 RecyclerView,因为 Parent RV 是水平布局管理器,有 > 3 个项目,child 也是 horzinotl,但我无法滚动 child它仅表示 parent 手势。
Parent布局管理器
LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
listItem.setLayoutManager(layoutManager);
listItem.setAdapter(adapter);
Child布局管理器
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
list.setHasFixedSize(true);
list.setLayoutManager(layoutManager);
list.setItemAnimator(new DefaultItemAnimator());
list.setAdapter(adapter);
XML - Parent
<androidx.cardview.widget.CardView
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
</androidx.cardview.widget.CardView>
XML - Parent 项
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/roundColor_svprogresshuddefault"
android:elevation="160dp"
android:padding="@dimen/home_cell_margin">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_item"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_gravity="center"
android:nestedScrollingEnabled="false" />
</androidx.constraintlayout.widget.ConstraintLayout>
XML - Child
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listHolder">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent" ></ImageView>
</LinearLayout>
结果示例
我通过创建一个简单的自定义 RecyclerView 和下面的最终代码修复了它
public class DJHRecyclerView extends RecyclerView {
public DJHRecyclerView(Context context) {
super(context);
}
public DJHRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DJHRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
getParent().requestDisallowInterceptTouchEvent(true);
return super.onInterceptTouchEvent(event);
}
}