RecyclerView单击项以重新充气另一个RecyclerView

时间:2018-07-23 17:31:02

标签: android android-recyclerview kotlin

我有一个名为“ ExpView”的活动,其中有两个RecyclerViews。在一个带有Adapter类“ ExpenseViewAdapter”的recyclerView中,我有两个ViewHolder:一个加载所有费用数据,另一个ViewHolder出现在第一个ViewHolder的末尾以显示总费用金额。

在带有自定义适配器“ DateViewAdapter”的第二个recyclerView中,我加载旅行日期以及水平显示在顶部的日期。

两个Recyclerviews均在ViewExp活动开始时加载。现在,我想要的是,当用户单击DateViewAdapter类中的日期/日期时,另一个recyclerview将根据所选日期加载费用数据。

我正在使用“房间持久性”数据库来存储和获取费用信息。所有这些都发生在ViewExp活动中。

现在,请向我展示一种将代码合并到DateViewAdapter类中的方法,以便当用户单击日期/日期时,它将位置数据发送到ViewExp类并调用“ db.userDao()。findByDate(startDate)” ”方法,并将其传递给“ travelViewRV.adapter = ExpenseViewAdapter(myExp,total)”方法。

以下是相关类别中的代码:

ViewExp类

package ksk.com.ksktravelexp

import android.arch.persistence.room.Room
import android.arch.persistence.room.migration.Migration
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.expitemlayout.*
import kotlinx.android.synthetic.main.view_exp.*
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.*

class ViewExp : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.view_exp)

        val db = Room.databaseBuilder(applicationContext, AppDb::class.java, "expDB")
                .allowMainThreadQueries().build()


        //Delete All Items in DB
//        db.userDao().deleteAll()

        val startDate = "2018-07-21"
        val tripDates = calculateTripDates(7,startDate)

        val myExp = db.userDao().findByDate(startDate)
        val total = calculateTotalExp(myExp)

        travelViewRV.layoutManager = LinearLayoutManager(applicationContext)!!
        travelViewRV.adapter = ExpenseViewAdapter(myExp, total)

        dayDateRV.layoutManager = LinearLayoutManager(applicationContext,LinearLayoutManager.HORIZONTAL,false)
        dayDateRV.adapter = DateViewAdapter(tripDates)



    }

    private fun calculateTotalExp(expList: MutableList<Expense>): Int {
        var total: Int = 0
        for (i in 0 until expList.size) {
            total += expList[i].amount
        }
        return total
    }

    fun calculateTripDates(numOfDays:Int, startDate:String):MutableList<String>{
        val dayOne = LocalDate.parse(startDate,DateTimeFormatter.ofPattern("yyyy-MM-dd"))
        val tripDates = mutableListOf<String>()

        for (i in 0 until numOfDays){
        val nextDate = dayOne.plusDays(i.toLong())
            tripDates.add(nextDate.format(DateTimeFormatter.ofPattern("MMM dd")))

    }
    return tripDates
    }

}

ExpenseViewAdapter类

package ksk.com.ksktravelexp

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.expitemlayout.view.*
import kotlinx.android.synthetic.main.total_layout.view.*

class ExpenseViewAdapter(val expList: MutableList<Expense>, val totalAmount: Int) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == R.layout.expitemlayout) {
            val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.expitemlayout, parent, false)
            return ExpenseViewAdapter.ExpenseVH(view)
        } else {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.total_layout, parent, false)
            return ExpenseViewAdapter.TotalAmountVH(view)

        }
    }

    override fun getItemViewType(position: Int): Int {
        if (position == expList.size) {
            return R.layout.total_layout

        } else return R.layout.expitemlayout
    }

    override fun getItemCount(): Int = expList.size + 1

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        if (holder is ExpenseVH) {

            holder.expense.text = expList[position].expense
            holder.amount.text = expList[position].amount.toString()
            holder.category.text = expList[position].category

        } else if (holder is TotalAmountVH) {
//            holder.total.text = holder.total.text.toString() + totalAmount.toString() + " BDT"
            holder.total.text = holder.total.text.toString() + totalAmount.toString() + " BDT"

        }
    }

    class ExpenseVH(view: View) : RecyclerView.ViewHolder(view) {

        val expense = view.expenseTV
        val amount = view.amountTV
        val category = view.categoryTV
    }

    class TotalAmountVH(view: View) : RecyclerView.ViewHolder(view) {
        val total = view.totalTV
    }


}

DateViewAdapter

package ksk.com.ksktravelexp

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import kotlinx.android.synthetic.main.date_layout.view.*

class DateViewAdapter(val tripDates:MutableList<String>):RecyclerView.Adapter<DateViewAdapter.ViewHolder>(){
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DateViewAdapter.ViewHolder{
        val view = LayoutInflater.from(parent.context).inflate(R.layout.date_layout,parent,false)
        return DateViewAdapter.ViewHolder(view)
    }

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

    override fun onBindViewHolder(holder:DateViewAdapter.ViewHolder, position: Int) {
        holder.day.text = (position+1).toString()
        holder.date.text = tripDates[position]


    }

    class ViewHolder(view: View): RecyclerView.ViewHolder(view){
        val day= view.dayTV
        val date = view.dateTV
        val dateLayout = view.dateLinearLayout

    }
}

0 个答案:

没有答案