android - 禁用片段的某些部分进行滚动,即使它位于嵌套的滚动视图中
问题描述
目前我正在完成我项目的最后一个细节,我试图将某个部分保留在嵌套的滚动视图中以保持在屏幕顶部,并且只让 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="➜"
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、滚动视图和约束进行一些重组,但到目前为止还没有成功。我在互联网上也找不到任何人想要和我一样的东西......
解决方案
根据您的问题描述,我假设您在打开屏幕时遇到回收站视图滚动或自动聚焦的问题,并且您需要将布局粘贴在顶部,因此将此字段传递给您的父布局。
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">
我希望它对你有帮助。
推荐阅读
- amazon-web-services - AWS Elastic Beanstalk 中的 URL 重写或重定向(使用 Spring Boot 应用程序)
- java - 与xml spring配置相关
- performance - 为什么我的像素处理功能这么慢?
- python - 使用 zip() 迭代两个可迭代对象时如何在两个可迭代对象中引发 IndexError
- c# - 如何防止 SQLite 中的重复记录或如何检查记录是否已存在
- mysql - SQL - 来自 3 个表的结果,聚合函数
- google-apps-script - 谷歌应用脚本:提交时获取谷歌表单响应的内容
- python - 如何获得 1024 x 1024 样本,而不是 128 x 128?
- windows - 如何使用 PowerShell 脚本在远程计算机上运行另一个 PowerShell 脚本
- javascript - 从javascript没有jQuery选择下拉控件中的项目