Android之ListView控件

Android之ListView控件

ListView类

ListView的主要属性

  • stackFromBottom,设置为true时,内容将从底部开始显示
  • cacheColorHint,设置为#00000000透明时,可以避免拖动时背景显示黑色的问题
  • divider,设置分割线,#00000000或@null分割线被隐藏
  • fadingEdge,设置为none,去除上边和下边的阴影
  • scrollbars,设置为none,隐藏滚动条

ListView介绍

ListView组件可以实现循环显示自定义组件的功能。

首先在显示ListView的XML文件里声明一个ListView:

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:background="#dee9f7">
   <ListView
       android:id="@+id/listview"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>
</LinearLayout>

在主函数中调用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<String> adapter = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_list_item_1,data);
        ListView listView = (ListView) findViewById(R.id.list_view);
        //将适配器中数据添加到ListView中
        listView.setAdapter(adapter);
    }
}

自定义ListView界面

自定义一个布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp" />

</LinearLayout>

定义好布局后,定义一个实体类,作为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<Fruit> {

    private int resourceId;

    public FruitAdapter(Context context, int textViewResourceId, List<Fruit> 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<Fruit> 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可以自己设置表头,表尾以及分割线。

  • footerDividersEnabled:是否在footerView(表尾)前绘制一个分隔条,默认为true
  • headerDividersEnabled:是否在headerView(表头)前绘制一个分隔条,默认为true
  • divider:设置分隔条,可以用颜色分割,也可以用drawable资源分割
  • dividerHeight:设置分隔条的高度

调用如下方法可对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 :选中时的颜色

你可能感兴趣的:(andriod,android)