如何在NestedScrollView中嵌套ListView?

9
我创建了一个应用程序,其中一个页面需要从网络服务动态加载内容。我想要一个可以与NestedScrollView内的线性布局一起滚动的listview。但是当内容加载到listview中时,它没有完全拉伸到其最大高度。
这是我的代码。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.myquestionth.myquestionth10.Profile2Activity"
    tools:showIn="@layout/activity_profile2">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="#BBBBBB" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Media heading"
                android:id="@+id/textView2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis."
                android:id="@+id/textView8" />

        </LinearLayout>

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#70bcf5" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

我搜索了一些关于scrollview不能嵌套的内容。这是一个例子,我想根据我的布局设计来实现谷歌应用商店评论页面。他们使用了什么方法?如果我做错了什么,请给我建议。非常感谢。

enter image description here

这是我想要的。

enter image description here


1
固定ListView的高度。或者最好的方法是使用CoordinatorLayout和RecyclerView。请参考此链接 - Ravi Theja
4个回答

6
好的,我建议你有两种方法来解决这个问题:
1)尝试将LinearLayout作为ListView的标题。请注意,标题应该按照此处的方式充气。
2)你提到你使用了NestedScrollView,所以也许你应该尝试用LinearLayout替换NestedScrollView中的ListView,就像聪明人在这里建议的那样,在循环中添加行视图,类似于适配器的工作方式。
祝你好运!

6

从安卓5.0版本开始,您可以使用

yourtListView.setNestedScrollingEnabled(true);

此选项可启用或禁用此视图的嵌套滚动。如果需要与旧版本操作系统向后兼容,则必须使用RecyclerView。


3

如果您想在嵌套滚动视图中展开列表视图,请使用此代码。在创建列表视图后,将列表视图引用传递给函数末尾。

private static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.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, ViewGroup.LayoutParams.WRAP_CONTENT));

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

1

建议将所有内容放在ListView内,而不是在LinearLayout下方和ScrollView中。

是的,你可以这样做。

在你的适配器上实现(覆盖)以下方法:

public class MyAdapter extends BaseAdapter {
    // One view to Header
    // One view to filter options ("most helpful first" and "Options")
    // One view to comments
    private final static int VIEW_HEADER = 0;
    private final static int VIEW_OPTIONS = 1;
    private final static int VIEW_COMMENTS = 2;
    private final static int VIEW_TYPE_MAX = 3;

    @Override
    public int getViewTypeCount () {
        // It will return 3 since I have 3 different types of VIEW
        return VIEW_TYPE_MAX;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return VIEW_HEADER;
        else if (position == 1)
            return VIEW_OPTIONS;
        else
            return VIEW_COMMENTS;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            if(getItemViewType(position) == VIEW_HEADER)
                // Inflate HEADER Layout
            else if (getItemViewType(position) == VIEW_OPTIONS)
                // Inflate Options Layout
            else
                // Inflate comments Layout
        }

        // Fill the view contents according to its type
        ....
        return convertView;
    }
}

Android会重复使用视图。但是Android始终会重复使用相同类型的视图。

谢谢Guiherme,我会尝试这个。 - Marcus
欢迎您。也可以尝试一下Alex Pepper的答案,它似乎更简单。 - guipivoto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接