在刚开是接触Android学习各种控件,ListView绝对是比较难掌握的一个控件,但是ListView又是很重要的一个控件,这也就要求我们必须好好掌握,现在Android出了recyclerview控件,这个控件比ListView要强大,未来应该会逐渐代替ListView,现在学习ListView可能有点过时了,但是ListView还是有很多值得我们学习的.因为多学一点没有坏处.
1.基本使用:展示数据和接收事件响应
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ListView mListView;
private ArrayList<String> mList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.container);
initData();
mListView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, mList));
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, mList.get(position), Toast.LENGTH_SHORT).show();
}
});
}
private void initData() {
for (int i = 0; i < 20; i++) {
mList.add("this is item " + i);
}
}
}
效果图:
2.常用属性设置:
<ListView
android:id="@+id/container"
android:divider="@color/colorPrimary"//定义分割线的背景颜色
android:dividerHeight="1dp"//设置分割线的宽度
android:layout_height="match_parent"
android:layout_width="match_parent"/>
如果想取消分割线显示:
setDividerHeight(0);// android:divider属性设置为@null或者设置为透明颜色.当然还有一些其他属性,和其他控件的属性设置差不多.
3,展示数据之Adapter.
ArrayAdapter:用来展示单条字符串数据,并且Android提供有现成的子布局,就是方便开发者展示数据,上面就是具体用法.
SimpleAdapter:展示的数据比ArrayAdapter展示的数据要丰富,可以用来展示多条数据,阅读源码发现,展示的数据类型包括imageView,textVIew和checkBox,下面我们来展示这三种类型的数据:
子布局:
<?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:layout_margin="5dp"
android:id="@+id/image_view_item"
android:src="@drawable/icon_one"
android:scaleType="fitXY"
android:layout_width="40dp"
android:layout_height="40dp"/>
<TextView
android:layout_marginTop="5dp"
android:id="@+id/text_view_item"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="0dp"/>
<CheckBox
android:layout_margin="5dp"
android:clickable="false" //为了防止 checkBox抢走item的点击事件,根据实际情况来设置
android:focusable="false"
android:id="@+id/check_box_item"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
代码:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ListView mListView;
private ArrayList<HashMap<String, Object>> mList = new ArrayList<HashMap<String, Object>>();
private int[] mImages = new int[]{R.drawable.icon_one, R.drawable.icon_two, R.drawable.icon_three, R.drawable.icon_four};
private SimpleAdapter simpleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.container);
initData();
simpleAdapter = new SimpleAdapter(MainActivity.this, mList, R.layout.list_item,
new String[]{"image", "text", "check"}, new int[]{R.id.image_view_item, R.id.text_view_item, R.id.check_box_item});
mListView.setAdapter(simpleAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, mList.get(position).get("text").toString(), Toast.LENGTH_SHORT).show();
if (mList.get(position).get("check").equals(false)) {
((CheckBox) view.findViewById(R.id.check_box_item)).setChecked(true);
mList.get(position).put("check", true);
} else {
((CheckBox) view.findViewById(R.id.check_box_item)).setChecked(false);
mList.get(position).put("check", false);
}
simpleAdapter.notifyDataSetChanged();
}
});
}
private void initData() {
Random random = new Random();
for (int i = 0; i < 20; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("image", mImages[random.nextInt(mImages.length)]);
map.put("text", "this is item " + i);
map.put("check", random.nextBoolean());
mList.add(map);
}
}
}
效果图:
BaseAdapter:以上几个现成的Adapter是Android帮我们封装实现好的Adapter,如果在实际开发中需要按照数据的组成来自定义布局,那么就需要实现自己的Adapter,自定义的Adapter都是需要继承BaseAdapte.
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ListView mListView;
private ArrayList<Data> mList = new ArrayList<Data>();
private int[] mImages = new int[]{R.drawable.icon_one, R.drawable.icon_two, R.drawable.icon_three, R.drawable.icon_four};
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.container);
initData();
mAdapter = new MyAdapter();
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, mList.get(position).getmTitle(), Toast.LENGTH_SHORT).show();
if (!mList.get(position).ismIsCheck()) {
((CheckBox) view.findViewById(R.id.check_box_item)).setChecked(true);
mList.get(position).setmIsCheck(true);
} else {
((CheckBox) view.findViewById(R.id.check_box_item)).setChecked(false);
mList.get(position).setmIsCheck(false);
}
mAdapter.notifyDataSetChanged();
}
});
}
private void initData() {
Random random = new Random();
for (int i = 0; i < 20; i++) {
Data data = new Data();
data.setmImage_id(mImages[random.nextInt(mImages.length)]);
data.setmTitle("this is base adapter item" + i);
data.setmIsCheck(random.nextBoolean());
mList.add(data);
}
}
private class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.image_view_item);
holder.textView = (TextView) convertView.findViewById(R.id.text_view_item);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.check_box_item);
convertView.setTag(holder);
} else {
holder = ((ViewHolder) convertView.getTag());
}
holder.imageView.setImageResource(mList.get(position).getmImage_id());
holder.textView.setText(mList.get(position).getmTitle());
holder.checkBox.setChecked(mList.get(position).ismIsCheck());
return convertView;
}
private class ViewHolder{
private ImageView imageView;
private TextView textView;
private CheckBox checkBox;
}
}
实现效果和上面用到的SimpleAdapter效果是一样的,不同的是创建了一个javaBean对象用来封装数据,并且我们在自定义布局加载数据时,要充分用到缓存和viewHolder,优化加载性能,这是固定写法,当然在性能考虑方法,如果布局中加载的图片过大,需要对图片资源做单独的优化处理,这是属于对图片的优化,这个话题另外再说,其实刚刚用到的ArrayAdapter是一个泛型类,我们只要改变传入的数据集和重写getView方法,可以很方便的实现自定义,不用重写这个BaseAdapter的抽象方法,并且ArrayAdapter还提供类其他方法,比如增加数据,更新数据的方法,还是比较好用的.好了,以上就是ListView的常规用法.