ScrollView内部的水平ScrollView无法正常工作

5
希望我们能愉快地度过时光。伙计,我遇到了一个小问题。当我将垂直滚动视图放置在水平滚动视图中时,垂直滚动视图无法正常工作。(我也尝试过相反的方式,即将水平滚动视图放置在垂直滚动视图中。)
当我尝试滚动布局时,它只允许我在一个方向上滚动,而不是同时在两个方向上滚动。我认为这是Android的真正问题。
请提供任何解决方案。 敬礼。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1" >

    <LinearLayout
        android:id="@+id/linearLayout21"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:background="@drawable/top_bar_bg" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="1dip"
            android:layout_marginTop="1dip"
            android:text=" Image card"
            android:textAppearance="?android:attr/textAppearanceLarge" >
        </TextView>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Description"
            android:textSize="20sp" >
        </TextView>

        <EditText
            android:id="@+id/IMAGEVIEW_TEXTVEIW"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_below="@+id/textView1"
            android:clickable="false"
            android:cursorVisible="false"
            android:focusable="false"
            android:gravity="top" >
        </EditText>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Image"
            android:textSize="20sp" >
        </TextView>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip"
        android:layout_marginTop="5dip"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <HorizontalScrollView
                android:id="@+id/horizontalScrollView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="fill_vertical|fill_horizontal" >

                <RelativeLayout
                    android:id="@+id/RelativeLayout01"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center"
                    android:layout_margin="5dip" >

                    <ScrollView
                        android:id="@+id/scrollView1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" >

                        <RelativeLayout
                            android:id="@+id/RelativeLayout01"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center" >

                            <RelativeLayout
                                android:id="@+id/relativeLayout2"
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent"
                                android:layout_centerInParent="true" >

                                <ImageView
                                    android:id="@+id/IMAGE_VIEW"
                                    android:layout_width="fill_parent"
                                    android:layout_height="fill_parent"
                                    android:layout_alignParentLeft="true"
                                    android:layout_alignParentTop="true"
                                    android:src="@drawable/diamond" >
                                </ImageView>
                            </RelativeLayout>
                        </RelativeLayout>
                    </ScrollView>
                </RelativeLayout>
            </HorizontalScrollView>
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>
4个回答

11

有一个比创建自定义视图更简单的解决方案:

布局:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ScrollView 
        android:id="@+id/scrollVertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <WateverViewYouWant/>

    </ScrollView>
</HorizontalScrollView>

代码(onCreate / onCreateView):

    final HorizontalScrollView hScroll = (HorizontalScrollView) value.findViewById(R.id.scrollHorizontal);
    final ScrollView vScroll = (ScrollView) value.findViewById(R.id.scrollVertical);
    vScroll.setOnTouchListener(new View.OnTouchListener() { //inner scroll listener         
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return false;
        }
    });
    hScroll.setOnTouchListener(new View.OnTouchListener() { //outer scroll listener         
        private float mx, my, curX, curY;
        private boolean started = false;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            curX = event.getX();
            curY = event.getY();
            int dx = (int) (mx - curX);
            int dy = (int) (my - curY);
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    if (started) {
                        vScroll.scrollBy(0, dy);
                        hScroll.scrollBy(dx, 0);
                    } else {
                        started = true;
                    }
                    mx = curX;
                    my = curY;
                    break;
                case MotionEvent.ACTION_UP: 
                    vScroll.scrollBy(0, dy);
                    hScroll.scrollBy(dx, 0);
                    started = false;
                    break;
            }
            return true;
        }
    });

您可以更改滚动视图的顺序。只需在布局和代码中更改它们的顺序即可。显然,您需要将WateverViewYouWant替换为您想要沿两个方向滚动的布局/视图。


3
请参见以下 XML。这对我很有效,希望对您也有用。
 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical">


        <HorizontalScrollView android:id="@+id/horizontal_scroll_view"
            android:layout_width="wrap_content" android:layout_height="wrap_content"

            android:scrollbars="horizontal">


            <ScrollView android:id="@+id/vertical_scroll_view"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:scrollbars="vertical">



                <LinearLayout android:id="@+id/linear_layout"
                    android:layout_width="fill_parent" android:layout_height="wrap_content">

                    <TableLayout android:layout_width="wrap_content"
                        android:layout_height="wrap_content" android:id="@+id/layout">
                        <!--

                            <TableRow android:layout_width="wrap_content"
                            android:layout_height="wrap_content" > <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="ksjdhfksjdhfksjdhfksdhfksjdhfksjdhfksjdhfkjsdhfkjsdfhkjsdfhkjhkjhkjhkjhkjhkjhkjhkjhkjhkjhkjhkjjjjjjjjjjjjjjjjjjjj"
                            /> </TableRow>
                        -->
                    </TableLayout>

                    <!--
                        childrens go here.. I have used an image view for demonstration
                    -->



        </LinearLayout>

        </ScrollView>

    </HorizontalScrollView>

</LinearLayout>     

1
@HikmatKhan:如果这个答案仍然存在你最初遇到的问题,而另一个答案(由Nicholas提供)解决了你的问题,为什么你会将这个答案标记为已接受的答案呢? - O. R. Mapper

2

感谢您的时间,兄弟。 链接非常有用,解决了我的问题。 - Hikmat Khan
1
@HikmatKhan:如果这个解决了问题,为什么你会选择另一个答案(根据你自己的话,它并不能解决问题)作为被采纳的答案呢?这似乎对这个答案的作者有些不公平,因为你把声誉授予了其他人。 - O. R. Mapper

0
如果您正在使用HorizontalScrollView来显示RecyclerView中的项目,只需添加一行代码即可使其变得简单:
recyclerView.setNestedScrollingEnabled(false);

如果你在ScrollView里面有多个HorizontalScrollView,你可以对每个在HorizontalScrollView里的RecyclerView使用上述代码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接