在Kotlin中没有接口将数据从recyclerview发送到片段
Sending data from recyclerview to fragment without intarface in Kotlin
我正在研究 kotlin,并且对 recyclerview 很困惑。
任务很简单:显示来自回收站(在 activity 内)在片段内单击的项目的数据。
模特:
data class MyModel (
val info1:String,
val info2:String,
val info3:String)
recyclerView
是使用我在 Antonio 的书中看到的想法实现的(kotlin for android 开发人员)- 没有使用 intarface:
class RecyclerAdapter(
val myList:List<MyModel>,
val listener:(MyModel)->Unit):RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemViewholder = LayoutInflater.from(parent.context).inflate(R.layout.recycler_item,parent,false)
return MyViewHolder(itemViewholder)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bind(myList[position],listener)
}
override fun getItemCount(): Int {
return myList.count()
}
class MyViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
fun bind(list:MyModel,itemListener: (MyModel) -> Unit) = with(itemView){
recycler_infor1.text = list.info1
recicler_infor2.text = list.info2
setOnClickListener { itemListener(list) }
}
}}
在Activity中:
with(my_recyclerView){
layoutManager = LinearLayoutManager(this@MainActivity,RecyclerView.VERTICAL,false)
setHasFixedSize(true)
adapter = RecyclerAdapter(fakeItens()){
supportFragmentManager.commit {
replace(R.id.fragment_container,FragmentDetail()).addToBackStack(null)
}
}
}
当应用程序为运行时,recyclerView
也会在每个项目中显示第一条和第二条信息。
现在,我需要在具有三个 textView
:
的片段中显示这些信息和第三个信息
android:id="@+id/frag_infor1"
android:id="@+id/frag_infor2"
android:id="@+id/frag_infor3"
我该怎么做?
感谢 null_override,我找到了解决方案:
1 - 制作 MyModel
Parcelable
data class MyModel(val info1:String?,
val info2:String?,
val info3:String?
):Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readString(),
parcel.readString()
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(info1)
parcel.writeString(info2)
parcel.writeString(info3)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<MyModel> {
override fun createFromParcel(parcel: Parcel): MyModel {
return MyModel(parcel)
}
override fun newArray(size: Int): Array<MyModel?> {
return arrayOfNulls(size)
}
}
}
2 - 传递给 bundle
并在两个
中使用 apply
将参数设置为 Fragment
with(my_recyclerView){
layoutManager = LinearLayoutManager(this@MainActivity,RecyclerView.VERTICAL,false)
setHasFixedSize(true)
adapter = RecyclerAdapter(fakeItens()){
val bundle = Bundle().apply { putParcelable("Key", it) }
supportFragmentManager.commit {
replace(R.id.fragment_container,FragmentDetail().apply { arguments = bundle }).addToBackStack(null)
}
}
}
3 - 获取 Fragment
中的数据
lateinit var data:MyModel
override fun onAttach(context: Context) {
super.onAttach(context)
arguments?.getParcelable<MyModel>("Key").let {
if (it != null) {
data = it
}
}
}
4 - 绑定 views
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
frag_infor1.text = data.info1
frag_infor2.text = data.info2
frag_infor3.text = data.info3
}
我正在研究 kotlin,并且对 recyclerview 很困惑。 任务很简单:显示来自回收站(在 activity 内)在片段内单击的项目的数据。
模特:
data class MyModel (
val info1:String,
val info2:String,
val info3:String)
recyclerView
是使用我在 Antonio 的书中看到的想法实现的(kotlin for android 开发人员)- 没有使用 intarface:
class RecyclerAdapter(
val myList:List<MyModel>,
val listener:(MyModel)->Unit):RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemViewholder = LayoutInflater.from(parent.context).inflate(R.layout.recycler_item,parent,false)
return MyViewHolder(itemViewholder)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bind(myList[position],listener)
}
override fun getItemCount(): Int {
return myList.count()
}
class MyViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
fun bind(list:MyModel,itemListener: (MyModel) -> Unit) = with(itemView){
recycler_infor1.text = list.info1
recicler_infor2.text = list.info2
setOnClickListener { itemListener(list) }
}
}}
在Activity中:
with(my_recyclerView){
layoutManager = LinearLayoutManager(this@MainActivity,RecyclerView.VERTICAL,false)
setHasFixedSize(true)
adapter = RecyclerAdapter(fakeItens()){
supportFragmentManager.commit {
replace(R.id.fragment_container,FragmentDetail()).addToBackStack(null)
}
}
}
当应用程序为运行时,recyclerView
也会在每个项目中显示第一条和第二条信息。
现在,我需要在具有三个 textView
:
android:id="@+id/frag_infor1"
android:id="@+id/frag_infor2"
android:id="@+id/frag_infor3"
我该怎么做?
感谢 null_override,我找到了解决方案:
1 - 制作 MyModel
Parcelable
data class MyModel(val info1:String?,
val info2:String?,
val info3:String?
):Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readString(),
parcel.readString()
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(info1)
parcel.writeString(info2)
parcel.writeString(info3)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<MyModel> {
override fun createFromParcel(parcel: Parcel): MyModel {
return MyModel(parcel)
}
override fun newArray(size: Int): Array<MyModel?> {
return arrayOfNulls(size)
}
}
}
2 - 传递给 bundle
并在两个
apply
将参数设置为 Fragment
with(my_recyclerView){
layoutManager = LinearLayoutManager(this@MainActivity,RecyclerView.VERTICAL,false)
setHasFixedSize(true)
adapter = RecyclerAdapter(fakeItens()){
val bundle = Bundle().apply { putParcelable("Key", it) }
supportFragmentManager.commit {
replace(R.id.fragment_container,FragmentDetail().apply { arguments = bundle }).addToBackStack(null)
}
}
}
3 - 获取 Fragment
lateinit var data:MyModel
override fun onAttach(context: Context) {
super.onAttach(context)
arguments?.getParcelable<MyModel>("Key").let {
if (it != null) {
data = it
}
}
}
4 - 绑定 views
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
frag_infor1.text = data.info1
frag_infor2.text = data.info2
frag_infor3.text = data.info3
}