gpt4 book ai didi

android - 如何在 Android Compose 中动态更新 AndroidView 内的 RecyclerView

转载 作者:行者123 更新时间:2023-12-05 00:15:18 25 4
gpt4 key购买 nike

我希望 RecyclerView 在列表更改时动态更改。

View 根据 AndroidView.update 表达式进行重构。然而我尝试过的都不起作用。我不熟悉 RecyclerViews,我认为这就是问题所在。

有什么想法吗?

 AndroidView(
factory = {

// Logic for inflating a RecyclerView in a normal Android View System
val view = LayoutInflater.from(it)
.inflate(R.layout.edit_list_xml_screen_layout, null, false)

val recyclerView = view.findViewById<RecyclerView>(R.id.taskListRV)
recyclerView.layoutManager = LinearLayoutManager(it)

val adapter = TaskRecyclerViewAdaptor(it, taskList)
// adapter.setClickListener()
recyclerView.adapter = adapter
recyclerView.addItemDecoration(
DividerItemDecoration(
it,
DividerItemDecoration.VERTICAL
)
)

recyclerView // Return recyclerView
},
update = {
/* TODO dynamically update recycler view */
}

如果有人能告诉我要添加到 adapter.setClickListener() 中的参数,我会加分

最佳答案

在我们的例子中:

  1. 我们在Fragment中使用了ViewBinding,因此我们需要先删除父组(本部分为cmiiw),我们使用这个ViewBindingDelegate here
  2. 点击监听器也作为参数传递,请参阅onItemClick
  3. 在我们的例子中,recyclerview 设置是在 OurFragment 中完成的,而不是在 Compose 屏幕中完成的,因此 compose 屏幕仅接收 recyclerView: (Context) -> View lambda膨胀 Compose 屏幕内的 View OurComposeScreen.kt

我们的 fragment

// OurFragment.kt

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initBanner()
}

private fun initBanner() {
binding.banner.setContent {
BannerListScreen(
recyclerView = { context ->
recyclerViewSetup(
context = context,
onItemClick = ::recyclerViewClick,
)
},
)
}
}

// ...

private fun recyclerViewSetup(
context: Context,
// modify this to your usecase, as we use RecyclerView.OnItemTouchListener, but this is also possible to use a simple lambda () -> Unit
onItemClick: (Context, MyAdapter) -> RecyclerView.OnItemTouchListener,
): RecyclerView = with(LayoutInflater.from(context).inflate(R.layout.your_custom_xml, null, false)) {
val recyclerView = RecyclerView(context).apply {
if (parent != null) (parent as ViewGroup).removeView(this) // we need to remove the parent group as it'll inflate the view twice if added in compose
layoutManager = LinearLayoutManager(context).apply {
orientation = LinearLayoutManager.HORIZONTAL
}

adapter = MyAdapter(...)
adapter.notifyDataSetChanged()
addOnItemTouchListener(onItemClick(context, adapter))
}

recyclerView
}


private fun recyclerViewClick(
context: Context,
adapter: MyAdapter,
) = RecyclerItemClickListener(
context,
RecyclerItemClickListener.OnItemClickListener { view, position ->
val b = adapter.getItem(position) // todo something with the click listener
}
)

我们的撰写屏幕:

// OurComposeScreen.kt
@Composable
internal fun BannerReminder(
// .....
recyclerView: (Context) -> View,
) {
Column(
// omitted....
) {
AndroidView(
modifier = /* ... */,
factory = { context -> recyclerView(context) }
)
}
}

关于android - 如何在 Android Compose 中动态更新 AndroidView 内的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67727174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com