Android SDK 23中的非滚动RecyclerView滚动问题
我正在Android SDK 23中的非滚动RecyclerView滚动问题,android,android-recyclerview,android-6.0-marshmallow,Android,Android Recyclerview,Android 6.0 Marshmallow,我正在滚动视图中实现一个回收视图。为了在整个页面上只有一个滚动行为,我实现了一个NonScrollRecyclerView版本。实施情况如下: public class NonScrollRecyclerView extends RecyclerView { public NonScrollRecyclerView(Context context) { super(context); } public NonScrollRecyclerView(Context context,
滚动视图
中实现一个回收视图
。为了在整个页面上只有一个滚动行为,我实现了一个NonScrollRecyclerView
版本。实施情况如下:
public class NonScrollRecyclerView extends RecyclerView {
public NonScrollRecyclerView(Context context) { super(context); }
public NonScrollRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
if(ev.getAction() == MotionEvent.ACTION_MOVE)
return true;
return super.dispatchTouchEvent(ev);
}
}
一旦我将构建和目标设置更新到SDK 23,我就无法滚动包含非CrollRecyclerView
的页面。具体的问题是,页面滚动正常,直到我到达回收器视图部分,一旦我滚动到此视图上,我就无法再向上或向下滚动
我在SDK 22及以下版本中没有遇到此问题
我的xml如下所示:
public class NonScrollRecyclerView extends RecyclerView {
public NonScrollRecyclerView(Context context) { super(context); }
public NonScrollRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
if(ev.getAction() == MotionEvent.ACTION_MOVE)
return true;
return super.dispatchTouchEvent(ev);
}
}
XML@layout/rv
包含回收器视图
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_gray">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_gray"
android:orientation="vertical">
<include
layout="@layout/row_mall_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv_mall_header"
android:layout_marginTop="8dp"/>
<include
layout="@layout/row_mall_shops"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv_mall_shops"
android:layout_marginTop="8dp"/>
<include
layout="@layout/row_mall_coupons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv_mall_coupons"
android:layout_marginTop="8dp"/>
<include
layout="@layout/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv_mall_feeds"
android:layout_marginTop="8dp"/>
</LinearLayout>
</ScrollView>
XML-@layout/rv
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_gray"
android:id="@+id/ll_mall_feeds">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:paddingTop="6dp"
android:paddingBottom="6dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_feedcount"
android:textColor="@color/semi_theme_blue"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="12dp"
android:layout_centerVertical="true" />
</RelativeLayout>
<com.project.ui.NonScrollRecyclerView
android:id="@+id/nrv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:listSelector="@android:color/transparent" />
</LinearLayout>
不建议在ScrollView中使用RecyclerView和ListView,因为在呈现ScrollView时不会计算元素的高度。这意味着,当显示ScrollView时,以及稍后当RecyclerView收到有关数据更改的通知时(例如,当您初始化适配器时),您的适配器可能不会被填充,因此无法重新计算元素高度 这真是一件让人头疼的事,因为您必须尝试计算元素的高度,而且它永远都不准确,因此在ScrollView中显示ListView或RecyclerView时会出现差异。有关如何计算图元高度的一些示例可以进行检查和修改 我的建议是将RecyclerView转换为线性布局,并以编程方式添加元素,以便模拟ListView或RecyclerView行为:
LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.files);
layout.removeAllViews();
for (int i = 0; i < fileAdapter.getCount(); i++) {
final View item = fileAdapter.getView(i, null, null);
item.setClickable(true);
item.setId(i);
item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fileContentRowPosition = v.getId();
# Your click action here
}
});
layout.addView(item);
}
LinearLayout布局=(LinearLayout)rootView.findviewbyd(R.id.files);
layout.removeallview();
对于(int i=0;i
这里是包含文件定义的XML:
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<LinearLayout
android:id="@+id/files"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
...
可以检查整个java代码和整个布局
另一方面,如果您仍然想继续您的实现,并且关于您的问题,您可以查看本文
非常感谢,对于整个页面中的一次滚动,您可以使用NestedScrollView而不是ScrollView,并设置recyclerView.setNestedScrollingEnabled(false) 你为什么不直接使用LinearLayout而不是RecyclerView呢?你基本上是把一个RealReVIEW变成一个线性布局,但是有更多的开销。同意上面的评论,只需去掉DealReVIEW,并且,如果你考虑到这一点,你就不会再问XML布局问题了,你可以根据你的需求创建YouTube自己的线性布局管理器……检查这个链接。