【问题标题】:How to make scrollable Items within a scrollable listview?如何在可滚动的列表视图中制作可滚动的项目?
【发布时间】:2017-11-22 08:55:24
【问题描述】:

我有一个小型数据库,它将数据填充到一个列表视图中,该列表视图使用下面提供的其他布局来显示数据。
如果第一个字段的文本超过两行,则文本开始隐藏。所以我想我会让列表视图的每一行都可以滚动。虽然它显示了一个滚动条,但没有任何反应。

这是列表视图使用的布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:layout_height="75dp"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="75dp"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView
            android:id="@+id/desc_d"
            android:layout_width="165dp"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:text="Description"
            android:gravity="center"
            android:textAppearance="@android:style/TextAppearance.Large"/>

        <TextView
            android:id="@+id/cate_d"
            android:layout_width="70dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_toEndOf="@id/desc_d"
            android:text="others"
            android:textAppearance="@android:style/TextAppearance.Large" />

        <TextView
            android:id="@+id/date_d"
            android:layout_width="70dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_toEndOf="@id/cate_d"
            android:text="16/11/2017"
            android:textAppearance="@android:style/TextAppearance.Large" />

        <TextView
            android:id="@+id/amt_d"
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_toEndOf="@id/date_d"
            android:text="100000"
            android:textAppearance="@android:style/TextAppearance.Large" />

    </RelativeLayout>
</ScrollView>


我的 MainActivity 布局有一个列表视图,它也可滚动并使用上述布局。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="beforeDescendants"
        android:focusableInTouchMode="true"
        android:keepScreenOn="false"
        android:scrollbarAlwaysDrawHorizontalTrack="false"
        android:scrollbarAlwaysDrawVerticalTrack="false"
        tools:context="apps.harry.expensedata.MainActivity">

        <Button
            android:id="@+id/add_expense_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="24dp"
            android:onClick="addExpenseClick"
            android:text="@string/button_1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/view_expense_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="24dp"
            android:onClick="viewClick"
            android:text="@string/button_2"
            app:layout_constraintStart_toEndOf="@+id/add_expense_btn"
            app:layout_constraintTop_toTopOf="parent" />


        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="406dp"
            android:layout_marginTop="84dp"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_editor_absoluteX="0dp">

            <ListView
                android:id="@+id/list_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                tools:layout_editor_absoluteX="8dp"
                tools:layout_editor_absoluteY="248dp" />
        </RelativeLayout>
    </android.support.constraint.ConstraintLayout>

</ScrollView>


这是APP的截图


如您所见,该布局中的每一行都有一个滚动条,但是当我滚动时没有任何反应。
请建议我如何实现这一点。

【问题讨论】:

  • man 您的滚动视图高度仅为 android:layout_height="75dp" 75 但在您的内部 textview 与父级匹配,因此当文本视图在 75 dp 部分有多行时仅​​可见,要么使滚动视图和下一个相对布局高度换行
  • 用于滚动使用 getParent().requestDisallowInterceptTouchEvent(true);在滚动视图触摸 lisner

标签: android listview scrollview


【解决方案1】:

这不是针对您的滚动问题的解决方案建议,但由于用户体验,您应该考虑不要在可滚动列表中包含可滚动列表项。

请参阅Material Guidelines。这里有一点摘录。

列表图块最多可以包含三行文本,并且同一列表中的图块之间的文本长度可能会有所不同。要显示多于三行的文字,请使用卡片。

将最显着的内容放在图块的左侧,将最不显着的内容放在右侧。

规格:

列表图块上的大部分空间应专用于主要操作 将最显着的内容放在磁贴的左侧 在具有多行内容的磁贴中,将最有区别的内容放在第一行 将补充动作放在右侧

【讨论】:

    【解决方案2】:

    尝试使用 maxLines 和 minLines 作为 TextView 的属性以容纳所有数据。

    【讨论】:

      【解决方案3】:

      制作scrollView Height="wrap_content",而不是固定大小。也许有用

      【讨论】:

        【解决方案4】:

        谷歌在他们的指南中定义不使用滚动视图内的滚动视图,它不仅仅是指南它不能正常工作。你看到了那个案例。 Tbe 解决方案设置为您的列表项高度为 wrap_content,最佳实践如上所述,不要使用超过 2-3 行文本的列表项。查看 materisl 设计指南以查看列表项的尺寸

        【讨论】:

          【解决方案5】:

          我建议您可以将自定义列表项高度更改为 wrap_content,然后您就不需要滚动了。

          <?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:gravity="center"
              android:orientation="horizontal"
              android:padding="10dp">
          
              <TextView
                  android:id="@+id/decription"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:hint="decription" />
          
              <TextView
                  android:id="@+id/whatFor"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:hint="whatFor" />
          
              <TextView
                  android:id="@+id/date"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:hint="date" />
          
              <TextView
                  android:id="@+id/amount"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:hint="amount" />
          
          </LinearLayout>
          

          您的输出将如下图所示。

          希望您的问题能够得到解决,如果您仍有任何疑问,请询问。

          【讨论】:

            猜你喜欢
            • 2023-02-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-18
            相关资源
            最近更新 更多