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

来源:互联网 发布:查看电脑的mac地址 编辑:程序博客网 时间:2024/04/28 05:28

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;     }}

结果图如下:





0 0
原创粉丝点击