繁体   English   中英

如何使用 kotlin 从另一个 RecyclerView 更新 RecyclerView

[英]How to update a RecyclerView from another RecyclerView with kotlin

在我的项目中,我有一个承载两个 RecylerView 的片段。 我想在第一个 RecyclerView 上单击一个项目时更新第二个 RecyclerView。 当我单击 CalendarAdapter 中的项目时,我希望 BookingAdapter 更新其值。 (BookinAdapter 当前为空)

首页片段

class HomeFragment : Fragment() {

    private lateinit var homeViewModel: HomeViewModel

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_home, container, false)
        return root
    }

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

        calendarRecycleView.layoutManager = LinearLayoutManager(view.context, LinearLayoutManager.HORIZONTAL, false)
        calendarRecycleView.adapter = CalendarAdapter()


        bookingsRecycleView.layoutManager = LinearLayoutManager(view.context, LinearLayoutManager.VERTICAL, false)
        bookingsRecycleView.adapter = BookingsAdapter()


    }
}

日历适配器

class CalendarAdapter : RecyclerView.Adapter<CalendarViewHolder>() {

    var selectedPosition = -1

    val daysList = listOf("10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28")

    override fun getItemCount(): Int {
        return daysList.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CalendarViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val cellForRow = layoutInflater.inflate(R.layout.row_calendar, parent, false)

        return CalendarViewHolder(cellForRow)
    }

    override fun onBindViewHolder(holder: CalendarViewHolder, position: Int) {
        holder.itemView.dayLabel.text = daysList.get(position)
        holder.itemView.monthLabel.text = "DEC"

        if(selectedPosition == position)
            holder.itemView.cardView.setBackgroundColor(Color.parseColor("#008577"));
        else
            holder.itemView.cardView.setBackgroundColor(Color.parseColor("#ffffff"));

        holder.itemView.setOnClickListener {
            selectedPosition = position
            notifyDataSetChanged()
        }
    }

}

class CalendarViewHolder(v: View): RecyclerView.ViewHolder(v) {

    init {
        v.setOnClickListener {
        }
    }
}

预订适配器

class BookingsAdapter : RecyclerView.Adapter<BookingsViewHolder>() {

    override fun getItemCount(): Int {
        return 3
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookingsViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val cellForRow = layoutInflater.inflate(R.layout.row_bookings, parent, false)

        return BookingsViewHolder(cellForRow)
    }

    override fun onBindViewHolder(holder: BookingsViewHolder, position: Int) {
        holder.itemView.hello.text = "Hello"

    }

    fun update() {
        this.notifyDataSetChanged()
        Log.d("Log", "ok")
    }

}

class BookingsViewHolder(v: View): RecyclerView.ViewHolder(v) {

    init {
        v.setOnClickListener {
        }
    }
}

您可以将侦听器传递给CalendarAdapter并侦听Fragment的点击。 像这样的东西:

 class CalendarAdapter(private val listener: CalendarItemListener): RecyclerView.Adapter<CalendarViewHolder>() {

    var selectedPosition = -1

    val daysList = listOf("10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28")

    override fun getItemCount(): Int {
    return daysList.size
}

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CalendarViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val cellForRow = layoutInflater.inflate(R.layout.row_calendar, parent, false)

        return CalendarViewHolder(cellForRow)
    }

    override fun onBindViewHolder(holder: CalendarViewHolder, position: Int) {
        holder.itemView.dayLabel.text = daysList.get(position)
        holder.itemView.monthLabel.text = "DEC"

        if (selectedPosition == position)
            holder.itemView.cardView.setBackgroundColor(Color.parseColor("#008577"));
        else
            holder.itemView.cardView.setBackgroundColor(Color.parseColor("#ffffff"));

        holder.itemView.setOnClickListener {
            listener.onItemClicked(daysList.get(position), "DEC")
            selectedPosition = position
            notifyDataSetChanged()
        }
    }

}

interface CalendarItemListener {
    fun onItemClicked(day: String, month: String)
}

class CalendarViewHolder(v: View) : RecyclerView.ViewHolder(v) {

    init {
        v.setOnClickListener {
        }
    }
}

在你的片段中实现点击监听器接口并传入CalendarAdapter

class HomeFragment : Fragment(), CalendarItemListener {

    private lateinit var homeViewModel: HomeViewModel

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_home, container, false)
        return root
    }

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

        calendarRecycleView.layoutManager = LinearLayoutManager(view.context, LinearLayoutManager.HORIZONTAL, false)
        calendarRecycleView.adapter = CalendarAdapter(this)


        bookingsRecycleView.layoutManager = LinearLayoutManager(view.context, LinearLayoutManager.VERTICAL, false)
        bookingsRecycleView.adapter = BookingsAdapter()


    }

    override onItemClicked(day: String, month: String){
        //update your second adapter
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM