【问题标题】:Why Horizontal ScrollView not working?为什么水平滚动视图不起作用?
【发布时间】:2017-03-24 03:05:26
【问题描述】:

我正在尝试使应用程序可以垂直滚动,并且在我的 ScrollView 内部并希望让 Horizo​​ntalView 水平滚动我的一些 TableLayout,Scrollview 工作正常但水平无法滚动。我该如何解决这个问题?感谢您的帮助。

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"        >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:id="@+id/tablejdl"
                android:weightSum="7"
                android:layout_marginTop="20dp"
                >

                <TableRow
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:weightSum="7"
                    android:gravity="center_horizontal">

                    <TextView
                        android:textColor="#ffffff"
                        android:layout_marginTop="10dp"

                        android:layout_width="0dp"
                        android:textSize="11sp"
                        android:layout_height="wrap_content"
                        android:text="Barang"
                        android:layout_weight="2"
                        android:id="@+id/kodebar"
                        />
                    <TextView
                        android:textColor="#ffffff"
                        android:layout_marginTop="10dp"

                        android:layout_width="0dp"
                        android:textSize="11sp"
                        android:layout_height="wrap_content"
                        android:text="Jumlah"
                        android:layout_weight="1"
                        android:id="@+id/jumlah"
                        />
                    <TextView
                        android:textColor="#ffffff"
                        android:layout_marginTop="10dp"

                        android:layout_width="0dp"
                        android:textSize="11sp"
                        android:layout_height="wrap_content"
                        android:text="Sup1"
                        android:layout_weight="1"
                        android:id="@+id/namabar"/>
                    <TextView
                        android:textColor="#ffffff"
                        android:layout_marginTop="10dp"

                        android:layout_width="0dp"
                        android:textSize="11sp"
                        android:layout_height="wrap_content"
                        android:text="Sup2"
                        android:layout_weight="1"
                        android:id="@+id/jumlahbar"/>

                    <TextView
                        android:textColor="#ffffff"
                        android:layout_marginTop="10dp"
                        android:layout_width="0dp"
                        android:textSize="11sp"
                        android:layout_height="wrap_content"
                        android:text="Sup3"
                        android:layout_weight="4"
                        android:id="@+id/stokbar"/>
                    <TextView
                        android:textColor="#ffffff"
                        android:layout_marginTop="10dp"

                        android:layout_width="0dp"
                        android:textSize="11sp"
                        android:layout_height="wrap_content"
                        android:text="Total"
                        android:layout_weight="2"
                        android:id="@+id/jtotal"/>
                </TableRow>
            </TableLayout>

                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/tablejdl"
                    android:id="@+id/recyclerpenawaran">
                </android.support.v7.widget.RecyclerView>

            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/recyclerpenawaran"
                android:id="@+id/tablejdl2"
                android:weightSum="7"
                android:layout_marginTop="15dp"
                >
                <TableRow
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:weightSum="7"
                    android:gravity="center_horizontal">

                    <TextView
                        android:textColor="#ffffff"
                        android:layout_marginTop="10dp"
                        android:layout_width="0dp"
                        android:textSize="11sp"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        />
                    <TextView
                        android:textColor="#ffffff"
                        android:layout_marginTop="10dp"
                        android:layout_width="0dp"
                        android:textSize="11sp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"

                        />
                    <CheckBox
                        android:textColor="#ffffff"
                        android:layout_marginTop="10dp"
                        android:layout_width="0dp"
                        android:textSize="11sp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:id="@+id/checksup1"/>
                    <CheckBox
                        android:textColor="#ffffff"
                        android:layout_marginTop="10dp"
                        android:layout_width="0dp"
                        android:textSize="11sp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:id="@+id/checksup2"/>

                    <CheckBox
                        android:textColor="#ffffff"
                        android:layout_marginTop="10dp"

                        android:layout_width="0dp"
                        android:textSize="11sp"
                        android:layout_height="wrap_content"

                        android:layout_weight="1"
                        android:id="@+id/checksup3"/>
                    <TextView
                        android:textColor="#ffffff"
                        android:layout_marginTop="10dp"
                        android:layout_width="0dp"
                        android:textSize="11sp"
                        android:layout_height="wrap_content"
                        android:text="Total"
                        android:layout_weight="1"
                        android:id="@+id/total"/>
                </TableRow>
            </TableLayout>
        </RelativeLayout>
    </LinearLayout>
</HorizontalScrollView>

我已经为我的子 Scrollview 布局添加了一些代码,可以滚动,但它不起作用。

scrool = (ScrollView)findViewById(R.id.vertical);
        scrollView = (HorizontalScrollView)findViewById(R.id.horizontal);
        scrool.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                scrollView.getParent().requestDisallowInterceptTouchEvent(false);
                return false;
            }
        });

       scrollView.setOnTouchListener(new View.OnTouchListener() {
           @Override
           public boolean onTouch(View v, MotionEvent event) {

               v.getParent().requestDisallowInterceptTouchEvent(true);
               return false;
           }
       });

【问题讨论】:

  • 因为您的 Horizo​​ntalScrollView 在另一个滚动条中,它也具有相同的垂直滚动能力。禁用父垂直滚动应该可以工作。
  • 感谢您的回答,但我也想在我的应用程序中垂直滚动。 @K.Sopheak
  • 尝试只使用recyclerview,你不需要那个horizo​​ntalscrollview 视图组。 RecyclerView 有了更多更新的方法,可以轻松实现。
  • 所以你可以在滚动子滚动视图时禁用父滚动视图。让我们看看这个答案可能会有所帮助。 stackoverflow.com/q/4767519/5241603stackoverflow.com/q/6210895/5241603

标签: android scrollview android-tablelayout horizontalscrollview


【解决方案1】:

您可以为此目的使用 NestedScrollview:-

 <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">
 <HorizontalScrollView
                 android:layout_width="wrap_content"
                android:layout_height="match_parent">
                 ...... 
 </HorizontalScrollView>
 </android.support.v4.widget.NestedScrollView>

【讨论】:

  • 您需要在两者中进行更改,在代码中更改:- scool = (NestedScrollView)findViewById(R.id.vertical);
猜你喜欢
  • 2014-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-13
  • 1970-01-01
相关资源
最近更新 更多