ListView 简介
ListView 是安卓里常用的控件, 本文介绍一下常用用法,以及优化等方法
1、改写activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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" />
</LinearLayout>
2、新建一个javabean
package com.example.tomchen.testlistview;
public class Icon {
private int imageId;
private String name;
public Icon(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
3、新建icon_item.xml 用于 listview 每个元素的视图
<?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="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
4、新建一个class IconAdapter 继承自 ArrayAdapter
package com.example.tomchen.testlistview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class IconAdapter extends ArrayAdapter<Icon> {
private int resourceId;
public IconAdapter(Context context, int resource, List<Icon> icons) {
super(context, resource, icons);
this.resourceId = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Icon icon = getItem(position);
View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
} else {
view = convertView;
}
ImageView image = (ImageView) view.findViewById(R.id.icon);
image.setImageResource(icon.getImageId());
TextView text = (TextView) view.findViewById(R.id.text);
text.setText(icon.getName());
return view;
}
}
Note
这儿有了一个优化, convertView 将之前的布局进行缓存, 以便之后进行重用.
所以我们判断convertView是否为空. 空的话就使用LayoutInflater去加载布局, 否则的话就直接复用convertView
测试listview
MainActivity里面加入下面代码
package com.example.tomchen.testlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<Icon> icons = new ArrayList<Icon>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
icons.add(new Icon("Icon_01", R.drawable.ic_01));
icons.add(new Icon("Icon_02", R.drawable.ic_02));
icons.add(new Icon("Icon_03", R.drawable.ic_03));
icons.add(new Icon("Icon_04", R.drawable.ic_04));
icons.add(new Icon("Icon_05", R.drawable.ic_05));
icons.add(new Icon("Icon_06", R.drawable.ic_06));
icons.add(new Icon("Icon_07", R.drawable.ic_07));
icons.add(new Icon("Icon_08", R.drawable.ic_08));
icons.add(new Icon("Icon_09", R.drawable.ic_09));
IconAdapter adapter = new IconAdapter(this, R.layout.icon_item, icons);
listView.setAdapter(adapter);
}
}
效果图如下:
您可能感兴趣的文章:解决Android ListView数据为空及加载错误的方法Android操作SQLite数据库(增、删、改、查、分页等)及ListView显示数据的方法详解Android实现树形层级ListViewAndroid编程实现动态更新ListView的方法Android listview多视图嵌套多视图Android中ListView用法实例分析Android程序开发ListView+Json+异步网络图片加载+滚动翻页的例子(图片能缓存,图片不错乱)Android 自定义弹性ListView控件实例代码(三种方法)学习Android Material Design(RecyclerView代替ListView)浅谈Android开发中ListView控件性能的一些优化方法Android实现ListView数据动态加载的方法Android编程之listView中checkbox用法实例分析Android中ListView结合CheckBox实现数据批量选择(全选、反选、全不选)android开发之listView组件用法实例简析实例讲解Android app开发中ListView的基本使用及优化