【问题标题】:Null bundle in fragment片段中的空包
【发布时间】:2021-07-01 11:31:42
【问题描述】:

我正在编写一个简单的 Kotlin 应用程序,我想将数据从 LessonsModule1Fragment 发送到 DetailsFragment 并尝试传递包变量。根据位置,片段应该使用适当的布局进行膨胀。

我试图在片段之间传递一个位置,但我总是得到 null

LessonsModule1Fragment

class LessonsModule1Fragment : Fragment(), OnLessonClickListener {

private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter: RecyclerView.Adapter<RecyclerViewLessonAdapter.ViewHolder>? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    val binding = inflater.inflate(R.layout.fragment_module1_lessons, container, false)

    val recyclerView = binding.findViewById<RecyclerView>(R.id.lesson_recycler_view)
    layoutManager = LinearLayoutManager(context)
    recyclerView.layoutManager = layoutManager
    adapter = RecyclerViewLessonAdapter(this)
    recyclerView.adapter = adapter

    return binding
}

override fun lessonClick(lesson: Lesson, position: Int) {
    var bundle = Bundle()
    bundle.putInt("POSITION", position)

    var transaction = this.parentFragmentManager.beginTransaction()
    var fragment = DetailsFragment()
    fragment.arguments = bundle
    transaction.replace(R.id.lesson_recycle_layout, fragment).commit()

    findNavController().navigate(R.id.action_lessonsModule1Fragment_to_detailsFragment)
}

DetailsFragment

class DetailsFragment : Fragment()  {

private var num: Int? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

    num = arguments?.getInt("POSITION")

   when (num) {
       0 -> return inflater.inflate(R.layout.fragment_details, container, false)
       1 -> return inflater.inflate(R.layout.fragment_details2, container, false)
       2 -> return inflater.inflate(R.layout.fragment_details3, container, false)
   }
        return inflater.inflate(R.layout.fragment_details, container, false)
}}

RecyclerViewLessonAdapter

class RecyclerViewLessonAdapter (private val lessonClickListener: OnLessonClickListener) : RecyclerView.Adapter<RecyclerViewLessonAdapter.ViewHolder>() {

private val lessons = DataStorage.getLessonsList()

inner class ViewHolder(itemView: View) :  RecyclerView.ViewHolder(itemView) {
    val itemTitle: TextView = itemView.findViewById(R.id.lesson_title)
    val itemDetails: TextView = itemView.findViewById(R.id.lesson_description)
    val itemImage: ImageView = itemView.findViewById(R.id.lesson_image)

    fun lessonBind(lesson: Lesson, clickListener: OnLessonClickListener) {
        itemView.setOnClickListener {
            clickListener.lessonClick(lesson, adapterPosition)
        }
    }

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.lesson_card, parent, false)
    return ViewHolder(v)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.itemImage.setImageResource(lessons[position].images)
    holder.itemTitle.text = lessons[position].title
    holder.itemDetails.text = lessons[position].details

    val lesson = lessons[position]
    holder.lessonBind(lesson, lessonClickListener)
}

override fun getItemCount() = lessons.size
}

OnLessonClickListener

interface OnLessonClickListener {
fun lessonClick(lesson: Lesson, position: Int)
}

或许有更合适的方式?

【问题讨论】:

  • 您似乎在这里导航两次,一次是手动使用事务,然后是使用导航组件?导航组件取代了管理您自己的事务的需要。如果您想使用它,请摆脱重复的事务并使用导航控制器传递参数。
  • 哦,谢谢!我用几行解决了这个问题

标签: android android-studio kotlin android-fragments


【解决方案1】:

我使用了导航参数,添加了这些行并且它起作用了。谢谢提示

        val action = LessonsModule1FragmentDirections.actionLessonsModule1FragmentToDetailsFragment(position)
    findNavController().navigate(action)

【讨论】:

    猜你喜欢
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2014-10-13
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多