我对 Android 应用程序开发有点陌生。好吧,我正在玩 RecyclerView。我有一个带有模式布局的父 recyclerview。现在模态布局有一个recyclerview(子recyclerview)。我设法创建了适配器并滚动了主要的 recyclerview 列表。不幸的是,我找不到滚动子 recyclerview 的方法。这是我正在玩的代码:
我已经尝试在父recyclerview适配器的onBindViewHolder方法中设置子recyclerview的适配器。
此外,我尝试为子 recyclerView 设置属性 nestedScrollingEnabled=true、descendantFocusability=blocksDescendants 和 focusableInTouchMode=true。
这是我的activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
父recyclerview模型(model.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Testing" />
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_marginVertical="8dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/modalRecyclerView"
android:layout_width="wrap_content"
android:layout_height="100dp" />
</LinearLayout>
子recyclerview模型(child_modal.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testing"/>
</LinearLayout>
在 MainActivity 中:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView.apply {
layoutManager = LinearLayoutManager(this@MainActivity, RecyclerView.VERTICAL, false)
adapter = ModalAdapter()
}
}
模态适配器:
class ModalAdapter : RecyclerView.Adapter<ModalAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context).inflate(R.layout.model, parent, false)
return ViewHolder(inflater)
}
override fun getItemCount(): Int {
return 5
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.view.modalRecyclerView.adapter = ChildModalAdapter()
holder.view.modalRecyclerView.layoutManager = LinearLayoutManager(holder.view.context, RecyclerView.VERTICAL, false)
}
class ViewHolder(val view: View): RecyclerView.ViewHolder(view)
}
子模态适配器:
class ChildModalAdapter : RecyclerView.Adapter<ChildModalAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context).inflate(R.layout.child_modal, parent, false)
return ViewHolder(inflater)
}
override fun getItemCount(): Int {
return 10
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
}
class ViewHolder(val view: View): RecyclerView.ViewHolder(view)
}
内部 recyclerview 不滚动,而父 recyclerview 滚动正常。我正在尝试找到一种方法使内部 recyclerview 与父 recyclerview 一起滚动(我希望两个 recyclerview 都滚动)。