You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView条目点击打开同Fragment并传递唯一ID方案问询

实现RecyclerView条目点击跳转并传递唯一ID

步骤1:给数据类添加唯一ID字段

首先需要给你的crypro_data类新增一个唯一ID字段,用来标识每个条目:

data class crypro_data(
    val id: Int, // 唯一标识ID
    val iconRes: Int,
    val name: String
)

步骤2:初始化列表时传入唯一ID

在Crypto Fragment里初始化数据列表时,给每个条目分配不重复的ID:

cryptolist.add(crypro_data(1, R.drawable.ic_baseline_history_24, "dolar"))
cryptolist.add(crypro_data(2, R.drawable.ic_baseline_history_24, "lari"))
cryptolist.add(crypro_data(3, R.drawable.ic_baseline_history_24, "lira"))
// 后续条目依次递增ID即可

步骤3:给Adapter添加点击回调

修改你的cryptoAdapter,通过高阶函数实现条目点击监听:

class cryptoAdapter(
    private val cryptoList: List<crypro_data>,
    private val onItemClicked: (crypro_data) -> Unit
) : RecyclerView.Adapter<cryptoAdapter.CryptoViewHolder>() {

    inner class CryptoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        init {
            // 给条目视图设置点击事件
            itemView.setOnClickListener {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    onItemClicked(cryptoList[position])
                }
            }
        }

        // 绑定数据的方法(根据你的原有逻辑实现)
        fun bind(data: crypro_data) {
            // 示例:itemView.icon.setImageResource(data.iconRes)
            // itemView.name.text = data.name
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CryptoViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.your_item_layout, parent, false) // 替换为你的条目布局ID
        return CryptoViewHolder(view)
    }

    override fun onBindViewHolder(holder: CryptoViewHolder, position: Int) {
        holder.bind(cryptoList[position])
    }

    override fun getItemCount() = cryptoList.size
}

步骤4:在Fragment中处理点击跳转逻辑

回到Crypto Fragment,初始化Adapter时传入点击回调,处理Fragment跳转并传递ID:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    // 原有RecyclerView初始化代码...
    recyclerView = view.findViewById(R.id.recyclerview)
    recyclerView.setHasFixedSize(true)
    recyclerView.layoutManager = LinearLayoutManager(activity)

    // 初始化列表(带ID)...
    cryptolist = ArrayList()
    cryptolist.add(crypro_data(1, R.drawable.ic_baseline_history_24, "dolar"))
    // 其他条目...

    // 初始化Adapter并设置点击回调
    cryptoAdapter = cryptoAdapter(cryptolist) { selectedItem ->
        // 创建目标Fragment(同一个Crypto Fragment)
        val targetFragment = Crypto()
        // 封装ID到Bundle
        val args = Bundle().apply {
            putInt("SELECTED_CRYPTO_ID", selectedItem.id)
        }
        targetFragment.arguments = args

        // 执行Fragment跳转
        parentFragmentManager.beginTransaction()
            .replace(R.id.fragment_container, targetFragment) // 替换为你的容器布局ID
            .addToBackStack(null) // 可选,添加到回退栈方便返回
            .commit()
    }
    recyclerView.adapter = cryptoAdapter
}

步骤5:在目标Fragment中接收ID

在Crypto Fragment的生命周期方法中获取传递过来的ID,用于后续逻辑处理:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    // 获取传递的ID
    val selectedCryptoId = arguments?.getInt("SELECTED_CRYPTO_ID")
    selectedCryptoId?.let {
        // 这里根据ID执行对应逻辑,比如加载详情数据等
        // 示例:Log.d("Crypto", "当前选中的条目ID: $it")
    }

    // 原有RecyclerView初始化逻辑...
}

内容的提问来源于stack exchange,提问作者PuzzleHead

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 09:00:55