ListView的主要属性
stackFromBottom,设置为true时,内容将从底部开始显示 cacheColorHint,设置为#00000000透明时,可以避免拖动时背景显示黑色的问题 divider,设置分割线,#00000000或@null分割线被隐藏 fadingEdge,设置为none,去除上边和下边的阴影 scrollbars,设置为none,隐藏滚动条 ListView介绍ListView组件可以实现循环显示自定义组件的功能。
首先在显示ListView的XML文件里声明一个ListView:
在主函数中调用ListView
public class MainActivity extends AppCompatActivity {
//准备数据源
private String[] data = { "Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape",
"Pineapple", "Strawberry", "Cherry", "Mango", "Apple", "Banana", "Orange", "Watermelon",
"Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango",};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//将数据源添加到适配器
ArrayAdapter adapter = new ArrayAdapter(
MainActivity.this, android.R.layout.simple_list_item_1,data);
ListView listView = (ListView) findViewById(R.id.list_view);
//将适配器中数据添加到ListView中
listView.setAdapter(adapter);
}
}
自定义ListView界面
自定义一个布局
定义好布局后,定义一个实体类,作为ListView适配器的适配类型
public class Fruit {
private String name;
private int imageId;
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
创建一个自定义适配器,继承ArrayAdapter
public class FruitAdapter extends ArrayAdapter {
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId, List objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
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;
}
}
getView()这个方法在每个子项被滚动到屏幕内部都会被调用。首先通过getItem()方法得到当前项的Fruit实例,然后使用LayoutInflater为这个子项加载传入的布局。
最后修改主函数
public class MainActivity extends AppCompatActivity {
private List fruitList = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits(); // 初始化水果数据
FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
private void initFruits() {
for (int i = 0; i < 2; i++) {
Fruit apple = new Fruit("Apple", R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana = new Fruit("Banana", R.drawable.banana_pic);
fruitList.add(banana);
Fruit orange = new Fruit("Orange", R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear", R.drawable.pear_pic);
fruitList.add(pear);
Fruit grape = new Fruit("Grape", R.drawable.grape_pic);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit mango = new Fruit("Mango", R.drawable.mango_pic);
fruitList.add(mango);
}
}
}
ListView 点击事件
在主函数中修改代码:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
Fruit fruit = fruitList.get(position);
Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
使用setOnItemClickListener()方法为ListView注册了一个监听器,当用户点击了ListView中任何一个子项,就会回调onItemClick()方法。该方法中可以通过position参数判断出用户点击的是哪一个子项。
设置表头表尾与分割线
调用如下方法可对ListView表头或者表尾的属性进行设置:
addHeaderView(View v):添加headView(表头),括号中的参数是一个View对象 addFooterView(View v):添加footerView(表尾),括号中的参数是一个View对象 addHeaderView(headView, null, false):和前面的区别:设置Header是否可以被选中 addFooterView(View,view,false):同上 ListView常用属性 android:divider="#f9b68b" :分割线颜色或样式 android:scrollbars=“none”:不显示滚动条 android:fadingEdge=“none” :去掉上边和下边黑色的阴影 android:divider="@drawable/@null" :不想显示分割线 android:scrollbars=“none” setVerticalScrollBarEnabled(true);:隐藏listView的滚动条 android:fadeScrollbars=“true” :设置为true就可以实现滚动条的自动隐藏和显示 android:fastScrollEnabled = “true” :加快滑动速度 android:listSelector="@color/pink" listView item :选中时的颜色作者:builder2991