Android之ListView控件

实现如图效果:1.水果名字,和水果照片

2.点击ListView 响应点击事件


首先写布局文件:

1 activity_main.xml给ListView设置id

<LinearLayout 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=".MainActivity" >

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

</LinearLayout>

2 fruit_item.xml     android:layout_gravity="center  让文字居中显示

<pre name="code" class="html"><?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="10dp"
        />
    <!-- android:layout_gravity="center"  让文字居中显示 -->
</LinearLayout>
<span style="font-family: Arial, Helvetica, sans-serif;">布局写好了,然后就开始写java代码吧</span>

    Fruit.java 
 

package com.example.listviewtest;


public class Fruit 
{
	private String name;  //水果名称
	
	private int imageId;  //水果对应图片的资源id
	
	public Fruit(String name, int imageId) //有参数的构造函数
	{
		this.name = name;
		this.imageId = imageId;
	}


	public String getName() 
	{
		return name;
	}


	public void setName(String name) 
	{
		this.name = name;
	}


	
	
	public int getImageId() 
	{
		return imageId;
	}


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

在MainActivity中  首先要有显示的资源文件在这里定义一个
private List<Fruit> fruitList = new ArrayList<Fruit>(); 接下来自定义一个适配器,这个适配器继承自ArrayAdapter,并将泛型制定为Fruit类

新建FruitAdapter

package com.example.listviewtest;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FruitAdapter extends ArrayAdapter<Fruit>   //将泛型指定为Fruit类
{
	private int resourceId;
	private ImageView fruitImage;
	private TextView	fruitName ;
	private View		 view;
	/*实现ArrayAdapter<>的方法
	 * ArrayAdapter重写了父类的一组构造函数,用于将上下文,ListView子项布局的id和数据都传递进来
	 * */
		
	//1方法
	public FruitAdapter(
				Context context,  //上下文
				int textViewResourceId, //ListView子项布局的id
				List<Fruit> objects)   //数据
		{
			super(context, textViewResourceId, objects);
			resourceId = textViewResourceId ; 
		}
	//2又重写了getView()方法
	//这个方法是每个子项被滚动到屏幕内的时候被调用
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			Fruit	fruit = getItem(position);//首先通过getItem()获得当前项的Fruit实例
			ViewHolder viewHolder;
				if(convertView == null)//如果不空,调用缓存加载出来,如果为空独自获取数据加载
				{ 					//然后使用LayoutInflater来为这个子项加载我们传入的布局
					view =	LayoutInflater.from(getContext()).inflate(resourceId, null);
					viewHolder = new ViewHolder();
					//接着调用的View的FindViewById()的方法分别获得ImageView和TextView的实例
					viewHolder.fruitImage =(ImageView) view.findViewById(R.id.fruit_image);
					viewHolder.fruitName  =(TextView) view.findViewById(R.id.fruit_name);
					view.setTag(viewHolder);   //将viewHolder存储在view中
				}
				else 
				{
					view =convertView;
				viewHolder = (ViewHolder) view.getTag(); //重新获取viewHolder
				}
		/*	
			fruitImage  =(ImageView) view.findViewById(R.id.fruit_image);
			fruitName  =(TextView) view.findViewById(R.id.fruit_name);*/
	//再用它们的setImageResource(fruit.getImageId()),setText(fruit.getName());方法显示图片和文字
				viewHolder.fruitImage.setImageResource(fruit.getImageId());
				viewHolder.fruitName.setText(fruit.getName());
				return  view ;
		}
		class ViewHolder //这里为了提高效率,用一个ViewHolder类,来缓存,在上面做一个判断,
		{
			ImageView fruitImage;
			TextView fruitName;
		}
}
MainActivity.java   

package com.example.listviewtest;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

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

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initFruit(); 	//初始化水果的数据
		FruitAdapter  adapter = new FruitAdapter(getApplicationContext(),R.layout.fruit_item,fruitList);
		ListView listview = (ListView) findViewById(R.id.list_View);
		listview.setAdapter(adapter);
		
		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(), 0).show();
			}
		});
	
	}



	private void initFruit()
	{
		Fruit aa = new Fruit("aa", R.drawable.aa);
		fruitList.add(aa);
		
		Fruit bb = new Fruit("bb", R.drawable.bb);
		fruitList.add(bb);
		
		Fruit cc = new Fruit("cc", R.drawable.cc);
		fruitList.add(cc);
		
		Fruit ee = new Fruit("ee", R.drawable.ee);
		fruitList.add(ee);
		
		Fruit ff = new Fruit("ff", R.drawable.ff);
		fruitList.add(ff);
		
		Fruit gg = new Fruit("gg", R.drawable.gg);
		fruitList.add(gg);
		
		Fruit hh = new Fruit("hh", R.drawable.hh);
		fruitList.add(hh);
		
		Fruit ii = new Fruit("ii", R.drawable.ii);
		fruitList.add(ii);
		
		Fruit jj = new Fruit("jj", R.drawable.jj);
		fruitList.add(jj);
		
		Fruit ll = new Fruit("ll", R.drawable.ll);
		fruitList.add(ll);
		
		Fruit mm = new Fruit("mm", R.drawable.mm);
		fruitList.add(mm);
	}

	
}



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