【问题标题】:scrollView Inside Horizontal Scrollview not working properly水平滚动视图内的滚动视图无法正常工作
【发布时间】:2018-10-04 16:51:49
【问题描述】:

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

当我尝试滚动布局时,只允许我向一个方向滚动,而不是同时向两个方向滚动。 我认为这是安卓正版问题。

请提供任何解决方案。 问候

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

【问题讨论】:

    标签: android scrollview horizontalscrollview


    【解决方案1】:

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

    布局:

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

    【讨论】:

      【解决方案2】:

      见下面的 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>     
      

      【讨论】:

      • @HikmatKhan:如果这个答案与你最初遇到的问题相同,而 another 答案(Nicholas 的答案)解决了你的问题,你为什么要标记 这个在这里回答是否被接受?
      【解决方案3】:

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

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

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

      【讨论】:

      • 感谢兄弟抽出时间。链接真的很有用,解决了我的问题。
      • @HikmatKhan:如果这解决了问题,为什么您将另一个答案(用您自己的话,不能解决问题)标记为已接受?似乎对这个答案的作者授予别人声誉有点不公平。
      【解决方案4】:

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

      recyclerView.setNestedScrollingEnabled(false);
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多