如何从 RecyclerView 读取数据并发送到 BottomSheet
How to Read Data from RecyclerView and send in BottomSheet
我正在使用 recyclerView 显示设备中安装的应用列表
图片 - Link
有关更多详细信息,我在 LongPress 上使用 bottomSheet,即在 ViewHolder Class 中,但是 如何将所选选项卡的数据发送到 bottomSheet 更多详细信息(例如包名称,API 级别等)...参考图片
我要 - Link
我从下面的编码中得到 - Link
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView.adapter = Adapter // I set adapter here with function getApps()
recyclerView.layoutManager = LinearLayoutManager(this)
private fun getApps(): List<DataClass> {
// here I get apps icon,name,size and return list<DataClass>
return list
}
Adapter.kt
class Adapter(private val listOfApps: List<AppData>) :
RecyclerView.Adapter<Adapter.ViewHolder>() {
class ViewHolder(appView: View) : RecyclerView.ViewHolder(appView), View.OnClickListener,
View.OnLongClickListener {
init {
appView.setOnClickListener(this)
appView.setOnLongClickListener(this)
}
val icon: ImageView = appView.App_icon
val name: TextView = appView.App_name
val size: TextView = appView.App_size
override fun onClick(v: View?) {
Toast.makeText(v?.context, "OnClick", Toast.LENGTH_SHORT).show()
}
override fun onLongClick(v: View?): Boolean {
// I want here on Long press BottomSheet appears with details
val bottomSheetDialog = BottomSheetDialog()
// Show bottomSheet on LongPress
bottomSheetDialog.show(
(v?.context as FragmentActivity).supportFragmentManager, bottomSheetDialog.tag
)
return true
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(
R.layout.list_apps, parent, false
)
return ViewHolder(view)
}
override fun getItemCount() = listOfApps.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val currentItem = listOfApps[position]
holder.icon.setImageDrawable(currentItem.icon)
holder.name.text = currentItem.name
holder.size.text = currentItem.size
}
}
BottomSheetDialog.kt
class BottomSheetDialog: BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.bottom_sheet, container, false)
}
override fun getTheme(): Int = R.style.RoundBottomSheetDialog
}
数据Class
data class AppData(
val icon: Drawable,
val name: String,
val size: String,
)
将您的 currentItem
列表项附加到 onBindViewHolder
内的 ViewHolder
和 onLongClick
内,将此数据传递给新创建的 BottomSheetDialog
,它正在扩展 Fragment
(所以你可以使用 Bundle
- setArgument
方法)。然后在 onCreateView
中将您的数据设置为 View
s
创建界面
interface OnListItemClicked {
fun onClicked(data : AppData)
}
在 BottomSheetDialog 中实现该接口
class BottomSheetDialog: BottomSheetDialogFragment(), OnListItemClicked {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.bottom_sheet, container, false)
}
override fun onClicked(data: AppData) {
//show Details here
}
override fun getTheme(): Int = R.style.RoundBottomSheetDialog
}
将额外的回调从主 activity 传递给适配器。
Adapter(private val listOfApps: List<AppData>, val onClick: (AppData) -> Unit)
使用 listOfAppData[position]
从适配器调用点击监听器
在 MainActivity 上传递一个函数到 Adapter 初始化中。
fun passDataToBottomSheet(data: AppData) {
val listener = OnListItemClicked()
listener.onClicked(data)
}
Adapter(list, ::passDataToBottomSheet)
就是这样。它现在应该可以工作了。
使用您当前的代码,最简单的解决方案是:
修改 BottomSheetDialog 以在构造函数中包含 AppData
class BottomSheetDialog(val appData: AppData): BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.bottom_sheet, container, false)
}
override fun getTheme(): Int = R.style.RoundBottomSheetDialog
}
在您的 ViewHolder 中添加 onBind 方法 class:
fun onBind(appData: AppData) {
icon.setImageDrawable(currentItem.icon)
name.text = currentItem.name
size.text = currentItem.size
}
修改 Adapter 中的 onBindViewHolder 方法以调用该 onBind 方法:
覆盖乐趣 onBindViewHolder(holder: ViewHolder, position: Int) {
holder.onBind(listOfApps[位置])
}
在您的 ViewHolder 中添加将在 onBind 中设置的 lateinit var currentItem: AppData
,我们可以在 onLongClick
:
中使用它
classViewHolder(appView:View):RecyclerView.ViewHolder(appView),View.OnClickListener,
View.OnLongClickListener{
.
.
.
覆盖乐趣 onLongClick(v: View?): Boolean {
// I want here on Long press BottomSheet appears with details
**val bottomSheetDialog = BottomSheetDialog(currentItem)**
// Show bottomSheet on LongPress
bottomSheetDialog.show(
(v?.context as FragmentActivity).supportFragmentManager, bottomSheetDialog.tag
)
return true
}
**private lateinit var currentItem: AppData**
fun onBind(appData: AppData) {
**currentItem = appData**
icon.setImageDrawable(currentItem.icon)
name.text = currentItem.name
size.text = currentItem.size
}
我正在使用 recyclerView 显示设备中安装的应用列表
图片 - Link
有关更多详细信息,我在 LongPress 上使用 bottomSheet,即在 ViewHolder Class 中,但是 如何将所选选项卡的数据发送到 bottomSheet 更多详细信息(例如包名称,API 级别等)...参考图片
我要 - Link
我从下面的编码中得到 - Link
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView.adapter = Adapter // I set adapter here with function getApps()
recyclerView.layoutManager = LinearLayoutManager(this)
private fun getApps(): List<DataClass> {
// here I get apps icon,name,size and return list<DataClass>
return list
}
Adapter.kt
class Adapter(private val listOfApps: List<AppData>) :
RecyclerView.Adapter<Adapter.ViewHolder>() {
class ViewHolder(appView: View) : RecyclerView.ViewHolder(appView), View.OnClickListener,
View.OnLongClickListener {
init {
appView.setOnClickListener(this)
appView.setOnLongClickListener(this)
}
val icon: ImageView = appView.App_icon
val name: TextView = appView.App_name
val size: TextView = appView.App_size
override fun onClick(v: View?) {
Toast.makeText(v?.context, "OnClick", Toast.LENGTH_SHORT).show()
}
override fun onLongClick(v: View?): Boolean {
// I want here on Long press BottomSheet appears with details
val bottomSheetDialog = BottomSheetDialog()
// Show bottomSheet on LongPress
bottomSheetDialog.show(
(v?.context as FragmentActivity).supportFragmentManager, bottomSheetDialog.tag
)
return true
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(
R.layout.list_apps, parent, false
)
return ViewHolder(view)
}
override fun getItemCount() = listOfApps.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val currentItem = listOfApps[position]
holder.icon.setImageDrawable(currentItem.icon)
holder.name.text = currentItem.name
holder.size.text = currentItem.size
}
}
BottomSheetDialog.kt
class BottomSheetDialog: BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.bottom_sheet, container, false)
}
override fun getTheme(): Int = R.style.RoundBottomSheetDialog
}
数据Class
data class AppData(
val icon: Drawable,
val name: String,
val size: String,
)
将您的 currentItem
列表项附加到 onBindViewHolder
内的 ViewHolder
和 onLongClick
内,将此数据传递给新创建的 BottomSheetDialog
,它正在扩展 Fragment
(所以你可以使用 Bundle
- setArgument
方法)。然后在 onCreateView
中将您的数据设置为 View
s
创建界面
interface OnListItemClicked { fun onClicked(data : AppData) }
在 BottomSheetDialog 中实现该接口
class BottomSheetDialog: BottomSheetDialogFragment(), OnListItemClicked { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.bottom_sheet, container, false) } override fun onClicked(data: AppData) { //show Details here } override fun getTheme(): Int = R.style.RoundBottomSheetDialog
}
将额外的回调从主 activity 传递给适配器。
Adapter(private val listOfApps: List<AppData>, val onClick: (AppData) -> Unit)
使用 listOfAppData[position]
在 MainActivity 上传递一个函数到 Adapter 初始化中。
fun passDataToBottomSheet(data: AppData) { val listener = OnListItemClicked() listener.onClicked(data) }
Adapter(list, ::passDataToBottomSheet)
就是这样。它现在应该可以工作了。
使用您当前的代码,最简单的解决方案是:
修改 BottomSheetDialog 以在构造函数中包含 AppData
class BottomSheetDialog(val appData: AppData): BottomSheetDialogFragment() {
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.bottom_sheet, container, false) } override fun getTheme(): Int = R.style.RoundBottomSheetDialog
}
在您的 ViewHolder 中添加 onBind 方法 class:
fun onBind(appData: AppData) { icon.setImageDrawable(currentItem.icon) name.text = currentItem.name size.text = currentItem.size }
修改 Adapter 中的 onBindViewHolder 方法以调用该 onBind 方法:
覆盖乐趣 onBindViewHolder(holder: ViewHolder, position: Int) { holder.onBind(listOfApps[位置]) }
在您的 ViewHolder 中添加将在 onBind 中设置的
中使用它lateinit var currentItem: AppData
,我们可以在onLongClick
:classViewHolder(appView:View):RecyclerView.ViewHolder(appView),View.OnClickListener, View.OnLongClickListener{ . . . 覆盖乐趣 onLongClick(v: View?): Boolean {
// I want here on Long press BottomSheet appears with details **val bottomSheetDialog = BottomSheetDialog(currentItem)** // Show bottomSheet on LongPress bottomSheetDialog.show( (v?.context as FragmentActivity).supportFragmentManager, bottomSheetDialog.tag ) return true } **private lateinit var currentItem: AppData** fun onBind(appData: AppData) { **currentItem = appData** icon.setImageDrawable(currentItem.icon) name.text = currentItem.name size.text = currentItem.size }