5.乐学成语-----------显示动物类成语列表

1.在layout下新建activity_animal.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:background="@drawable/bg_animal"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/lvAnimalList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layoutAnimation="@anim/anim_layout_listview"
        android:listSelector="#00000000">
</ListView>
</LinearLayout>
2.在layout目录下新建animal_item.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:padding="10dp">
    <TextView 
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="助人为乐"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    
<ImageButton
    android:id="@+id/btnSave"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/tvName"
    android:src="@drawable/btnsave"/>
</RelativeLayout>
3.新建类AnimalAdapter,代码如下:

public class AnimalAdapter extends ArrayAdapter<Animal>{
    private int resourceld;
    private Context context;
	public AnimalAdapter(Context context, int resource,List<Animal>objects) {
		super(context, resource,objects);
		this.context=context;
	    resourceld = resource;
		// TODO Auto-generated constructor stub
	}
     public View getView(int position,View convertView,ViewGroup parent){
		final Animal animal=getItem(position);//获取当前项的Animal实例
		View view;
		ViewHolder  viewHolder;
		if(convertView ==null){
			view=LayoutInflater.from(getContext()).inflate(resourceld, null);
            viewHolder=new ViewHolder();
            viewHolder.tvName=(TextView)view.findViewById(R.id.tvName);
            viewHolder.btnSave=(ImageButton)view.findViewById(R.id.btnSave);
            viewHolder.btnSave.setFocusable(false);
            viewHolder.btnSave.setFocusableInTouchMode(false);
            viewHolder.btnSave.setOnClickListener(new OnClickListener(){
            	public void onClick(View view){
            		Toast.makeText(context, "你要收藏"+animal.getName()+"吗",Toast.LENGTH_SHORT ).show();
            	}
            });
		    view.setTag(viewHolder);
		}else{
			view=convertView;
			viewHolder=(ViewHolder)view.getTag();
		}
		viewHolder.tvName.setText(animal.getName());
		
    	 return view;
    	 
     }
     class ViewHolder{
    	 TextView tvName;
    	 ImageButton btnSave;
     }
}

结果图如下:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值