scrollView Inside Horizo​​ntal Scrollview无法正常工作

时间:2011-10-19 10:24:30

标签: android scrollview horizontalscrollview

希望我们有愉快的时间。我遇到了一个小问题。当我将垂直滚动视图放在水平滚动视图中时,垂直滚动视图无法正常工作。(我通过在垂直方向内放置水平滚动视图来尝试反之亦然。)< / p>

当我尝试滚动布局时,只允许我向一个方向滚动,而不是同时滚动。 我认为这是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 个答案:

答案 0 :(得分:8)

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

布局:

<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,而是将所需的布局/视图滚动到两个方向。

答案 1 :(得分: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>     

答案 2 :(得分:2)

我不确定你能达到你想要的那种方式。至少不是根据我所读到的。

然而,这是一个由制作自定义滚动视图的人撰写的博客,可能符合您的需求。

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

答案 3 :(得分:0)

如果您要在RecyclerView内的HorizontalScrollView中显示项目,只需添加一行代码即可使其简单:

recyclerView.setNestedScrollingEnabled(false);

如果HorizontalScrollView中有多个ScrollView,则可以对RecyclerViewHorizontalScrollView中的每个Scrollview使用相同的上述代码。

相关问题