1.ListView简单用法
第一步:
在activity_main.xml中增加:
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
第二步:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1 ,data); //data是自定义数组
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
2.定制LIstView界面(MVC)
V层:
在activity_main.xml建立一个ListView控件
新建立一ListView的页面子布局
(绘制V层的时候会自动调用getView())
M层:
建立一需要展示的实体类
C层:
建立一自定义Adapter(继承ArrayAdapter)
例子如下:
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
/***
*
* @param context 域,上下文
* @param textViewResourceId 布局ID
* @param objects 实体对象
*/
public FruitAdapter(Context context,int textViewResourceId, List<Fruit> objects ) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
*Provides the metrics for the shadow image. These include the dimensions of
* the shadow image, and the point within that shadow that should
* be centered under the touch location while dragging.
//控制展示,相当于DAO类,获得数据
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position); //自动将List<Fruit>中的值解出,获取当前项的Fruit实例
//new 出布局、控件
View view = LayoutInflater.from(getContext()).inflate(resourceId,null); (布局)
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image); (控件)
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name); (控件)
//赋值
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
}
在MainActivity中调用,相当于Action类
例子如下:
private List<Fruit> fruitList = new ArrayList<Fruit>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化fruitList
initFruits();
FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
ListView listView = (ListView) findViewById(R.id.list_view);
//返回数据至V层
listView.setAdapter(adapter);
}
private void initFruits() {
Fruit apple = new Fruit("Apple",R.drawable.a);
fruitList.add(apple);
Fruit banana = new Fruit("Banana",R.drawable.b);
fruitList.add(banana);
Fruit orange = new Fruit("Orange",R.drawable.c);
fruitList.add(orange);
Fruit watermelon = new Fruit("Watermelon",R.drawable.d);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear",R.drawable.e);
fruitList.add(pear);
Fruit grape = new Fruit("Grape",R.drawable.f);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple",R.drawable.g);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("Strawberry",R.drawable.h);
fruitList.add(strawberry);
Fruit cherry = new Fruit("Cherry",R.drawable.i);
fruitList.add(cherry);
Fruit mango = new Fruit("Mango",R.drawable.j);
fruitList.add(mango);
}
getView:
getView()方法是ListView的每个列表项目绘制在屏幕上时被调用
3.提升listview的运行效率
View view; if (convertView == null) { view = LayoutInflater.from(getContext()).inflate(resourceId, null); } else { view = convertView; }
ConvertView是对之前加载好的布局进行缓存
4.Android获取LayoutInflater对象的方法总结
获取context对象
一:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View child = inflater.inflate(R.layout.child, null);
二:
LayoutInflater inflater = LayoutInflater.from(context); View child = inflater.inflate(R.layout.child, null);
方法1和方法2其实都是对context().getSystemService()的使用