【发布时间】: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>
如您所见,该布局中的每一行都有一个滚动条,但是当我滚动时没有任何反应。
请建议我如何实现这一点。
【问题讨论】:
-
man 您的滚动视图高度仅为 android:layout_height="75dp" 75 但在您的内部 textview 与父级匹配,因此当文本视图在 75 dp 部分有多行时仅可见,要么使滚动视图和下一个相对布局高度换行
-
用于滚动使用 getParent().requestDisallowInterceptTouchEvent(true);在滚动视图触摸 lisner
标签: android listview scrollview