Как прокрутить NestedScrollView с ListView внутри? - PullRequest
0 голосов
/ 09 ноября 2019

Я создаю действие, в котором мне нужно показать несколько элементов и представление списка, и я хочу, чтобы, когда пользователь выполняет прокрутку, я хотел прокрутить все действия, а не только представление списка, для этого я реализовал 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>

Ответы [ 2 ]

0 голосов
/ 09 ноября 2019

попробуйте следующим образом:

<?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();
    }
0 голосов
/ 09 ноября 2019

Вложение ListView внутри ScrollView проблематично, так как вызывает многократные вертикальные прокрутки, которые вводят пользователя в заблуждение. Вместо этого используйте один RecyclerView, который может предоставлять несколько ViewTypes.

Ваш адаптер 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.
    }
}
...