[Android Development] BaseAdapter

BaseAdapter main container

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BaseActivity">

    <ListView
        android:id="@+id/list_view3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ImageView
        android:id="@+id/write"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@mipmap/write"
        android:layout_gravity="right|bottom"
        android:layout_margin="40dp"/>

</FrameLayout>

item layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/profile"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@mipmap/profile1"
        android:layout_margin="5dp"/>

    <TextView
        android:id="@+id/nickname"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="用户1"
        android:textSize="26sp"
        android:layout_toRightOf="@id/profile"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="2048-10-24"
        android:textSize="22sp"
        android:layout_toRightOf="@id/profile"
        android:layout_below="@id/nickname"/>

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Good Morning!"
        android:textSize="22sp"
        android:layout_margin="5dp"
        android:layout_below="@id/profile"/>

    <ImageView
        android:id="@+id/repost"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/repost"
        android:layout_below="@id/content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"/>

    <ImageView
        android:id="@+id/comment"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/comment"
        android:layout_below="@id/content"
        android:layout_toLeftOf="@id/repost"
        android:layout_marginRight="10dp"/>

    <ImageView
        android:id="@+id/like"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/like"
        android:layout_below="@id/content"
        android:layout_toLeftOf="@id/comment"
        android:layout_marginRight="10dp"/>

</RelativeLayout>

item object

class Msg(var profile: Int, var nickname: String, var content: String, var isLike: Boolean)

Create Adapter, inherit BaseAdapter

//根据准备好的数据源和子项布局完成ListView效果的一一设置
//做细节处理
class MyAdapter(private val list: List<Msg>, private val ctx: Context) : BaseAdapter() {
    
    
    //获取数量(设置ListView的长度)
    override fun getCount(): Int {
    
    
        return list.size
    }

    //获取视图(设置ListView每一项的显示效果)
    //用来处理组件和数据适配的过程
   override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    
    
        //完成对View的设置
        //将布局资源转为View
        var view: View? = convertView
        //参数1:你所要引用的布局资源
        //参数2:父容器
        view = LayoutInflater.from(ctx).inflate(R.layout.item4, null) //?

        val msg = list[position]

        val profile = view.findViewById<ImageView>(R.id.profile)
        profile.setImageResource(msg.profile)

        val nickname = view.findViewById<TextView>(R.id.nickname)
        nickname.text = msg.nickname

        val like = view.findViewById<ImageView>(R.id.like)
        if (msg.isLike) {
    
    
            like.setImageResource(R.mipmap.liked)
        } else {
    
    
            like.setImageResource(R.mipmap.like)
        }

        val comment = view.findViewById<ImageView>(R.id.comment)
        comment.setOnClickListener {
    
     Toast.makeText(ctx, "你点击了评论", Toast.LENGTH_SHORT).show() }

        val repost = view.findViewById<ImageView>(R.id.repost)
        repost.setOnClickListener {
    
    Toast.makeText(ctx, "----------转发----------", Toast.LENGTH_SHORT).show()}


        return view
    }

	//返回某个数据项
    override fun getItem(position: Int): Any? {
    
    
        return null
    }
    
	//返回某个数据项的ID
    override fun getItemId(position: Int): Long {
    
    
        return 0
    }


}

Configure Adapter

class BaseActivity : AppCompatActivity() {
    
    
    private var listView3: ListView? = null
    private var write : ImageView? = null
    private val list: MutableList<Msg> = ArrayList()
    private val ps = intArrayOf(
        R.mipmap.profile1, R.mipmap.profile2,
        R.mipmap.profile3, R.mipmap.profile4, R.mipmap.profile5, R.mipmap.profile6,
        R.mipmap.profile7, R.mipmap.profile8
    )

    private fun InitData() {
    
    
        for (i in 1..8) {
    
    
            val m = Msg(ps[i - 1], "用户$i", "Good Morning $i", i % 2 == 0)
            list.add(m)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_base)

        var listView_copy = listView3
        listView_copy = findViewById(R.id.list_view3)
        write = findViewById(R.id.write)

        InitData()

        val adapter : BaseAdapter = MyAdapter(list, this)

        listView_copy?.adapter = adapter

        var myWrite = write
        myWrite?.setOnClickListener(View.OnClickListener {
    
    
            val m = Msg(R.mipmap.profile9, "paradox", "Good noon", false)
            list.add(m)
            //通知适配器更新数据
            adapter.notifyDataSetChanged()
            //设置listview自动显示到最新数据
            listView3?.transcriptMode = AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL
        })
    }
}

Guess you like

Origin blog.csdn.net/weixin_42020386/article/details/112883962