Android Fragments 未连接适配器;正在跳过布局-fragment/Kotlin中有错误

6rvt4ljy  于 2022-12-29  发布在  Android
关注(0)|答案(1)|浏览(116)

这是MainFragment文件的代码

package hr.ferit.tomislav.lucic5.tl5_projekt

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainFragment : Fragment() {
    private lateinit var transactions : ArrayList<Transaction>
    private lateinit var transactionAdapter: TransactionAdapter
    private lateinit var linearlayoutManager: LinearLayoutManager
    private var recyclerview : RecyclerView? = view?.findViewById(R.id.recyclerview)

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_main, container, false)
        transactions = arrayListOf(
            Transaction("President", 400.00),
            Transaction("Jolly", 300.00)
        )

        linearlayoutManager = LinearLayoutManager(context)
        transactionAdapter = TransactionAdapter(transactions)

        recyclerview?.apply {
            layoutManager = linearlayoutManager
            adapter = transactionAdapter
        }

        return view
    }
}

这是事务适配器文件

package hr.ferit.tomislav.lucic5.tl5_projekt

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView

class TransactionAdapter(val transactions: ArrayList<Transaction>) :
    RecyclerView.Adapter<TransactionAdapter.TransactionHolder>() {

    class TransactionHolder(val view: View) : RecyclerView.ViewHolder(view) {
        val label : TextView = view.findViewById(R.id.label)
        val amount : TextView = view.findViewById(R.id.amount)
    }

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

    override fun onBindViewHolder(holder: TransactionHolder, position: Int) {
        val transaction = transactions[position]
        val context = holder.amount.context

        if(transaction.amount >= 0){
            holder.amount.text = "+ $%.2f".format(transaction.amount)
            holder.amount.setTextColor(ContextCompat.getColor(context, R.color.green))
        }else {
            holder.amount.text = "- $%.2f".format(Math.abs(transaction.amount))
            holder.amount.setTextColor(ContextCompat.getColor(context, R.color.red))
        }
        holder.label.text = transaction.label
    }

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

调试后,事务没有显示,这是一个大学项目,我们必须只使用一个活动,然后片段,当我在活动中使用类似的代码,事务显示,但不是在片段
我尝试对初始化适配器进行一些更改,但没有任何效果

zi8p0yeb

zi8p0yeb1#

我已经通过删除onCreateView中的所有内容并覆盖onViewCreated解决了这个问题:

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

    transactions = arrayListOf(
        Transaction("President", 400.00),
        Transaction("Jolly", 300.00)
    )
    var layoutManager = LinearLayoutManager(context)
    recyclerView = view.findViewById(R.id.recyclerview)
    recyclerView.layoutManager = layoutManager
    adapter = TransactionAdapter(transactions)
    recyclerView.adapter = adapter

}

相关问题