kotlin-如何在片段中使用该列表视图来通过单击替换片段

时间:2018-04-09 16:55:22

标签: android kotlin

在我使用意图移动和传递数据的活动中。我如何在两个片段之间做同样的事情?

MainFragment:

class MainMarket: Fragment(){
  var adapter : MainAdapter?=null
  var listOfMkabala = ArrayList<MainMarketTickClass>()
  companion object {
   fun newInstance():Fragment{
       var fb : MainMarket = MainMarket()
       return fb
   }
  }


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

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

    adapter = MainAdapter (this.context!!,listOfMkabala)
    list.adapter = adapter
  }
}

我的适配器:

class MainAdapter: BaseAdapter {
  var  listOfData= ArrayList<MainMarketTickClass>()
  var context:Context?=null
  constructor(context:Context, listOfMkabaln: ArrayList<MainMarketTickClass>):super(){
      this.listOfData=listOfMkabaln
      this.context=context
  }

  override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
    val mo3d =  listOfData[p0]

    var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    var myView = inflator.inflate(R.layout.maintick, null)
    myView.textView.text = mo3d.title!!

    return myView
  }

  override fun getItem(p0: Int): Any {
    return listOfData[p0]
  }

  override fun getItemId(p0: Int): Long {
    return p0.toLong()
  }

  override fun getCount(): Int {
    return  listOfData.size
  }
}

1 个答案:

答案 0 :(得分:0)

在这里,试试这个

Bundle bundle = new Bundle();
bundle.putString("id", "0");
fragment.setArguments(bundle);
...