首页 > 程序开发 > 移动开发 > Android >

Android ListView(一)

2017-02-23

针对安卓代码基础,我大体分为三类:语法、控件和生命周期,此文简单记录一下常用控件之一ListView。

针对安卓代码基础,我大体分为三类:语法、控件和生命周期,此文简单记录一下常用控件之一ListView。

项目名称 ListDemo-Android
使用Android studio创建一个项目,名为ListDemo,创建时生成一个MainActivity的空界面
1,在layout文件包中,找到activity_main.xml文件,设置TetxView的id为text,添加一个ListView的控件,占满屏幕。代码如下:

    
    

    


2.在MainActivity.java文件中,声明变量:textView ,listView ,以及一个类型为ListViewAdapter的操作者 listViewAdapter
代码如下:
    private TextView textView;
    private ListView listView ;

    private ListViewAdapter listViewAdapter ;


实例化变量,代码如下:
        textView = (TextView) findViewById(R.id.text);
        listView = (ListView) findViewById(R.id.listview);

        textView.setText("代码展示ListView的实例:");

创建ListViewAdapter.java,继承BaseAdapter,引入方法,代码如下:

public class ListViewAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return null;
    }
}


现在Demo只做简单展示,每个列表表格都有:名字,年龄,头像三个元素。所以需要声明一个UserModel类型的数组和UserModel.java的类,以及一个ListAdapterView的静态类,
在ListViewAdapter中,声明一个上下文,定义一个构造函数:代码:
UserModel.java

public class UserModel {
    private int iconint ;
    private String name ;
    private int age ;


    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


    public void setIconint(int iconint) {
        this.iconint = iconint;
    }

    public int getIconint() {
        return iconint;
    }

    public String getName() {
        return name;
    }

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

listViewAdapter:

public class ListViewAdapter  extends BaseAdapter{

    private Context context ;
    private List list;

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

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

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

    private ListAdapterView adapterView ;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        adapterView = null ;
        if (convertView == null) {
            adapterView = new ListAdapterView();

            convertView = LayoutInflater.from(context).inflate(R.layout.view_adapter,parent,false);
            adapterView.icon = (ImageView ) convertView.findViewById(R.id.icon) ;
            adapterView.name = (TextView) convertView.findViewById(R.id.name);
            adapterView.age = (TextView) convertView.findViewById(R.id.age);
//            View v = Inflater.class
            convertView.setTag(adapterView);
        }else {
            adapterView = (ListAdapterView) convertView.getTag();
        }
        //赋值
        UserModel m= list.get(position);
        adapterView.icon.setBackgroundResource(m.getIconint());
        adapterView.name.setText(m.getName());
        adapterView.age.setText(m.getAge()+"");

        return convertView;
    }


    public ListViewAdapter(Context context, List list) {
        this.list = list;
        this.context = context;
    }

    static class ListAdapterView  {
        private ImageView icon ;
        private TextView name ;
        private TextView age ;
    }
}

在构建public View getView(int position, View convertView, ViewGroup parent)函数时,创建一个名为view_adapter.xml的文件,添加控件,代码:




    
    
    

此时,返回到MainActivity,java类,代码:

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private ListView listView ;

    private ListViewAdapter listViewAdapter ;

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

        textView = (TextView) findViewById(R.id.text);
        listView = (ListView) findViewById(R.id.listview);

        textView.setText("代码展示ListView的实例:");
        List list = new ArrayList<>();
        for (int i = 0 ; i<20;i++){
            UserModel m = new UserModel();
            m.setAge(i);

            switch (i%5){
                case 0:
                    m.setIconint(R.mipmap.a);
                    break;
                case 1:
                    m.setIconint(R.mipmap.b);
                    break;
                case 2:
                    m.setIconint(R.mipmap.c);
                    break;
                case 3:
                    m.setIconint(R.mipmap.d);
                    break;

                default:
                    m.setIconint(R.mipmap.a);
                    break;
            }

            m.setName("第"+i+"个");
            list.add(m);
        }
        listViewAdapter = new ListViewAdapter(this,list);
        listView.setAdapter(listViewAdapter);
    }
}

添加资源文件在mipmap中,四张图片分别是a.png、b.png、c.png、d.png

运行结果:如图:
\

相关文章
最新文章
热点推荐