ListView的简单运用

在前面几篇博客中介绍了ListView控件的使用,今天将用一个小案例介绍ListView的简单运用,这个例子参考了郭霖老师的《第一行Android代码》,郭霖老师的书中介绍的有点复杂,其实不需要那么复杂也可以实现那个效果,要实现一个如下所示的效果。

滑动屏幕可以查看屏幕上展示的所有水果



点击不同的水果后会在屏幕上提示点中的水果



实现代码,首先使用Android Studio创建一个Android工程

1、修改activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.fyt.fruitdemo.MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ListView>
</RelativeLayout>

新建一个布局文件item_listview.xml用于设置条目的布局

<?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:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/apple_pic"/>

    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="10dip"
        android:textSize="30dp"
        android:text="苹果"/>
</LinearLayout>

新建Fruit.java文件用于处理水果的信息

package com.fyt.fruitdemo;

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 void setNameId(String name) {
        this.name = name;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
}

修改MainActivity.java文件

package com.fyt.fruitdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private List<Fruit> fruitList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //创建集合,用来保存水果的数据
        fruitList = new ArrayList<Fruit>();

        //创建水果对象
        Fruit fruit1 = new Fruit("苹果", R.drawable.apple_pic);
        Fruit fruit2 = new Fruit("香蕉", R.drawable.banana_pic);
        Fruit fruit3 = new Fruit("樱桃", R.drawable.cherry_pic);
        Fruit fruit4 = new Fruit("葡萄", R.drawable.grape_pic);
        Fruit fruit5 = new Fruit("芒果", R.drawable.mango_pic);
        Fruit fruit6 = new Fruit("桔子", R.drawable.orange_pic);
        Fruit fruit7 = new Fruit("梨", R.drawable.pear_pic);
        Fruit fruit8 = new Fruit("菠萝", R.drawable.pineapple_pic);
        Fruit fruit9 = new Fruit("草莓", R.drawable.strawberry_pic);
        Fruit fruit10 = new Fruit("西瓜", R.drawable.watermelon_pic);

        //将水果对象添加到集合中
        fruitList.add(fruit1);
        fruitList.add(fruit2);
        fruitList.add(fruit3);
        fruitList.add(fruit4);
        fruitList.add(fruit5);
        fruitList.add(fruit6);
        fruitList.add(fruit7);
        fruitList.add(fruit8);
        fruitList.add(fruit9);
        fruitList.add(fruit10);

        //获得布局文件上的ListView控件
        ListView listView = (ListView)findViewById(R.id.lv);

        //为ListView设置适配器
        listView.setAdapter(new MyAdapter());

        //给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();
            }
        });
    }

    //创建一个继承自BaseAdapter的内部类
    public class MyAdapter extends BaseAdapter {

        //由系统调用,用于设置ListView中条目的个数
        @Override
        public int getCount() {
            return fruitList.size();
        }

        //由系统调用,获取一个View对象,作为ListView的条目
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            //从集合中获得水果的信息
            Fruit fruit = fruitList.get(position);

            View v;

            //如果ListView的条目没有缓存
            if(convertView == null) {

                //将布局文件填充成一个View对象
                v = View.inflate(MainActivity.this, R.layout.item_listview, null);
            }
            //ListView中的条目有缓存
            else {
                v = convertView;
            }

            //设置水果的图片
            ImageView imageView = (ImageView) v.findViewById(R.id.fruit_image);
            imageView.setImageResource(fruit.getImageId());


            //设置水果的名称
            TextView textView = (TextView) v.findViewById(R.id.fruit_name);
            textView.setText(fruit.getName());

            return v;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
    }
}

你可能感兴趣的:(ListView的简单运用)