Android 回收器视图内部的回收器视图不滚动
在另一个回收器视图中有一个回收器视图。两者都需要垂直滚动。外部回收器视图正在正确滚动,但内部回收器视图未正确滚动 代码如下:Android 回收器视图内部的回收器视图不滚动,android,android-recyclerview,vertical-scrolling,nestedrecyclerview,Android,Android Recyclerview,Vertical Scrolling,Nestedrecyclerview,在另一个回收器视图中有一个回收器视图。两者都需要垂直滚动。外部回收器视图正在正确滚动,但内部回收器视图未正确滚动 代码如下: LinearLayoutManager mLayoutManager = new LinearLayoutManager(ViewActivity.this); outerRecyclerView.setLayoutManager(mLayoutManager); ViewAdapter adapter = new ViewAdapter(ViewActivity.thi
LinearLayoutManager mLayoutManager = new LinearLayoutManager(ViewActivity.this);
outerRecyclerView.setLayoutManager(mLayoutManager);
ViewAdapter adapter = new ViewAdapter(ViewActivity.this);
outerRecyclerView.setAdapter(adapter);
ViewAdapter如下所示:
public void onBindViewHolder(ViewAdapter.ViewViewHolder holder, int position)
{
//RECYCLER VIEW
//TODO: Inner Recycler view scroll movement
LinearLayoutManager mLayoutManager = new LinearLayoutManager(context);
holder.protocolRecyclerView.setLayoutManager(mLayoutManager);
ViewProtocolAdapter adapter = new ViewProtocolAdapter(context);
holder.protocolRecyclerView.setAdapter(adapter);
}
我尝试了以下两种观点,但都不能解决问题
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
if(rv.getChildCount() > 0) {
View childView = rv.findChildViewUnder(e.getX(), e.getY());
if(childView ==listView) {
int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
rv.requestDisallowInterceptTouchEvent(true);
}
}
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
我也试过这个:
outerRecyclerView.setNestedScrollingEnabled(true);//Does not make any difference
innerRecyclerView.setNestedScrollingEnabled(true);//Recycler View start scrolling but very slowly and sometimes scrolls the outer one.
不久前,我在实现回收器视图时遇到了同样的问题。两个回收器视图都将开始滚动,对吗。尽管使用嵌套回收器视图是个坏主意,但如果您希望它正确滚动,则必须禁用内部视图上的滚动。我不确定,但我想是这样的。让我知道它是否有效。如果没有,我将挖掘我的代码并尝试找到解决方案
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(parent.getContext()) {
@Override
public boolean canScrollVertically() {
return false;
}
};
您可以做的是,您可以添加一个滚动视图作为视图支架的根布局,在其中封装第二个回收器视图
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/subContainer"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/subList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</ScrollView>
这样,所有垂直滚动事件将由父回收器视图处理 现在,要使其工作,您需要禁用子列表回收器视图的垂直滚动
LinearLayoutManager layoutManager = new LinearLayoutManager(context) {
@Override
public boolean canScrollVertically() {
return false;
}
};
LinearLayoutManager layoutManager = new LinearLayoutManager(context) {
@Override
public boolean canScrollVertically() {
return false;
}
};
使用android.support.v4.widget.NestedScrollView代替ScrollView
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/firstRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/secondRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
对我来说,它工作得非常好
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
/>
</android.support.v4.widget.NestedScrollView>
不要在回收器视图中保留回收器视图。相反,您应该放大内部回收器视图。您可以使用线性布局,并在回收器视图的onBindViewHolder中对该布局进行充气。通过这样做,您将永远不会面临滚动问题 将下面的ontouch侦听器应用于内部回收器,该回收器可能位于父回收器的适配器中
您不需要将recyclerView放入另一个recyclerView中。这是没有道理的。如果要将XML放在一个布局中,只需执行以下步骤:
- 使NestedScrollView成为父视图
- 在嵌套的ScrollView中创建LinerLayout
- 将RecyclerViews放入NestedScrollView中
<android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:id="@+id/firstRecycler" android:layout_width="match_parent" android:layout_height="wrap_content"/> <android.support.v7.widget.RecyclerView android:id="@+id/secondRecycler" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </android.support.v4.widget.NestedScrollView>
- 因为homever可能有用,我在一些帖子中看到了一些东西
首先,我制作了一个自定义LinearLayoutManager阻止垂直滚动
class CustomHorizontalLinearLayout(context: Context) :
LinearLayoutManager(context, RecyclerView.HORIZONTAL, false) {
override fun canScrollVertically(): Boolean {
return false
}
并将其设置为子RecyclerView(在本例中称为recommendationRV):
接下来,我将外部NestedScrollView添加到子RecyclerViewHolder XML中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="220dp"
android:orientation="vertical"
android:background="@color/transparent"
android:paddingVertical="10dp">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recommendationRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
android:clickable="true"
android:focusable="true"
/>
</androidx.core.widget.NestedScrollView>
这似乎奏效了。这可能不是最好的方法,但它对我来说很有吸引力,所以我希望这能帮助别人尝试使用这段代码,它对我很有用
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.core.widget.NestedScrollView>
它们都需要垂直滚动?您希望出现什么样的行为?您可以尝试为外部回收器启用嵌套滚动,并为内部回收器禁用它吗?拥有两个朝同一方向滚动的嵌套视图是一个非常糟糕的主意。为什么你需要这样做?@davidargylethacker一般来说,我在“回收者”视图中有一种展开/折叠的布局。当外部回收器视图的行展开时,它有另一个列表显示属于该特定行的行。这就是为什么,我有两个回收视图在同一个方向滚动。如果你有更好的想法来实现这一点,请让我知道。如果你想要一个可扩展的回收视图,那么你可以尝试:好的。我做这件事的方法是让内部回收视图不可滚动。因此,在滚动时,只有外部一个处于活动状态。这是一段代码RecyclerView.LayoutManager mLayoutManager=newlinearlayoutmanager(parent.getContext()){@Override public boolean canscrollvertical(){return false;}}};setLayoutManager(mLayoutManager);`现在,这段代码位于第一个回收器视图适配器(外部回收器视图)的onCreateViewHolder()中。@AndyRoid我真的不记得我是怎么做到的。您可以尝试进入内部recyclerview的xml布局,将布局高度作为包装内容,并让我知道它是否有效(还可以禁用垂直滚动,就像我在前面的评论中提到的那样)android:layout\u height=“wrap\u content”
我们有没有办法让InnerV处理自己的scrollEvents?虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题会真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请您的答案添加解释,并说明适用的限制和假设。这适用于可在同一方向滚动的RecyclerView中的任何类型的视图使用RecyclerView内部RecyclerView谢谢
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="220dp"
android:orientation="vertical"
android:background="@color/transparent"
android:paddingVertical="10dp">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recommendationRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
android:clickable="true"
android:focusable="true"
/>
</androidx.core.widget.NestedScrollView>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.core.widget.NestedScrollView>