【问题标题】:How to Read Data from RecyclerView and send in BottomSheet如何从 RecyclerView 读取数据并发送到 BottomSheet
【发布时间】:2020-07-28 06:41:23
【问题描述】:

我正在使用 recyclerView 来显示设备中安装的应用列表

图片 - Link

关于更多细节,我在 LongPress 上使用 bottomSheet,即在 ViewHolder 类中,但 如何将所选选项卡的数据发送到 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
}

数据类

    data class AppData(
    val icon: Drawable,
    val name: String,
    val size: String,
)

【问题讨论】:

    标签: android kotlin android-recyclerview android-adapter bottom-sheet


    【解决方案1】:

    使用您当前的代码,最简单的解决方案是:

    1. 修改您的 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
      

      }

    2. 在 ViewHolder 类中添加 onBind 方法:

       fun onBind(appData: AppData) {
           icon.setImageDrawable(currentItem.icon)
           name.text = currentItem.name
           size.text = currentItem.size
       }
      
    3. 修改 Adapter 中的 onBindViewHolder 方法以调用该 onBind 方法:

      override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.onBind(listOfApps[位置]) }

    4. 在您的 ViewHolder 中添加 lateinit var currentItem: AppData,它将在 onBind 中设置,我们可以在 onLongClick 中使用它:

      类 ViewHolder(appView: View) : RecyclerView.ViewHolder(appView), View.OnClickListener, View.OnLongClickListener { . . . 覆盖 fun 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
       }
      

    【讨论】:

      【解决方案2】:

      将您的currentItem 列表项附加到ViewHolder 内部onBindViewHolder 和内部onLongClick 将此数据传递给新创建的BottomSheetDialog,它正在扩展Fragment(因此您可以使用Bundle - @987654328 @ 方法)。然后在onCreateView 中将您的数据设置为Views

      【讨论】:

        【解决方案3】:
        1. 创建接口

           interface OnListItemClicked {
          
              fun onClicked(data : AppData)
          
            }
          
        2. 在 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
          

          }

        3. 从主活动向适配器传递额外的回调。

          Adapter(private val listOfApps: List&lt;AppData&gt;, val onClick: (AppData) -&gt; Unit)

        使用listOfAppData[position]从适配器调用点击监听器

        1. 在 MainActivity 上将一个函数传递给适配器初始化。

          fun passDataToBottomSheet(data: AppData) {
          
            val listener = OnListItemClicked()
          
            listener.onClicked(data)
          
           }
          

          Adapter(list, ::passDataToBottomSheet)

        就是这样。它现在应该可以工作了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-10
          • 2011-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多