简体   繁体   English

如何在可滚动列表视图中创建可滚动项?

[英]How to make scrollable Items within a scrollable listview?

I have small database which populates data into a listview which uses other layout as provided below to show the data. 我有一个小的数据库,该数据库将数据填充到一个列表视图中,该列表视图使用下面提供的其他布局来显示数据。
What happens is if the 1st field's text is more than two lines, the text starts hiding. 如果第一字段的文本超过两行,则文本开始隐藏。 So I thought i would make that listview's each row scrollable. 所以我想我可以使listview的每一行滚动。 Though it shows a scroll bar but nothing happens. 尽管它显示滚动条,但是什么也没有发生。

This is the layout which is used by listview 这是listview使用的布局

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


My MainActivity layout has a listview which is also scrollable and uses above layout. 我的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>


Here's the screen shot of the APP 这是APP的屏幕截图
在此处输入图片说明


As you can see there's a scroll bar available for each row in that layout, but nothing happens when I scroll. 如您所见,该布局中的每一行都有一个滚动条,但是滚动时什么也没有发生。
Please suggest me how this could be achieved. 请建议我如何实现。

This is not a solution suggestion for your scrolling problem, but due to user experience you should consider to not habe scrollable list items inside a scrollable list. 这不是解决滚动问题的建议,但是由于用户体验,您应该考虑不要将可滚动列表项包含在可滚动列表中。

See Material Guidelines for this. 参见材料指南 Here is a little excerpt. 这是一个小节选。

List tiles may contain up to three lines of text, and text length may vary between tiles in the same list. 列表图块最多可包含三行文本,并且同一列表中图块之间的文本长度可能会有所不同。 To display more than three lines of text, use a card. 要显示三行以上的文字,请使用卡片。

Place the most distinguishing content on the left of the tile and the least distinguishing content on the right. 将最有区别的内容放在图块的左侧,将最不显眼的内容放在右侧。

Specifications: 产品规格:

The majority of space on a list tile should be dedicated to the primary action Place the most distinguishing content towards the left of the tile In tiles with multi-line content, place the most distinguishing content in the first line Place supplemental actions on the right side 列表图块上的大部分空间应专用于主要操作将最有区别的内容朝图块的左侧放置在多行内容的图块中,将最有区别的内容放在第一行中将补充动作放在右侧

Make scrollView Height="wrap_content" instead of fixed size . 使scrollView Height="wrap_content"而不是固定的size maybe useful 也许有用

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

Google defined in their guidelines do not use scrollview inside of scrollview and it is more then simply guidelines it doesnt work properly. Google在其准则中定义的Google不在scrollview中使用scrollview,而不仅仅是准则,它不能正常工作。 and you see that case. 然后您看到这种情况。 Tbe solution is set to your listitem height as wrap_content and the best practices is as was mentioned above do not use listitems with more than 2-3 lines of text. 将解决方案设置为您的listitem高度为wrap_content,并且最佳实践如上所述,请勿将文本超过2至3行的列表项使用。 take a look into materisl design guide to see the dimensions of list items 查看材料设计指南以查看列表项的尺寸

I would suggest you to can change your custom list item height to wrap_content and then you don't need scrolling. 我建议您可以将自定义列表项目的高度更改为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>

Your output will be like below image. 您的输出将如下图所示。

在此处输入图片说明

I hope your issue will be resolved and if you still have any query then please ask. 希望您的问题能得到解决,如果仍有疑问,请询问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM