脚本宝典收集整理的这篇文章主要介绍了koltin使用RecyclerView,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
ITem
<?XMl version="1.0" encoding="utf-8"?>
<LinearLayout >"http://schemas.andROId.COM/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/topic_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="0sp" />
<TextView
android:id="@+id/article_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/create_at"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="end"
android:textSize="12sp" />
</LinearLayout>
Adapter
class ArticleAdapter(val items: ArrayList<JSONObject>) : RecyclerView.Adapter<ArticleAdapter.ViewHolder>() {
override fun getItemCount(): Int = items.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder? {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
val holder = ViewHolder(itemView)
itemView.setOnClickListener {
parent.context.startActivity<ContentActivity>("id" to holder.topic_id.text)
}
return holder
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = items[position]
holder.title.text = item.getString("title")
holder.topic_id.text = item.getString("id")
holder.create_at.text = DateTime(item.getString("create_at")).toString("yyyy:MM:dd HH:mm")
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val title = itemView.find<TextView>(R.id.article_title)
val create_at = itemView.find<TextView>(R.id.create_at)
val topic_id = itemView.find<TextView>(R.id.topic_id)
}
}
Main Layout
<android.support.v4.widget.SwipeRefreshLayout >"http://schemas.android.com/apk/res/android"
>"http://schemas.android.com/tools"
android:id="@+id/layout_swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bug.myapplication.ArticleFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
Activity
val a_lists = ArrayList<JSONObject>()
val adapter = ArticleAdapter(a_lists)
list.layoutManager = LinearLayoutManager(this)
list.adapter = adapter
以上是脚本宝典为你收集整理的koltin使用RecyclerView全部内容,希望文章能够帮你解决koltin使用RecyclerView所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。