使用ListView的传统写法

作者: wxyass 分类: Android 发布时间: 2017-06-27 14:51

我们一般编写listView的时候顺序是这样的:

  • 需要展示的数据集List
  • 为这个数据集编写一个ListView
  • 为这个ListView编写一个Adapter,一般继承自BaseAdapter
  • 在BaseAdapter内部编写一个ViewHolder类,对应ListView里面的item控件,提高控件的查询效率

1. 在activity_main.xml中添加listview布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

</RelativeLayout>

2. 添加listview的条目布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp" >

    <TextView
        android:id="@+id/titleTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="Android新技能"
        android:textColor="#444"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/descTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/titleTv"
        android:layout_marginTop="10dp"
        android:maxLines="2"
        android:minLines="1"
        android:text="Android为ListView和GridView打造万能适配器"
        android:textColor="#898989"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/timeTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/descTv"
        android:layout_marginTop="10dp"
        android:paddingTop="3dp"
        android:text="2015-05-04"
        android:textColor="#898989"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/phoneTv"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/descTv"
        android:layout_marginTop="10dp"
        android:background="#2ED667"
        android:drawableLeft="@drawable/phone"
        android:drawablePadding="5dp"
        android:gravity="center"
        android:padding="2dp"
        android:text="10086"
        android:textColor="#ffffff"
        android:textSize="12sp" />

</RelativeLayout>

3. 添加bean对象

package com.wxyass.listviewadapter;


/**
 * Created by smyhvae on 2015/5/4.
 */
public class Bean {
    private String title;
    private String desc;
    private String time;
    private String phone;

    public Bean() {
    }

    public Bean(String title, String desc, String time, String phone) {
        this.title = title;

        this.desc = desc;
        this.time = time;
        this.phone = phone;
    }

    public String getTitle() {
        return title;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getTime() {
        return time;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

4. 添加自定义适配器MyAdapter.java继承自BaseAdapte

package com.wxyass.listviewadapter;


import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * Created by smyhvae on 2015/5/4.
 */
public class MyAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<Bean> mDatas;

    //MyAdapter需要一个Context,通过Context获得Layout.inflater,然后通过inflater加载item的布局
    public MyAdapter(Context context, List<Bean> datas) {

        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }

    //返回数据集的长度
    @Override
    public int getCount() {
        return mDatas.size();
    }

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

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

    //这个方法才是重点,我们要为它编写一个ViewHolder
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_listview, parent, false); //加载布局
            holder = new ViewHolder();

            holder.titleTv = (TextView) convertView.findViewById(R.id.titleTv);
            holder.descTv = (TextView) convertView.findViewById(R.id.descTv);
            holder.timeTv = (TextView) convertView.findViewById(R.id.timeTv);
            holder.phoneTv = (TextView) convertView.findViewById(R.id.phoneTv);

            convertView.setTag(holder);
        } else {   //else里面说明,convertView已经被复用了,说明convertView中已经设置过tag了,即holder
            holder = (ViewHolder) convertView.getTag();
        }

        Bean bean = mDatas.get(position);
        holder.titleTv.setText(bean.getTitle());
        holder.descTv.setText(bean.getDesc());
        holder.timeTv.setText(bean.getTime());
        holder.phoneTv.setText(bean.getPhone());

        return convertView;
    }

    //这个ViewHolder只能服务于当前这个特定的adapter,因为ViewHolder里会指定item的控件,不同的ListView,item可能不同,所以ViewHolder写成一个私有的类
    private class ViewHolder {
        TextView titleTv;
        TextView descTv;
        TextView timeTv;
        TextView phoneTv;
    }

} 

5. 修改MainActivity.java中的代码

package com.wxyass.listviewadapter;


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

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView listView;
    private List<Bean> mDatas;
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();

    }

    //方法:初始化View
    private void initView() {
        listView = (ListView) findViewById(R.id.listView);
    }

    //方法;初始化Data
    private void initData() {
        mDatas = new ArrayList<Bean>();

        //将数据装到集合中去
        Bean bean = new Bean("Android新技能1", "Android为ListView和GridView打造万能适配器", "2015-05-04", "10086");
        mDatas.add(bean);

        bean = new Bean("Android新技能2", "Android为ListView和GridView打造万能适配器", "2015-05-04", "10086");
        mDatas.add(bean);

        bean = new Bean("Android新技能3", "Android为ListView和GridView打造万能适配器", "2015-05-04", "10086");
        mDatas.add(bean);

        bean = new Bean("Android新技能4", "Android为ListView和GridView打造万能适配器", "2015-05-04", "10086");
        mDatas.add(bean);

        //为数据绑定适配器
        mAdapter = new MyAdapter(this,mDatas);

        listView.setAdapter(mAdapter);
    }

}

成功显示:

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注