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

标签 android android-layout

我正在尝试在 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/

相关文章:

android - 跨不同 Activity 保存和加载布局

android - 在我的第一个布局资源中包含第二个布局资源

android - 如何在 Android WebView 中使用 CSS3?

java - checkin 函数时数组大小错误

android - 如何从/dev/iio :deviceX?中读取LSM330的数据

android - 设置 android :layout_column = "1" 时 TableLayout 不跳过第 0 列

android - 如何使 SearchView 始终在 android 中展开?

Android - 找不到命令

java - 无法使用命令行编译 Android/Java 项目

android - 为 child 设置默认重力