Android自定义View及数据适配

1. 编写自定义view的layout xml文件

例如:my_view.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
	android:id="@+id/person"
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
    <ImageView    
		android:id="@+id/portrail"
        android:layout_width="100dip"   
        android:layout_height="100dip"  
        android:layout_gravity="bottom|center_horizontal"  
        android:gravity="center|bottom"  
        android:background="#00FF00"  
        />  
  
    <TextView  
		android:id="@+id/name"
		android:layout_width="100dip"  
        android:layout_height="100dip"  
        android:layout_gravity="bottom|left"  
        android:gravity="left|top"  
        android:background="#FF0000"  
        android:text="@string/name"  
        />  
</LinearLayout>  



2. 编写View类extends LinearLayout

例如:MyView.java

public class MyView extends LinearLayout implements android.content.DialogInterface.OnClickListener{

	protected ImageView portrail;	
    	TextView name = null;
	
	public MyView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		LayoutInflater.from(context).inflate(R.layout.my_view, this, true);  
		portrail = (ImageView) findViewById(R.id.portrail);
		name = (TextView) findViewById(R.id.name);
	}
	public void setPortrail(Bitmap bmp){
		portrail.setImageBitmap(bmp);
	}
		
	public void setName(String n){
		name.setText(n);	
	}

	@Override
	public void onClick(DialogInterface arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
}


3. 编写adapter

例如:MyAdapter.java

public class MyAdapter extends BaseAdapter{

	ArrayList<Expert> mLists = new ArrayList<Expert>();
	
	private Context mContext = null;
	
	public void append(ArrayList<Expert> mLists){
		mLists.addAll(mLists);
		notifyDataSetChanged();
	}

	public MyAdapter(Context context) {
		// TODO Auto-generated constructor stub
		super();
		mContext = context;
	}
		
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mLists.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		
		return mLists.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		MyView mv = null;
		
		if(arg1 == null){
			mv = new MyView(mContext);
		}else{
			mv = (MyView)arg1;
		}
		
		String n = ;
		
		mv.setPortrail(mLists.get(arg0).portrail)
		mv.setName(mLists.get(arg0).name);
		
		return mv;
	}	
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值