基于BaseAdapter适配器和ListView控件的自定义显示
本内容是本人经过网上学习和查看相关资料为学习记忆而整理。
直接上代码:
package com.example.comtool;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Person;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.LinkedList;
//定义静态类
class ViewHolder{
TextView aName;
TextView aTell;
ImageView aIcon;
Button aBeat;
}
//定义person类
class person{
private String name;
private String tell;
private int icon;
private String beat;
public person(){
}
person(String name, String tell, int icon,String beat){
this.name=name;
this.tell=tell;
this.icon=icon;
this.beat=beat;
}
public String getName(){
return name;
}
public String getTell(){
return tell;
}
public int getIcon(){
return icon;
}
public String getBeat(){return beat;}
public void setName(String name){
this.name=name;
}
public void setTell(String tell){
this.tell=tell;
}
public void setIcon(int icon){
this.icon=icon;
}
public void setBeat(String beat){this.beat=beat;}
}
//定义适配器
class PersonAdapter extends BaseAdapter {
private LinkedList mData;
private Context mContext;
PersonAdapter(LinkedList<person> mData, Context mContext) {
this.mData = mData;
this.mContext = mContext;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder=null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_it, parent, false);
viewHolder=new ViewHolder();
viewHolder.aIcon = (ImageView) convertView.findViewById(R.id.imgtou);
viewHolder.aName = (TextView) convertView.findViewById(R.id.name);
viewHolder.aTell = (TextView) convertView.findViewById(R.id.says);
viewHolder.aBeat=(Button) convertView.findViewById(R.id.test);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.aIcon.setBackgroundResource(mData.get(position).getIcon());
viewHolder.aName.setText(mData.get(position).getName());
viewHolder.aTell.setText(mData.get(position).getTell());
viewHolder.aBeat.setText(mData.get(position).getBeat());
//定义按钮事件
viewHolder.aBeat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,mData.get(position).getTell(),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
public class Main2Activity extends AppCompatActivity {
//private LinkedList mData = null;
//private Context mContext;
//private PersonAdapter mAdapter = null;
///private ListView list1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Context mContext = this;
ListView list1 = (ListView) findViewById(R.id.listview1);
LinkedList<person> mData = new LinkedList<person>();
mData.add(new person("B神", "无形被黑,最为致命", R.mipmap.a1,"按钮1"));
mData.add(new person("基神", "大神好厉害~", R.mipmap.a2,"按钮2"));
mData.add(new person("曹神", "我将带头日狗~", R.mipmap.a3,"按钮3"));
mData.add(new person("测试", "这真的是个测试", R.mipmap.a4,"按钮4"));
PersonAdapter mAdapter = new PersonAdapter((LinkedList<person>) mData, mContext);
list1.setAdapter(mAdapter);
}
}
XML文件代码如下:
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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=".Main2Activity">
<ListView
android:layout_width="409dp"
android:layout_height="729dp"
android:id="@+id/listview1"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
配合ListView控件外观的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="200dp"
android:layout_height="62dp"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:textColor="#1D1D1C"
android:textSize="20sp" />
<TextView
android:id="@+id/says"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8px"
android:textColor="#B4B4B9"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#B4B4B9"
android:textSize="30sp" />
</LinearLayout>