即使片段在嵌套的滚动视图中,也无法对其进行滚动滚动

问题描述 投票:0回答:1

当前,我正在完成项目的最后一个细节,我试图将某个部分保留在嵌套的滚动视图中,以保持在屏幕顶部,并且只允许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, 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和约束进行一些重组,但到目前为止没有成功。我也找不到任何人想要和我一样的东西在互联网上...

android xml kotlin layout android-nestedscrollview
1个回答
0
投票

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

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

希望对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.