gpt4 book ai didi

android - NestedScrollView 和 Horizo​​ntal RecyclerView 平滑滚动

转载 作者:IT老高 更新时间:2023-10-28 23:11:06 26 4
gpt4 key购买 nike

我有一个垂直的nestedscrollview,其中包含一堆带有水平布局管理器设置的recyclerview。这个想法与新的 google play 商店的外观非常相似。我能够使其正常工作,但它一点也不流畅。以下是问题:

1) 即使我点击它,水平 recyclerview 项目大多数时候都无法拦截触摸事件。 ScrollView 似乎优先于大多数 Action 。我很难捕获水平运动。这个 UX 令人沮丧,因为我需要尝试几次才能正常工作。如果你查看 play store,它能够很好地拦截触摸事件,而且效果很好。我注意到在 Play Store 中,他们设置的方式是在一个垂直回收 View 中包含许多水平回收 View 。没有 ScrollView 。

2) 水平recyclerviews的高度必须手动设置,并且没有简单的方法来计算子元素的高度。

这是我正在使用的布局:

<android.support.v4.widget.NestedScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:background="@color/dark_bgd"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:id="@+id/main_content_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
tools:visibility="gone"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
android:id="@+id/starring_list"
android:paddingLeft="@dimen/spacing_major"
android:paddingRight="@dimen/spacing_major"
android:layout_width="match_parent"
android:layout_height="180dp" />

这种 UI 模式非常基本,很可能在许多不同的应用中使用。我读过很多 SO,其中 ppl 说将列表放在列表中是一个坏主意,但它是一种非常常见且现代的 UI 模式,到处都在使用。想想 netflix 之类的界面,里面有一系列水平滚动列表一个垂直列表。难道没有一种顺利的方法可以做到这一点吗?

来自商店的示例图片:

Google Play Store

最佳答案

因此,平滑滚动问题现已修复。这是由设计支持库(当前为 23.1.1)中的 NestedScrollView 中的错误引起的。

您可以在此处阅读有关该问题和简单修复的信息: https://code.google.com/p/android/issues/detail?id=194398

简而言之,在您执行 throw 之后,nestedscrollview 没有在滚动组件上注册一个完整的,因此它需要一个额外的 'ACTION_DOWN' 事件来释放父 nestedscrollview 拦截(吃掉)后续事件。所以发生的情况是,如果您尝试滚动您的子列表(或 viewpager),在一次 throw 后,第一次触摸会释放父 NSV 绑定(bind),随后的触摸将起作用。这让用户体验非常糟糕。

本质上需要在NSV的ACTION_DOWN事件上添加这一行:

computeScroll();

这是我正在使用的:

public class MyNestedScrollView extends NestedScrollView {
private int slop;
private float mInitialMotionX;
private float mInitialMotionY;

public MyNestedScrollView(Context context) {
super(context);
init(context);
}

private void init(Context context) {
ViewConfiguration config = ViewConfiguration.get(context);
slop = config.getScaledEdgeSlop();
}

public MyNestedScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

public MyNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}


private float xDistance, yDistance, lastX, lastY;

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final float x = ev.getX();
final float y = ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
lastX = ev.getX();
lastY = ev.getY();

// This is very important line that fixes
computeScroll();


break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - lastX);
yDistance += Math.abs(curY - lastY);
lastX = curX;
lastY = curY;

if (xDistance > yDistance) {
return false;
}
}


return super.onInterceptTouchEvent(ev);
}

}

使用该类代替 xml 文件中的nestedscrollview,子列表应正确拦截和处理触摸事件。

唷,实际上有很多这样的错误让我想完全放弃设计支持库,并在它更成熟时重新访问它。

关于android - NestedScrollView 和 Horizo​​ntal RecyclerView 平滑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258496/

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