- Android自定义适配器使用(Adaper)
- 这是和Activity对应的布局文件,包含ListView,名字:showmenu.xml
- <</span>RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#86818181"
- android:layout_marginLeft="0dp"
- android:layout_marginRight="0dp"
- >
- <</span>RelativeLayout
- android:layout_width="@dimen/view_200padd"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:background="@drawable/listviewbg"
- android:padding="@dimen/view_4padd"
- >
- <</span>ListView
- android:id="@+id/menuListShow"
- android:layout_marginTop="@dimen/view_15padd"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:footerDividersEnabled="true"
- android:dividerHeight="1px"
- >
- </</span>ListView>
- </</span>RelativeLayout>
- </</span>RelativeLayout>
- 这是每个Item对应的布局文件,它里面可以包含多个自己需要放置的组件,名字:itemvo_item.xml
- <</span>RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="@dimen/view_65padd"
- android:descendantFocusability="blocksDescendants">
- <</span>ImageView
- android:id="@+id/imgIcon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_centerVertical="true"
- android:layout_marginLeft="@dimen/view_6padd"/>
- <</span>TextView
- android:id="@+id/itemName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/imgIcon"
- android:layout_marginLeft="@dimen/view_10padd"
- android:layout_centerVertical="true"
- android:textColor="@color/widht"
- android:textSize="@dimen/view_18padd"/>
- <</span>ImageView
- android:id="@+id/rigthimg"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_marginRight="@dimen/view_6padd"
- android:layout_centerVertical="true"/>
- </</span>RelativeLayout>
- 这是和itemvo_item.xml对应的对象,当然也可以是其他集合,数组,只要能给布局文件提供数据,什么都行,名字:Itemvo
- import java.util.ArrayList;
- import java.util.List;
- import com.sake.base.R;
- public class Itemvo {
- private int iconid;//显示的图标
- private String strView;//显示的文字
- private int lasticon;//最后的图片或图标
- public int getIconid() {
- return iconid;
- }
- public void setIconid(int iconid) {
- this.iconid = iconid;
- }
- public String getStrView() {
- return strView;
- }
- public void setStrView(String strView) {
- this.strView = strView;
- }
- public int getLasticon() {
- return lasticon;
- }
- public void setLasticon(int lasticon) {
- this.lasticon = lasticon;
- }
- public List getMenuItem(){
- List lists=new ArrayList();
- Itemvo vo=new Itemvo();
- vo.setIconid(R.drawable.saomiao);
- vo.setStrView("扫一扫");
- vo.setLasticon(R.drawable.right_flag);
- lists.add(vo);
- Itemvo vo2=new Itemvo();
- vo2.setIconid(R.drawable.neworder);
- vo2.setStrView("新建订单");
- vo2.setLasticon(R.drawable.right_flag);
- lists.add(vo2);
- return lists;
- }
- }
- 这是最终的适配器类,传递参数进行封装,就可以使用了,名字:MenuAdaptor
- import java.util.List;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.sake.base.R;
- import com.sake.vo.Itemvo;
- public class MenuAdaptor extends BaseAdapter{
- ListlistItemvos;
- Context context;
- public MenuAdaptor(List listItemvos, Context context) {
- super();
- this.listItemvos = listItemvos;
- this.context = context;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return listItemvos.size();
- }
- @Override
- public Object getItem(int arg0) {
- return listItemvos.get(arg0);
- }
- @Override
- public long getItemId(int arg0) {
- return arg0;
- }
- @Override
- public View getView(int arg0, View arg1, ViewGroup arg2) {
- LayoutInflater inflater=LayoutInflater.from(context);
- View view=inflater.inflate(R.layout.itemvo_item, null);
- ImageView imgIcon=(ImageView) view.findViewById(R.id.imgIcon);
- TextView itemName=(TextView) view.findViewById(R.id.itemName);
- ImageView rigthimg=(ImageView) view.findViewById(R.id.rigthimg);
- imgIcon.setImageResource(listItemvos.get(arg0).getIconid());
- itemName.setText(listItemvos.get(arg0).getStrView());
- rigthimg.setImageResource(listItemvos.get(arg0).getLasticon());
- return view;
- }
- }
- 如何使用,看这里:
- Itemvo item=new Itemvo();
- ListView listv=(ListView) customView.findViewById(R.id.menuListShow);
- listv.setFocusable(true);
- //初始化菜单
- listv.setAdapter(new MenuAdaptor(item.getMenuItem(), OrderMsg_C.this));