自定义 ListView 界面显示

实现的效果图:

 

1,定义一个实体类作为ListView适配器的适配类型,Base 只有三个字段 iconId表示对应图片资源id, title和time 分别表示标题的时间

public class Base {

    private int iconId;//对应图片资源id
    private String title;
    private String time;

    public Base(int iconId, String title, String time) {
        this.iconId = iconId;
        this.title = title;
        this.time = time;
    }

    public void setIconId(int iconId) {
        this.iconId = iconId;
    }

    public int getIconId() {
        return iconId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

 

2,为ListView的子项指定一个自定义布局,用于展示每一条数据,向下面这样用一个ImageView 和 两个TextView显示

 

创建base_item.xml,代码如下

<?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="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        
        <TextView
            android:id="@+id/time"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
    
</LinearLayout>

 

 

 

3,创建自定义适配器ContentAdapter,继承自ArrayAdapter

 

public class ContentAdapter extends ArrayAdapter<Base> {

    private int resourceId;
    /**
     * 重写父类构造函数,传入上下文, ListView子布局id, 数据
     * @param context
     * @param textViewResourceId
     * @param objects
     */
    public ContentAdapter(@NonNull Context context, int textViewResourceId, @NonNull List<Base> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }


    /**
     * 每个子项被滚动到屏幕内的时候调用
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Base base = getItem(position);//获取当前项 Base 实例

        View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);

        ImageView baseIcon = (ImageView) view.findViewById(R.id.icon);
        TextView baseTitle = (TextView) view.findViewById(R.id.title);
        TextView baseTime = (TextView) view.findViewById(R.id.time);

        baseIcon.setImageResource(base.getIconId());
        baseTitle.setText(base.getTitle());
        baseTime.setText(base.getTime());

        return view;
    }
}

 

4,编写MainActivity

public class MainActivity extends AppCompatActivity {

    private List<Base> baseList = new ArrayList<>();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    String date = df.format(new Date());// new Date()为获取当前系统时间,也可使用当前时间戳
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBases();//初始化baseList数据


//创建ContentAdapter实例,传入上下文, 子布局id ,数据baseList BaseAdapter adapter = new ContentAdapter(MainActivity.this,R.layout.base_item,baseList); ListView listView = (ListView) findViewById(R.id.listview); listView.setAdapter(adapter);//绑定适配器 listView.setOnItemClickListener(onClickListener);//监听点击事件 } AdapterView.OnItemClickListener onClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Base base = baseList.get(i); Toast.makeText(MainActivity.this,base.getTitle(),Toast.LENGTH_SHORT).show(); } }; private void initBases(){ for(int i = 0; i < 5; i++){ Base baseOne = new Base(R.drawable.one,"我是数字1",date); baseList.add(baseOne); Base baseTwo = new Base(R.drawable.two,"我是数字2",date); baseList.add(baseTwo); Base baseThree = new Base(R.drawable.one,"我是数字1",date); baseList.add(baseOne); Base baseFour = new Base(R.drawable.two,"我是数字2",date); baseList.add(baseTwo); } } }

 

 

完整Demo https://files.cnblogs.com/files/boguse/ListViewDemo.zip

posted @ 2018-08-24 14:58  九月彭于晏  阅读(1462)  评论(0编辑  收藏  举报