禁用片段的某些部分进行滚动,即使它在嵌套的滚动视图中

Disable certain part of fragment for scrolling even though it's inside a nested scrollview

目前我正在完成项目的最后细节,我正在尝试将某个部分保留在嵌套滚动视图中以留在屏幕顶部,只让 recyclerview 滚动。

这是我现在拥有的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/marktplaats_refresh"
    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">
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/imageView14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:tint="#9F000000"
                app:layout_constraintCircleRadius="10dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/offer_bg"/>

            <Button
                android:id="@+id/verkoopEenProduct"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="10dp"
                android:background="@null"
                android:text="Verkoop een product"
                android:textColor="@color/white"
                android:textSize="12sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

            <EditText
                android:id="@+id/button5"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="10dp"
                android:background="@drawable/light_contact_buttons"
                android:hint="Zoek in marktplaats"
                android:textAlignment="center"
                android:gravity="center_vertical"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView33"/>

            <TextView
                android:id="@+id/textView32"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:text="Marktplaats"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/verkoopEenProduct"/>

            <TextView
                android:id="@+id/textView33"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:text="Hier vind u alles wat u nodig\nhebt voor uw praktijk"
                android:textColor="@color/white"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView32"/>

            <TextView
                android:id="@+id/textView34"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:text="Alle producten"
                android:textSize="18sp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imageView14"/>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/marktplaats_recyclerview"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView34"/>

            <Button
                android:id="@+id/button11"
                android:translationX="-20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:background="@null"
                android:rotation="180"
                android:text="&#x279C;"
                android:textColor="@color/white"
                android:textSize="36sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

这是我的科特林


class MarktplaatsOverview : ApplicationFragment(){
    var products: Array<MarketModel> = arrayOf()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_marktplaats, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        marktplaats_refresh.setOnRefreshListener {
            refreshData()
            marktplaats_refresh.isRefreshing = false
        }

        button11.setOnClickListener {
            navController.navigate(R.id.action_marktplaatsOverview_to_more)
        }
        button5.requestFocus()

        refreshData()

        verkoopEenProduct.setOnClickListener {
            navController.navigate(R.id.action_marktplaatsOverview_to_marktplaatsDetail)
        }
    }

    private fun refreshData() {
        if (DataStorage.market.isNotEmpty()) {
            setupRecyclerView()
        } else {
            SharedInstance.api.getAllMarketItems {
                if (isAdded) {
                    setupRecyclerView()
                }
            }
        }
    }

    private fun setupRecyclerView(){
        SharedInstance.api.getAllMarketItems {
            products = DataStorage.market
            val xLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true)
            xLayoutManager.stackFromEnd = true
            marktplaats_recyclerview.layoutManager = xLayoutManager
            marktplaats_recyclerview.adapter = MarktplaatsAdapter(products)
        }
    }
}

有人能解决这个棘手的问题吗?我一直在尝试对 swiperefreshlayout、scrollviews 和 constraints 进行一些重组,但到目前为止没有成功。 我在互联网上也找不到任何人想要和我一样的东西...

根据你的问题描述,我假设你遇到了一个问题,即当你打开屏幕时回收器视图滚动或自动获取焦点,你需要将布局固定在顶部,所以将这个字段传递给你的父布局。

android:descendantFocusability="blocksDescendants"

或替换您的 SwipeRefreshLayout

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/marktplaats_refresh"
    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:descendantFocusability="blocksDescendants"
    android:layout_height="match_parent">

希望对你有所帮助