天天看点

乐学成语(成语分类列表显示)

   这次完成的是乐学成语中成语分类列表的显示,也就是对ListView控件的应用,对ListView的界面进行编辑制定,效果如图所示,这里也应用到了打招呼案例中的点击产生响应的操作,既当点击某一类别时,弹出此类别名称

乐学成语(成语分类列表显示)
乐学成语(成语分类列表显示)

1.首先需要准备好图片资源、文字资源,将图片资源导入到drawable中,文字资源写入values中的strings.xml中,如下所示:

<span style="font-size:14px;"><string-array name="category">
        <item>动物类</item>
        <item>自然类</item>
        <item>人物类</item>
        <item>季节类</item>
        <item>数字类</item>
        <item>寓言类</item>
        <item>其他类</item>
    </string-array></span>           

2.定义一个实体类,作为ListView适配器的适配类型,建类Category,代码如下:

package cn.edu.bztc.happyldiom.entity;

public class Category {
	private String name;//类别名称
	private int imageId;//类别对应的图片
	public Category(String name, int imageId) {
		super();
		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 setImaged(int imageId) {
		this.imageId = imageId;
	}
	
}
           

3.layout下新建activity_study.xml,构建一个轮廓ListView控件,代码如

<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"
    android:background="@drawable/bg_ling"
    tools:context=".StudyActivity" >

    <ListView
        android:id="@+id/lvCategories"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:listSelector="#00000000"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layoutAnimation="@anim/anim_layout_listview"
        >
    </ListView>
    
</RelativeLayout>           

4.然后为ListView的子项指定一个自定义布局,在layout目录下新建category_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:padding="10dp"
    android:orientation="horizontal" >
    <ImageView 
        android:id="@+id/category_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/category_animal"/>
    <TextView
        android:id="@+id/category_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="animal" 
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>           

5.在应用的包下创建自定义的适配器CategoryAdapter,适配器继承自ArrayAdapter,并将泛型指定为 Category类,代码如下:

package cn.edu.bztc.happyldiom.adapter;

import java.util.List;

import cn.edu.bztc.happyldiom.R;
import cn.edu.bztc.happyldiom.entity.Category;

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 CategoryAdapter extends ArrayAdapter<Category>{
	private int resourceId;
	public CategoryAdapter(Context context, int resource,
			List<Category> objects) {             //写了父类的构造方法,用于将上下文、listview子项布局的ID和数据dou传递进来
		super(context, resource, objects);
		resourceId=resource;
	}
@Override
public View getView(int position,View convertView,ViewGroup parent){//重写了getView()方法
	Category category=getItem(position);//获取当前项的Category实例
	View view;
	ViewHolder viewHolder;
	if(convertView==null){
		view=LayoutInflater.from(getContext()).inflate(resourceId, null);//LayoutInflater</span><span style="font-size:14px;">为子项加载我们传入的布局
		viewHolder=new ViewHolder();
		viewHolder.categoryImage=(ImageView) view.findViewById(R.id.category_image);//获取ImageView实例
		viewHolder.categoryName=(TextView) view.findViewById(R.id.category_name);//获取TextView实例
		view.setTag(viewHolder);//将viewholder存储在view中
		
		}else{
			
		view=convertView;
		viewHolder=(ViewHolder)view.getTag();//重新获取ViewHolder
	}
	viewHolder.categoryImage.setImageResource(category.getImageId());//设置显示的图片
	viewHolder.categoryName.setText(category.getName());//设置显示的文字
	return view;//最后将布局返回
}
class ViewHolder{
	ImageView categoryImage;
	TextView categoryName;
}
}
           

6.新建StudyActivity继承自Activity,代码如下:

<span style="font-size:14px;">package cn.edu.bztc.happyldiom.activity;

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

import cn.edu.bztc.happyldiom.R;
import cn.edu.bztc.happyldiom.adapter.CategoryAdapter;
import cn.edu.bztc.happyldiom.entity.Category;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class StudyActivity extends Activity {
	private List<Category> categoryList;
	private String[] category_names;
	private int[] category_images;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_study);
		intiCategories();  // 初始化类别
		CategoryAdapter adapter = new CategoryAdapter(this, //创建了CategoryAdapter对象
				R.layout.category_item, categoryList);
		ListView listView = (ListView) findViewById(R.id.lvCategories);
		listView.setAdapter(adapter);  //将CategoryAdapter作为适配器传递给ListView
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				switch (position) {
				case 0:
					Intent intent = new Intent(StudyActivity.this,
							StudyAnimalActivity.class);
					startActivity(intent);
					break;
				default:
					break;
				}
				Category category = categoryList.get(position);
				Toast.makeText(StudyActivity.this, category.getName(),
						Toast.LENGTH_LONG).show();//用于做出响应,弹出相应的名称

			}
		});
	}
	private void intiCategories() {
		categoryList = new ArrayList<Category>();
		Resources resources = getResources();
		category_names = resources.getStringArray(R.array.category);//将所有的目录名称导入
		category_images = new int[] { R.drawable.category_animal,   //将所有对应的图片导入
				R.drawable.category_nature, R.drawable.category_human,
				R.drawable.category_season, R.drawable.category_number,
				R.drawable.category_fable, R.drawable.category_other };
		for (int i = 0; i < category_names.length; i++) {
			categoryList.add(new Category(category_names[i], category_images[i]));//全部添加到目录列表中
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.study, menu);
		return true;
	}

}</span><span style="font-size: 18px;">
</span>           

7.这样完成了listview界面的制定,在运行程序之前记得修改AndroidManifest.xml文件将StudyActivity变为入口类,运行一下程序。