ScrollView 中的 ListView 在 Android 上不滚动 [英] ListView inside ScrollView is not scrolling on Android

查看:33
本文介绍了ScrollView 中的 ListView 在 Android 上不滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ScrollView 内滚动 ListView 时遇到问题.我有一个活动,它在顶部有一些 EditText,然后是一个带有两个选项卡的选项卡主机,每个选项卡都有一个 ListView.当 EditText 视图聚焦时,软键盘出现,因为我有一个 ScrollView,内容是可滚动的.但是当 ListViews 中有更多项目(选项卡中的项目)时,问题就出现了,即使有更多项目,我也无法滚动 ListView.

I am having trouble with a scrolling ListView inside a ScrollView. I have an Activity which has some EditTexts in the top part and then a tab host with two tabs which have one ListView each. When the EditText views are focused, the soft keyboard comes up and as I have a ScrollView, the content is scrollable. But the problem comes when there are more items in ListViews (ones in tabs), I am not able to scroll the ListView, even if there are more items.

以下是布局 XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="?backgroundImage"
    android:orientation="vertical">

  <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:layout_alignParentBottom="true"
        android:layout_margin="10dip"
        android:id="@+id/buttons">
    <Button
            android:text="Save"
            android:layout_margin="2dip"
            android:textSize="20dip"
            android:id="@+id/btnSaveorUpdate"
            android:layout_height="wrap_content"
            android:layout_width="145dip"></Button>
    <Button
            android:text="Cancel"
            android:layout_margin="2dip"
            android:textSize="20dip"
            android:id="@+id/btnCancelorDelete"
            android:layout_height="wrap_content"
            android:layout_width="145dip"></Button>
  </LinearLayout>
  <ScrollView
        android:layout_above="@id/buttons"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:layout_margin="10dip">
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="10dip">
      <TextView
                android:text="Bill details"
                android:textColor="?textColorDark"
                android:layout_alignParentTop="true"
                android:id="@+id/txtEnterDetails"
                android:textSize="25dip"
                android:textStyle="bold"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_marginBottom="2dip"></TextView>
      <LinearLayout
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:layout_width="0dip"
                android:layout_height="0dip" />
      <EditText
                android:layout_width="fill_parent"
                android:hint="Enter data"
                android:inputType="numberDecimal"
                android:id="@+id/txtSample"
                android:textSize="@dimen/editText"
                android:layout_height="@dimen/editTextHeight"
                android:text=""></EditText>
      <EditText
                android:layout_width="fill_parent"
                android:id="@+id/txtDescription"
                android:hint="Enter description"
                android:textSize="@dimen/editText"
                android:layout_height="@dimen/editTextHeight"
                android:inputType="text"
                android:text=""></EditText>
      <EditText
                android:layout_width="fill_parent"
                android:id="@+id/txtComment"
                android:hint="Enter comment (if any)"
                android:textSize="@dimen/editText"
                android:layout_height="@dimen/editTextHeight"
                android:inputType="text"
                android:text=""></EditText>
      <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
        <TextView
                    android:id="@+id/txtDate"
                    android:layout_width="wrap_content"
                    android:text=""
                    android:textSize="20dip"
                    android:textColor="?textColorDark"
                    android:layout_marginLeft="10dip"
                    android:layout_height="@dimen/editTextHeight"
                    android:layout_gravity="center_vertical" />
        <Button
                    android:id="@+id/btnPickDate"
                    android:layout_width="wrap_content"
                    android:layout_height="@dimen/editTextHeight"
                    android:text="Select date"
                    android:layout_margin="2dip"
                    android:textSize="15dip"
                    android:layout_gravity="center_vertical" />
      </LinearLayout>
      <TabHost
                android:id="@+id/tabhost"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
        <LinearLayout
                    android:id="@+id/linearLayout1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">
          <TabWidget
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:id="@android:id/tabs"></TabWidget>
          <FrameLayout
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:id="@android:id/tabcontent">
            <ScrollView
                            android:layout_above="@id/buttons"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:fillViewport="true"
                            android:id="@+id/tab1">
              <LinearLayout
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical">
                <TableLayout
                                    android:layout_height="wrap_content"
                                    android:layout_width="fill_parent">
                  <TableRow
                                        android:id="@+id/tableRow1"
                                        android:layout_marginLeft="2dip"
                                        android:layout_marginRight="5dip"
                                        android:layout_width="fill_parent"
                                        android:layout_height="wrap_content">
                    <LinearLayout
                                            android:layout_width="fill_parent"
                                            android:layout_height="wrap_content"
                                            android:orientation="horizontal">
                      <ImageView
                                                android:src="@drawable/ic_menu_invite"
                                                android:layout_width="40dip"
                                                android:layout_height="40dip"
                                                android:layout_gravity="center_vertical"></ImageView>
                      <TextView
                                                android:text="Add friend"
                                                android:layout_height="wrap_content"
                                                android:layout_width="fill_parent"
                                                android:layout_centerVertical="true"
                                                android:textColor="?textColorDark"
                                                android:textSize="@dimen/editText"
                                                android:layout_gravity="center_vertical" />
                    </LinearLayout>
                  </TableRow>
                  <TableRow
                                        android:layout_width="fill_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_marginLeft="5dip"
                                        android:layout_marginRight="5dip">
                    <TextView
                                            android:id="@+id/txtData1"
                                            android:layout_width="170dip"
                                            android:layout_height="wrap_content"
                                            android:text="Data"
                                            android:textSize="14dip"
                                            android:textStyle="bold"
                                            android:textColor="#000000">
                    </TextView>
                    <TextView
                                            android:id="@+id/txtData2"
                                            android:layout_width="wrap_content"
                                            android:layout_height="wrap_content"
                                            android:text="Sample"
                                            android:textSize="13dip"
                                            android:textColor="#000000"></TextView>

                  </TableRow>
                </TableLayout>
                <ListView
                                    android:cacheColorHint="#00000000"
                                    android:id="@+id/ListView01"
                                    android:layout_height="wrap_content"
                                    android:layout_width="fill_parent" />
              </LinearLayout>
            </ScrollView>
            <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center_horizontal"
                            android:id="@+id/tab2"
                            android:orientation="vertical">
              <TableLayout
                                android:layout_height="wrap_content"
                                android:layout_width="fill_parent">
                <TableRow
                                    android:id="@+id/tableRow2"
                                    android:layout_marginLeft="2dip"
                                    android:layout_marginRight="5dip"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content">
                  <LinearLayout
                                        android:layout_width="fill_parent"
                                        android:layout_height="wrap_content"
                                        android:orientation="horizontal">
                    <ImageView
                                            android:src="@drawable/ic_menu_invite"
                                            android:layout_width="40dip"
                                            android:layout_height="40dip"
                                            android:layout_gravity="center_vertical"></ImageView>
                    <TextView
                                            android:text="Sample"
                                            android:layout_height="wrap_content"
                                            android:layout_width="fill_parent"
                                            android:layout_centerVertical="true"
                                            android:textColor="?textColorDark"
                                            android:textSize="@dimen/editText"
                                            android:layout_gravity="center_vertical" />
                  </LinearLayout>
                </TableRow>
                <TableRow
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="5dip"
                                    android:layout_marginRight="5dip">
                  <TextView
                                        android:id="@+id/txtUser1"
                                        android:layout_width="170dip"
                                        android:layout_height="wrap_content"
                                        android:text="User"
                                        android:textSize="14dip"
                                        android:textStyle="bold"
                                        android:textColor="#000000">
                  </TextView>
                  <TextView
                                        android:id="@+id/txtUserData"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:text="UserData"
                                        android:textSize="13dip"
                                        android:textColor="#000000"></TextView>
                </TableRow>
              </TableLayout>

              <ListView
                                android:cacheColorHint="#00000000"
                                android:id="@+id/ListView02"
                                android:layout_height="wrap_content"
                                android:layout_width="fill_parent" />

            </LinearLayout>


          </FrameLayout>
        </LinearLayout>
      </TabHost>
    </LinearLayout>
  </ScrollView>
