gpt4 book ai didi

android - 在水平 ScrollView 内创建水平 ScrollView

转载 作者:行者123 更新时间:2023-11-30 04:53:57 25 4
gpt4 key购买 nike

我正在尝试在 android 的水平 ScrollView 中创建一个水平 ScrollView 。第一个水平 ScrollView 工作正常。当我尝试滚动时,第二个水平 ScrollView 不起作用。

我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="250dp"
android:layout_height="100dp"
app:cardCornerRadius="20dp"
app:cardBackgroundColor="@color/colorPrimary"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:scrollbars="horizontal"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<LinearLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4"></Button>
</LinearLayout>
</HorizontalScrollView>
</androidx.cardview.widget.CardView>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>

</RelativeLayout>

有人可以分享您如何实现这一目标的知识吗?

最佳答案

您可以为嵌套 ScrollView 创建自定义类以支持水平平滑滚动。

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

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

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

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

public HorizontalNestedScrollView(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);
}
}

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

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

希望对您有所帮助。

关于android - 在水平 ScrollView 内创建水平 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59541045/

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