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
class MyAdapter(private val list: List<Msg>, private val ctx: Context) : BaseAdapter() {
override fun getCount(): Int {
return list.size
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var view: View? = convertView
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
}
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()
listView3?.transcriptMode = AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL
})
}
}