Android简单ListView练习记录

Android简单ListView练习记录

新建项目UILayoutListView

  • 主文件:MainActivity.java
  • 水果类:Fruit.java
  • 适配器类:FruitAdapter.java
  • 主界面XML文件:activity_main.xml
  • 定义水果列表XML:fruit_item.xml

activity_main.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="match_parent" >

    <!-- 列表 -->
    <!-- 设置为全屏 -->
    <ListView  android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

fruit_item.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="match_parent" android:orientation="horizontal" >

    <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" android:layout_marginLeft="10dip" />

</LinearLayout>

Fruit.java

package com.example.uilayoutlistview;

public class Fruit {

    private String name;
    private int ImageId;

    public Fruit(String name, int imageId) {
        super();
        this.name = name;
        ImageId = imageId;
    }

    public String getName() {
        return name;
    }

    public int getImageId() {
        return ImageId;
    }
}

FruitAdapter.java

package com.example.uilayoutlistview;

import java.util.*;

import android.annotation.SuppressLint;
import android.content.*;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class FruitAdapter extends ArrayAdapter<Fruit> {

    private int resourceId;

    public FruitAdapter(Context context, int textViewResourceId,
            List<Fruit> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit fruit = getItem(position); // 获取当前Fruit实例
        // View view = LayoutInflater.from(getContext()).inflate(resourceId,
        // null); //获取View

        // 为了提高ListView的运行效率
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);

            // 为了更高的提高效率 对控件的实例进行缓存
            viewHolder = new ViewHolder();
            viewHolder.fruitImage = (ImageView) view
                    .findViewById(R.id.fruit_image);
            viewHolder.textView = (TextView) view.findViewById(R.id.fruit_name);

            view.setTag(viewHolder); // 将viewHolder放入缓存

        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.fruitImage.setImageResource(fruit.getImageId());
        viewHolder.textView.setText(fruit.getName());

        return view;
    }

    private class ViewHolder {
        ImageView fruitImage;
        TextView textView;
    }
}

MainActivity.java

package com.example.uilayoutlistview;

import java.util.*;

import android.app.*;
import android.os.*;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
    private List<Fruit> fruitList = new ArrayList<Fruit>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        // 初始化水果数据
        initFruits();

        // 建立适配器来加载数据
        FruitAdapter fruitAdapter = new FruitAdapter(MainActivity.this,
                R.layout.fruit_item, fruitList);

        final ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setTextFilterEnabled(true);
        listView.setOnItemClickListener(new 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();
            }

        });
        listView.setAdapter(fruitAdapter);

    }

    private void initFruits() {
        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);
    }
}

运行效果如下图所示
Android简单ListView练习记录_第1张图片

2015年9月4日16:01:32

你可能感兴趣的:(Android简单ListView练习记录)