</RelativeLayout>

请谁能告诉我这里的问题是什么?我在 ScrollView 中的 ListView 问题上有另一篇文章,但在我的情况下它们没有用.

Please can anyone tell me what the problem is here? I have another post on the ListView inside ScrollView problem, but they were of no use in my case.

推荐答案

你不应该把 ListView 放在 ScrollView 中,因为 ListView> 类实现自己的滚动,它只是不接收手势,因为它们都由父 ScrollView 处理.我强烈建议您以某种方式简化您的布局.例如,您可以添加要滚动到 ListView 的视图作为页眉或页脚.

You shouldn't put a ListView inside a ScrollView because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent ScrollView. I strongly recommend you to simplify your layout somehow. For example you can add views you want to be scrolled to the ListView as headers or footers.

更新:

从 API Level 21 (Lollipop) 开始,Android SDK 正式支持嵌套滚动容器.ViewViewGroup 类中有很多方法提供了这个功能.要在 Lollipop 上进行嵌套滚动,您必须通过将 android:nestedScrollingEnabled="true" 添加到其 XML 声明或通过显式调用 setNestedScrollingEnabled(true)<来为子滚动视图启用它/代码>.

Starting from API Level 21 (Lollipop) nested scroll containers are officially supported by Android SDK. There're a bunch of methods in View and ViewGroup classes which provide this functionality. To make nested scrolling work on the Lollipop you have to enable it for a child scroll view by adding android:nestedScrollingEnabled="true" to its XML declaration or by explicitly calling setNestedScrollingEnabled(true).

如果您想在 Lollipop 之前的设备上进行嵌套滚动(您可能会这样做),则必须使用 Support 库中的相应实用程序类.首先,您必须将 ScrollView 替换为 嵌套滚动视图.后者同时实现了 NestedScrollingParentNestedScrollingChild 因此它可以用作父滚动容器或子滚动容器.

If you want to make nested scrolling work on pre-Lollipop devices, which you probably do, you have to use corresponding utility classes from the Support library. First you have to replace you ScrollView with NestedScrollView. The latter implements both NestedScrollingParent and NestedScrollingChild so it can be used as a parent or a child scroll container.

但是ListView 不支持嵌套滚动,因此您需要对其进行子类化并实现NestedScrollingChild.幸运的是,Support 库提供了 NestedScrollingChildHelper 类,所以你只需要创建这个类的一个实例,并从你的视图类的相应方法中调用它的方法.

But ListView doesn't support nested scrolling, therefore you need to subclass it and implement NestedScrollingChild. Fortunately, the Support library provides NestedScrollingChildHelper class, so you just have to create an instance of this class and call its methods from the corresponding methods of your view class.

这篇关于ScrollView 中的 ListView 在 Android 上不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