![](/img/trans.png)
[英]How to make view on top of another view in CoordinatorLayout?
[英]How to make a view move as part of RecyclerView using CoordinatorLayout
我正在學習 Android CoordinatorLayout/RecyclerView/Behavior/Nested scroll 的東西。 我發現 API 不是很簡單。 因此我做了一個小實驗,但發現 CoordinatorLayout 中的視圖行為並沒有像預期的那樣出現。
這是我想要做的:我想要一個 CoordinatorLayout 作為父容器。 在它里面,有一個 RecyclerView 作為它的孩子。 同樣作為它的孩子,還有另一個紅色的 View。 我希望紅色視圖隨着 RecyclerView 滾動而移動。 我希望兩個視圖的移動如此同步,以至於紅色視圖似乎是 RecyclerView 的一部分。
注意:我知道要實現我剛才所說的,還有更簡單的方法。 但我只想用 CoordinatorLayout 和 Behavior 類來做,這樣我就可以了解它們是如何工作的。
這是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="io.github.seemuch.coordinatorlayoutexperiment.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:dividerHeight="10dp"
/>
<View
android:id="@+id/red_view"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#ff0000"
app:layout_scrollFlags="scroll|enterAlways"
/>
</android.support.design.widget.CoordinatorLayout>
這是我的行為類:
package io.github.seemuch.coordinatorlayoutexperiment;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class MyBehavior extends CoordinatorLayout.Behavior<View> {
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View toolbar, View dependency) {
return dependency instanceof RecyclerView;
}
@Override
public boolean onStartNestedScroll (CoordinatorLayout coordinatorLayout,
View child,
View directTargetChild,
View target,
int nestedScrollAxes) {
int vertical = (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL);
int horizontal = (nestedScrollAxes & ViewCompat.SCROLL_AXIS_HORIZONTAL);
return (vertical != 0 && horizontal == 0);
}
@Override
public void onNestedPreScroll (CoordinatorLayout coordinatorLayout,
View child,
View target,
int dx,
int dy,
int[] consumed) {
float currY = child.getY();
if (currY <= 0 && dy >= 0) {
return;
}
child.setY(currY - dy);
}
}
正如您在 onNestedPreScroll 方法中看到的那樣,我希望子節點(即 red_view)移動到它所依賴的視圖(即 RecyclerView)盡可能多的位置。
實際發生的事情是這樣的:如果你滾動 RecyclerView 的動作非常大,紅色的視圖會和它一起移動,正如預期的那樣; 但是如果你移動得很少,red_view 的移動速度會比 RecyclerView 快得多。
這是屏幕錄制視頻的鏈接: https : //youtu.be/zK4g61F2aa0這里是該項目的 Github 存儲庫,如果您想自己下載並運行它: https : //github.com/seemuch/協調器布局實驗
那么,有人知道發生了什么嗎? 謝謝!
找到了一個解決方案:不要覆蓋 onNestedPreScroll,而是覆蓋 onNestedScroll()。 就這么簡單。 但是,它不處理投擲。 Flings 需要單獨處理。
我已經解決了這個問題,做了一些非常簡單的事情。 首先,我定義了RecyclerView
不可滾動(我們仍然可以觸摸每一行),然后在布局中放置一個ScrollView
。
這里有一些代碼:
布局文件
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="409dp"
android:layout_height="729dp"
android:layout_marginTop="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.505" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.089" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
主活動.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val adapter = PersonAdapter { /*handle touch*/ }
val manager = CustomGridLayoutManager(this)
manager.setScrollEnabled(false)
recycler.layoutManager = manager
recycler.adapter = adapter
}
}
自定義網格布局管理器.kt
class CustomGridLayoutManager(context: Context?) :
LinearLayoutManager(context) {
private var isScrollEnabled = true
fun setScrollEnabled(flag: Boolean) {
isScrollEnabled = flag
}
override fun canScrollVertically(): Boolean {
return isScrollEnabled && super.canScrollVertically()
}
}
PersonAdapter.kt
class PersonAdapter(val onItemClick : (Int) -> (Unit))
: RecyclerView.Adapter<PersonAdapter.ViewHolder>() {
private val personList = arrayOf("marco", "anna", "gianna", "giulia", "alice", "sini", "marco", "anna", "gianna",
"giulia", "alice", "sini", "marco", "anna", "gianna", "giulia", "alice", "sini", "marco", "anna", "gianna", "giulia", "alice", "sini")
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.row_layout, parent, false))
}
override fun getItemCount(): Int {
return personList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val element = personList[position]
holder.text.text = element
holder.text.setOnClickListener {
onItemClick(position)
}
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val text = itemView.findViewById<TextView>(R.id.textRow)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.