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

当前,我正在完成项目的最后一个细节,我试图将某个部分保留在嵌套的滚动视图中,以保持在屏幕顶部,并且只允许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>

这是我的kotlin


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,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和约束进行一些重组,但到目前为止没有成功。 我也无法在互联网上找到任何人想要与我相同的东西...

q7967139 回答:禁用片段的某些部分进行滚动,即使它位于嵌套的滚动视图中

根据您的问题描述,我假设您遇到一个问题,即打开屏幕时,回收站视图会自动滚动或自动聚焦,并且需要将布局停留在顶部,因此将此字段传递给父布局。

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">

希望对您有帮助。

本文链接:https://www.f2er.com/3030009.html

大家都在问