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

已注销
981 声望48 粉丝

Focus on programming efficiency