【Android开发】BaseAdapter

本文展示了如何使用BaseAdapter结合ListView在Android中创建一个动态加载数据的列表,并实现条目的点击事件,包括用户头像、昵称、内容的显示以及点赞、评论和转发功能的交互。同时,通过在活动中添加写入按钮,演示了如何更新数据并通知Adapter刷新列表。
摘要由CSDN通过智能技术生成
BaseAdapter主容器
<?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布局
<?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对象
class Msg(var profile: Int, var nickname: String, var content: String, var isLike: Boolean)
创建Adapter,继承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
    }


}
配置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
        })
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值