将RecyclerView数据绑定到新活动(Android-Kotlin)

时间:2019-03-24 19:21:33

标签: android kotlin android-recyclerview

您好,我想传递用于填充recyclerview的数据。

class RecyclerViewAdapter(val list: List<ListModel>): RecyclerView.Adapter<RecyclerViewAdapter.ListViewHolder>(){

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ListViewHolder {
        val layoutInflater = LayoutInflater.from(p0.context)
        val itemlist = layoutInflater.inflate(R.layout.recycler_item,p0,false)
        return ListViewHolder(itemlist)
    }

    override fun onBindViewHolder(p0: ListViewHolder, p1: Int) {
        val myList: ListModel = list[p1]

        p0.itemView.txtImageName?.text = myList.title
        Glide.with(p0.itemView.context)
            .load(myList.postimg)
            .into(p0.itemView.listImage);

    }

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

    inner class ListViewHolder(v: View) : RecyclerView.ViewHolder(v){
        init {
            v.setOnClickListener {
                val intent = Intent(v.context, WebviewActivity::class.java)
                intent.putExtra("key", "testfromact") //this is where i want to put myList.title variable
                v.context.startActivity(intent)
            }

        }
    }

}

您可以看到我正在尝试将数据传递给新活动... 最好的简单广告方法是什么

1 个答案:

答案 0 :(得分:0)

用于将数据从RecyclerView传递到活动的优化代码:

这是适配器代码:

  1. 我没有使用过Kotlin合成,也没有使用onBindViewHolder()中的视图 由于该问题,每次它都会获取视图并显示它时,这并不是最佳实践,因此我们使用ViewHolder只是为了达到此目的并实现缓存。 因此,请看我如何在内部类中做到这一点。

  2. 对于优化的代码,我使用了这个漂亮的库。 Anko is a Kotlin library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.

    class TreeAdapter(private val listSurveyData: List<Tree>, val context: Context) : RecyclerView.Adapter<TreeAdapter.TreeViewHolder>() {
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): 
    TreeViewHolder {
    
    val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout_tree, parent, false)
    
     return TreeViewHolder(view)
    
      }
    
    override fun getItemCount(): Int = listSurveyData.size
    
    override fun onBindViewHolder(holder: TreeViewHolder, position: Int) {
        holder.setUpViewHolder(listSurveyData[position])
    }
    
    inner class TreeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val txtSpeciesName: TextView = itemView.txt_speciesName
        private val ctlContainer: ConstraintLayout = itemView.ctl_container
    
        fun setUpViewHolder(tree: Tree) {
            txtSpeciesName.text = tree.species
            ctlContainer.setOnClickListener {
                context.startActivity<MainActivity>(SURVERY_DATA_KEY to tree)
            }
        }
    }
    

    }

还如何使用它!

活动中: 另外,使用Serializable扩展Tree数据类!

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val tree = intent.getSerializableExtra(SURVERY_DATA_KEY) as Tree
    } 
}