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

4 回答 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,而是您想要双向滚动的布局/视图。

于 2013-01-10T10:48:31.390 回答
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>     
于 2011-10-19T10:28:28.113 回答
2

我不确定您是否可以通过这种方式实现您的目标。至少不是根据我所读到的。

但是,这是一个制作自定义滚动视图的人的博客,可能适合您的需求。

http://blog.gorges.us/2010/06/android-two-dimensional-scrollview/

于 2011-10-19T12:40:54.850 回答
0

如果您在 中使用显示项目RecyclerViewHorizontalScrollView只需添加一行代码即可使其变得简单:

recyclerView.setNestedScrollingEnabled(false);

如果你有多个HorizontalScrollView里面,ScrollView你可以对里面的每个使用相同RecyclerViewHorizontalScrollView代码Scrollview

于 2018-10-04T08:35:47.657 回答