【问题标题】:How to scroll NestedScrollView with a ListView inside?如何使用里面的 ListView 滚动 NestedScrollView?
【发布时间】:2020-03-05 23:49:39
【问题描述】:

我正在创建一个活动,我需要在其中显示几个元素和一个列表视图,并且我希望当用户滚动时我想滚动所有活动,而不仅仅是列表视图,为此,我实现了一个 NestedScrollView,但是滚动不起作用,它仍然没有滚动,我需要一个属性吗?还是有更好的方法来做我需要做的事情?

我附上我的 XML 代码:

<androidx.core.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"
    android:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

       <!-- Other items -->

        <ListView
            android:id="@+id/lvAC"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.core.widget.NestedScrollView>

【问题讨论】:

    标签: android android-layout android-listview


    【解决方案1】:

    试试这个方法:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        >
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <View
                android:id="@+id/view"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="@drawable/ic_launcher_background"/>
    
    
            <ListView
                android:id="@+id/lvAC"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view"
                android:nestedScrollingEnabled="true"
                />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
    

    并将此方法设置为您的 listview

    public static void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                // pre-condition
                return;
            }
    
            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
    
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
            listView.requestLayout();
        }
    

    【讨论】:

    • 感谢您的回答,但我已经实现了该方法,但它对我不起作用。
    【解决方案2】:

    在 ScrollView 中嵌套 ListView 是有问题的,因为它会导致多个垂直滚动行为,这会让用户感到困惑。相反,请使用可以提供多个 ViewType 的单个 RecyclerView。

    您的 RecyclerView 适配器应该实现getItemViewType,返回您需要显示的不同视图的数量。例如,如果您有:

    <androidx.core.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"
        android:fillViewport="true">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <!-- Other items -->
            <View
                android:id="@+id/view1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                />
            <View
                android:id="@+id/view2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/view1"
                app:layout_constraintStart_toStartOf="parent"
                />
    
            <ListView
                android:id="@+id/lvAC"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintTop_toBottomOf="@id/view2"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />
    
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    
    </androidx.core.widget.NestedScrollView>
    

    你应该有这样的布局:

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        />
    

    你的适配器应该是这样的:

    class MyViewHolder(val rootView: View) : RecyclerView.ViewHolder(rootView)
    
    class MyAdapter(val items: List<Any>) : RecyclerView.Adapter<MyViewHolder>() {
    
    
        companion object {
            const val TYPE_VIEW1 = 1
            const val TYPE_VIEW2 = 2
            const val TYPE_ITEMS = 3
        }
    
        override fun getItemViewType(position: Int): Int = when (position) {
            0 -> TYPE_VIEW1
            1 -> TYPE_VIEW2
            else -> TYPE_ITEMS
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder = when (viewType) {
            TYPE_VIEW1 -> MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_view1, parent, false))
            TYPE_VIEW2 -> MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_view2, parent, false))
            else -> MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_other, parent, false))
        }
    
        override fun getItemCount(): Int = 2 + items.size
    
        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }
    }
    

    【讨论】:

    • 感谢您的回答,我实施了您使用 RecyclerView 而不是您的代码的建议,因为它不符合我想要做的。但是根据您的建议,我创建了一个解决方案。如果不是太麻烦,我想再问你一个问题,RecyclerView中如何用Headers分割section?
    • 你需要按照我说的来,拥有多种视图类型。如果您的所有“项目”都适合相同的模具,则您可以有 2 种视图类型 - 一种用于标题,一种用于项目。您的 getItemCount() 将需要返回标题数 + 项目数。
    猜你喜欢
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    相关资源
    最近更新 更多