android - 如何使用 ListView 使 fragment 可滚动

标签 android listview android-fragments android-listview

我想要一个具有 2 个 ListView 的 fragment 。我不希望 listView 滚动,我只希望整个 Fragment 滚动。继承人的代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff" >

    <TextView
        android:id="@+id/day"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center_horizontal"
        android:paddingTop="15dp"
        android:text="Monday"
        android:textColor="#000"
        android:textSize="55sp" />

    <TextView
        android:id="@+id/date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/day"
        android:gravity="center_horizontal"
        android:text="27 April, 2014"
        android:textColor="#000"
        android:textSize="45sp" />

    <ListView
        android:id="@+id/home_list_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/date"
        android:layout_margin="10dp"
        android:background="@color/list_home_time_bg"
        android:divider="@android:color/white"
        android:dividerHeight="20.0sp" />

    <ListView
        android:id="@+id/home_list_stime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/home_list_time"
        android:layout_margin="10dp"
        android:background="@color/list_home_stime_bg"
        android:divider="@android:color/white"
        android:dividerHeight="20.0sp" />

    </RelativeLayout>

当我运行这段代码时,第二个 listView 有自己的滚动条,有没有办法阻止它并使整个 Fragment 滚动?

最佳答案

要停止 ListView 滚动使用 -

listView.setScrollContainer(false);

要滚动 fragment ,请将其放入 ScrollView -

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <!-- everything you already have -->
</ScrollView>

更多检查 - how-to-get-a-non-scrollable-listview .

注意:

如果将 ListView 放在 ScrollView 中,则所有 ListView 都不会拉伸(stretch)到其全高。以下是解决此问题的方法。

/**** Method for Setting the Height of the ListView dynamically.
 **** Hack to fix the issue of not showing all the items of the ListView
 **** when placed inside a ScrollView  ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

要使用此方法,只需将 ListView 传递给此方法:

ListView list1 = (ListView) view.findViewById(R.id.home_list_time);
setListViewHeightBasedOnChildren(list1);

ListView list2 = (ListView) view.findViewById(R.id.home_list_stime);
setListViewHeightBasedOnChildren(list2);

更多信息 - android-list-view-inside-a-scroll-view .

关于android - 如何使用 ListView 使 fragment 可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24631574/

相关文章:

android - Galaxy S5 Lollipop - 并非所有断点都在 Android Studio 调试器下停止执行

java - 使 ConstraintLayout 组件在简单的 UI 中填充整个显示

android - 设置背景颜色的个性化 ListView 错误

android - 如何从我的自定义适配器类向数据库添加项目?

android - 在 Android 导航组件中使用 navigateUp 传递数据/包

java - 无尽的 ViewPager(不循环)

java - Firebase 数据库中的 setValue() 方法不起作用。值没有添加到数据库中

android - 是否有记录在案的 Android 版本接收安全更新的生命周期?

c# - 每行带有按钮的 WPF ListView

android - 在 Fragment 中关闭软键盘