kotlin-cant从RecyclerView适配器替换Fragment

时间:2018-04-12 17:20:14

标签: android android-fragments kotlin

点击后,我终止了应用程序 而且我不知道在哪里必须输入适配器或中的点击顺序 RecyclerView的片段 Mainadapter.kt

class ContentAdapter(
    private val activity: MainActivity, 
    private var listOfData: ArrayList<MainMarketTickClass>
) : RecyclerView.Adapter<ContentAdapter.ViewHolder>(), View.OnClickListener {
    override fun onClick(v: View?) {
        val manager = activity.supportFragmentManager
        val transaction = manager.beginTransaction()
        val trs = AddCar()
        transaction.add(android.R.id.content, trs, "IndividualBook")
        transaction.addToBackStack(null)
        transaction.commit()
        var heroActivity: MainActivity = activity

    }

    override fun getItemCount(): Int = listOfData.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var inf = ViewHolder(LayoutInflater.from(parent!!.context).inflate(R.layout.maintick, parent, false))
        return inf

    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder!!.bind(listOfData)
        holder.itemView.MainImage.setOnClickListener(this)
    }

    class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        fun bind(listOfData: ArrayList<MainMarketTickClass>) {
            val dataListin2 = listOfData[adapterPosition]
            itemView.textView.text = dataListin2.title
        }
    }

RecyclerView片段

class MainMarket : Fragment() {
    var ITEMSList = ArrayList<MainMarketTickClass>()

    companion object {
        fun newInstance(): Fragment {
            var fb: MainMarket = MainMarket()
            return fb
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var inf = inflater!!.inflate(R.layout.main_marker, container, false)
        return inf
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        ITEMSList.add(MainMarketTickClass("123"))
        ITEMSList.add(MainMarketTickClass(" 123"))
        ITEMSList.add(MainMarketTickClass("123"))

        var adapter = ContentAdapter(MainActivity(), ITEMSList)
        list.adapter = adapter
        list.layoutManager = LinearLayoutManager(this.context, LinearLayoutManager.VERTICAL, false)
    }
}

我在OnClick fun FATAL EXCEPTION中有一些错误

enter image description here

1 个答案:

答案 0 :(得分:0)

我建议您使用与在适配器中使用单击侦听器事务不同的方法。

这是Adapter

class ContentAdapter constructor(private val activity: MainActivity, private var listOfData: ArrayList<MainMarketTickClass>, val listener: ContentListener) : RecyclerView.Adapter<ContentAdapter.ViewHolder>() {

    override fun getItemCount(): Int = listOfData.size
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var inf = ViewHolder(LayoutInflater.from(parent!!.context).inflate(R.layout.maintick, parent, false))
        return inf

    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(listOfData, listener)
    }

    class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {

        fun bind(listOfData: ArrayList<MainMarketTickClass>, listener: ContentListener) {
            val dataListin2 = listOfData[adapterPosition]

            itemView.textView.text = dataListin2.title

            // this is the click listener. It calls the onItemClicked interface method implemented in the Activity
            itemView.setOnClickListener {
                listener.onItemClicked(listOfData.get(adapterPosition))
            }
        }
    }

    public interface ContentListener {
        fun onItemClicked(item: MainMarketTickClass)
    }

}

这是MainActivity

class MainMarket: Fragment(), ContentAdapter.ContentListener {

var ITEMSList = ArrayList<MainMarketTickClass>()

companion object {
   fun newInstance():Fragment{

       var fb : MainMarket = MainMarket()
       return fb
   }
}


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    var inf = inflater!!.inflate(R.layout.main_marker,container,false)
    return inf
}

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

    ITEMSList .add ( MainMarketTickClass("123"))
    ITEMSList .add ( MainMarketTickClass(" 123"))
    ITEMSList .add ( MainMarketTickClass("123"))

  var adapter = ContentAdapter (MainActivity(),ITEMSList)
    list.adapter = adapter
    list.layoutManager = LinearLayoutManager(this.context,LinearLayoutManager.VERTICAL,false)

}

override onItemClicked(item: MainMarketTickClass) {
// this method gets executed everytime an item gets clicked. You should 
// delegate the fragment transactions to the activity that hosts the fragment. 
// You should not do a fragment transaction from inside the fragment.
// You can use a similar interface approach to contact the activity from the Fragment.

}