水平滚动视图内的滚动视图无法正常工作

发布于 2024-12-10 14:37:01 字数 5162 浏览 0 评论 0原文

希望我们玩得开心。伙计,我遇到了一个小问题。当我将垂直滚动视图放在水平滚动视图内时,垂直滚动视图无法正常工作。(我也尝试反之亦然,将水平滚动视图放在垂直内。)

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

Hope we have good time.dude i m stuck in a little problem .when i place vertical scroll view inside horizontal scroll view then vertical scrollview not works properly.(i tried vice verse too by placing horizontal scroll view inside vertical.)

when i try to scroll the layout just allow me to scroll in one direction not in both simultaneously.
i think this is android genuine problem.

kindly provide any solution .
Regards

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我喜欢麦丽素 2024-12-17 14:37:01

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

布局:

<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;
        }
    });

您可以更改滚动视图的顺序。只需更改它们在布局和代码中的顺序即可。显然,您可以放置​​想要双向滚动的布局/视图,而不是 WatereverViewYouWant。

There is much more simpler solution than creating a custom view:

Layout:

<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>

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

You can change the order of the scrollviews. Just change their order in layout and in the code. And obviously instead of WateverViewYouWant you put the layout/views you want to scroll both directions.

尹雨沫 2024-12-17 14:37:01

请参阅下面的 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>     

See Below xml.This works for me.Hope will work for you too.

 <?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>     
夏至、离别 2024-12-17 14:37:01

我不确定你能以这种方式实现你想要的目标。至少根据我读到的内容不是这样。

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

http://blog.gorges.us/2010/06/android-二维滚动视图/

I'm not sure you can achieve what you're trying to that way. Atleast not according to what I've read.

However, here is a blog by a guy who made a custom scrollview, that might fit your needs.

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

风为裳 2024-12-17 14:37:01

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

recyclerView.setNestedScrollingEnabled(false);

如果您有多个 Horizo​​ntalScrollView对于 ScrollView ,您可以对 Horizo​​ntalScrollView 中的每个 RecyclerView 使用相同的代码,该代码位于滚动视图

If you are using displaying items in RecyclerView inside the HorizontalScrollView, just make it simple by adding a line of code:

recyclerView.setNestedScrollingEnabled(false);

If you have multiple HorizontalScrollView inside the ScrollView you can can use the same above code for each RecyclerView in HorizontalScrollView which is inside the Scrollview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文