koltin使用RecyclerView
Item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="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 xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="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
相关推荐
chenjinlong 2020-02-03
yinbaoshiguang 2019-06-27
明学的白板 2018-01-25
优主张 2018-01-16
稀土 2018-01-16
sfhong00 2020-06-05
sheikhdz 2020-05-18
chenjinlong 2020-05-05
安辉 2020-04-30
magic00 2019-12-24
fengyeezju 2019-12-12
magic00 2019-10-21
whale 2019-06-30
DaLei 2019-06-30