gpt4 book ai didi

android - Android SDK 23 中的 Non Scroll RecyclerView 滚动问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:53:19 24 4
gpt4 key购买 nike

我正在 ScrollView 中实现 RecyclerView。为了在整个页面上只有一个滚动行为,我实现了一个 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 后,我无法滚动包含 NonScrollRecyclerView 的页面。具体问题是页面滚动正常,直到我到达回收器 View 部分,一旦我滚动到这个 View ,我就无法再滚动,无论是向上还是向下。

我不会在 SDK 22 及以下版本中遇到这个问题

我的xml如下:

XML @layout/rv 包含回收器 View

<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 时会出现差异。可以检查如何计算元素高度的一些示例 herehere .

我的建议是将您的 RecyclerView 变成 LinearLayout 并以编程方式添加元素,这样您就可以模拟 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);

}

这是带有文件定义的 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代码here和整个布局 here .

另一方面,如果您仍想继续实现,关于您的问题,您可以查看这篇关于 Handling Scrollable Controls in Scrollview 的文章

最好的问候,

关于android - Android SDK 23 中的 Non Scroll RecyclerView 滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33889743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com