欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

十、高级控件之ListView

程序员文章站 2022-07-14 18:06:05
...

上课视频:
链接:https://pan.baidu.com/s/1xR5jJBiU2vwiW2pE6AD5nA 密码:eg01

一、ListView 的使用场景

展示一些重复格式数据的列表,如消息列表,音乐列表。

二、ListView所用的设计模式介绍

ListView所用的设计模式是适配器模式,更多适配器想关设计模式介绍看下面地址:
http://www.runoob.com/design-pattern/adapter-pattern.html

三、ListView的使用。

使用ListView控件需要三个类
1.ListView本身控件
2.ListView的适配器,适配器要依赖数据集和Activity本身
3.数据集。

上课代码:
Activity中的代码

package com.joychen.www.a11listviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private MessageAdapter myadapter;

    private List<MessageBean> msgList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.mylistv);
        msgList = getMessageList();
        myadapter = new MessageAdapter(msgList,this);
        listView.setAdapter(myadapter);

    }


    private List<MessageBean> getMessageList(){
        List<MessageBean> list = new ArrayList<>();
        for (int i=0;i<30;i++){
            MessageBean mb = new MessageBean(i+"","","小明"+i,"最后一条消息"+i);
            list.add(mb);
        }

        return list;
    }

}

Activity中依赖的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:textColor="#fff"
        android:background="@color/colorPrimary"
        android:text="ListView控件使用"
        />

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

</LinearLayout>

适配器MessageAdapter代码

package com.joychen.www.a11listviewdemo;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by joychen on 2018/5/2.
 */

public class MessageAdapter extends BaseAdapter {

    private List<MessageBean> data;
    private Activity activity;

    public MessageAdapter(List<MessageBean> data,Activity activity){
        this.data = data;
        this.activity = activity;
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view =  LayoutInflater.from(activity).inflate(R.layout.item_message_lv,null);
        TextView name = view.findViewById(R.id.firend_name_txt);
        TextView message = view.findViewById(R.id.last_message_txt);
        TextView time = view.findViewById(R.id.time_txt);

        MessageBean mb = data.get(position);
        name.setText(mb.getName());
        message.setText(mb.getLastMessage());
        time.setText(getTime(mb.getTime()));
        return view;
    }

    private String getTime(long t){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return format.format(new Date(t));
    }
}

实体MessageBean

package com.joychen.www.a11listviewdemo;

import java.util.Date;

/**
 * Created by joychen on 2018/5/2.
 */

public class MessageBean {
    private String id;
    private String head;
    private String name;
    private String lastMessage;
    private long time = new Date().getTime();

    public MessageBean() {

    }

    /**
     *
     * @param id
     * @param head
     * @param name
     * @param lastMessage
     */
    public MessageBean(String id, String head, String name, String lastMessage) {
        this.id = id;
        this.head = head;
        this.name = name;
        this.lastMessage = lastMessage;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getHead() {
        return head;
    }

    public void setHead(String head) {
        this.head = head;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastMessage() {
        return lastMessage;
    }

    public void setLastMessage(String lastMessage) {
        this.lastMessage = lastMessage;
    }

    public long getTime() {
        return time;
    }

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

四、ListView的内存优化