Kotlin Android中ViewHolder中的getActivity()/ context

时间:2017-07-28 07:37:12

标签: android kotlin android-viewholder kotlin-android-extensions

我正在为片段构建ViewHolder和适配器,当我尝试为ViewHolder创建OnClick时,我传递的上下文都没有工作。我可以使用activity getActivity() p0!!.contextitemView.contextpackage com._________.criminalintent import android.os.Bundle import android.support.v4.app.Fragment import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import android.widget.Toast class CrimeListFragment: Fragment() { private var mCrimeRecyclerView: RecyclerView? = null private var mAdapter: CrimeAdapter? = null override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { // fragment_crime_list.xml has a RecyclerView element = crime_recycler_view // inflate the fragment into the activity val view = inflater!!.inflate(R.layout.fragment_crime_list, container, false) // grab the recyclerView and give it a required layoutManager mCrimeRecyclerView = view.findViewById(R.id.crime_recycler_view) mCrimeRecyclerView!!.layoutManager = LinearLayoutManager(activity) updateUI() return view } private fun updateUI() { val crimeLab = CrimeLab.get(activity) val crimes = crimeLab.getCrimes() mAdapter = CrimeAdapter(crimes) // Connect the adapter to the recyclerView mCrimeRecyclerView!!.adapter = mAdapter } /** * in Kotlin, we must give the view passed into the constructor directly * as a substitute for a super() call * * create a ViewHolder that holders the crime list item's view * * super(itemView) = super(inflater!!.inflate(R.layout.list_item_crime, parent, false)) * MUST give it the direct value in Kotlin */ private class CrimeHolder(inflater: LayoutInflater?, parent: ViewGroup): RecyclerView.ViewHolder(inflater!!.inflate(R.layout.list_item_crime, parent, false)), View.OnClickListener { private var mCrime: Crime? = null /** * When given a crime, this CrimeHolder will update the title and date for this Crime */ fun bind(crime: Crime) { mCrime = crime val titleTextView = itemView.findViewById<TextView>(R.id.crime_title) val dateTextView = itemView.findViewById<TextView>(R.id.crime_date) titleTextView.text = mCrime!!.mTitle dateTextView.text = mCrime!!.mDate.toString() } override fun onClick(p0: View?) { Toast.makeText(WHAT_TO_PUT_HERE, "${mCrime!!.mTitle} clicked!", Toast.LENGTH_SHORT / 2) .show() } } private class CrimeAdapter(private var mCrimes: MutableList<Crime>): RecyclerView.Adapter<CrimeHolder>() { /** * - Calls our CrimeHolder to make our custom ViewHolders * - Called by RecyclerView when it needs a new view to display * - Gets the layoutInflater from the ViewGroup and returns a CrimeHolder of it */ override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): CrimeHolder = CrimeHolder(LayoutInflater.from(parent!!.context), parent) /** * Bind the crime (data) to the CrimeHolder */ override fun onBindViewHolder(holder: CrimeHolder?, position: Int) { holder!!.bind(mCrimes[position]) } /** * Sees how many items are in the RecyclerView that need to be shown */ override fun getItemCount(): Int = mCrimes.size } } 也可以使用queryParameters = { [param]: true }; 。我应该从哪里获取我的上下文,以及如何引用它?

.bigBox
{
    width: 700px;
    height: 500px;
    background: grey;
}

.box{
    width: 32%;
    display: inline-block;
    background: yellow;
}

3 个答案:

答案 0 :(得分:4)

在您的实施中,您可以安全地使用提供给Context的{​​{1}} View

OnClickListener

请记住设置onclick:

override fun onClick(p0: View) {
          Toast.makeText(p0.context, "${mCrime!!.mTitle} clicked!", Toast.LENGTH_SHORT / 2)
                .show()
    }

此外,默认情况下,所有Kotlin类都嵌套(fun bind(crime: Crime) { mCrime = crime val titleTextView = itemView.findViewById<TextView>(R.id.crime_title) val dateTextView = itemView.findViewById<TextView>(R.id.crime_date) titleTextView.text = mCrime!!.mTitle dateTextView.text = mCrime!!.mDate.toString() itemView.setOnClickListener(this) } )。所以你的static相当于Java中的private class CrimeHolder。这就是您无法在private static class CrimeHolder

中访问getActivity()的原因

答案 1 :(得分:1)

在持有人中使用itemView.context属性。

编辑: 您onClick没有工作的原因&#34; (未被调用)是因为您尚未注册onClickListener,例如:

itemView.setOnClickListener(this)

在您的持有人initbind内。

答案 2 :(得分:0)

 itemView.setOnClickListener {
                Toast.makeText(itemView.context, "Item is clicked $position", Toast.LENGTH_SHORT).show() }
        }