控件:
package com.ejoy.customcontrol;
import com.ejoy.adpter.AdapterForLinearLayout;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
public class LinearLayoutForListView extends LinearLayout {
private AdapterForLinearLayout adapter;
private OnClickListener onClickListener = null;
/**
* 绑定布局
*/
public void bindLinearLayout() {
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
View v = adapter.getView(i, null, null);
v.setOnClickListener(this.onClickListener);
/*if (i == count - 1) {
LinearLayout ly = (LinearLayout) v;
ly.removeViewAt(2);
}*/
addView(v, i);
}
Log.v("countTAG", "" + count);
}
public LinearLayoutForListView(Context context) {
super(context);
}
public LinearLayoutForListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 获取Adapter
*
* @return adapter
*/
public AdapterForLinearLayout getAdpater() {
return adapter;
}
/**
* 设置数据
*
* @param adpater
*/
public void setAdapter(AdapterForLinearLayout adpater) {
this.adapter = adpater;
bindLinearLayout();
}
/**
* 获取点击事件
*
* @return
*/
public OnClickListener getOnclickListner() {
return onClickListener;
}
/**
* 设置点击事件
*
* @param onClickListener
*/
public void setOnclickLinstener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
}
适配器:
package com.ejoy.adpter;
import java.util.List;
import java.util.Map;
import com.ejoy.adpter.AsyncBitmapLoader.ImageCallBack;
import com.ejoy.android.R;
import com.ejoy.utils.EjoyLog;
import com.ejoy.utils.StringUtils;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class AdapterForLinearLayout extends BaseAdapter {
private LayoutInflater mInflater;
private int resource;
private List<? extends Map<String, ?>> data;
private String[] from;
private int[] to;
private Context mContext;
AsyncBitmapLoader asyncBitmapLoader = new AsyncBitmapLoader();
public AdapterForLinearLayout(Context context,
List<? extends Map<String, ?>> data, int resouce, String[] from,
int[] to) {
this.data = data;
this.resource = resouce;
this.data = data;
this.from = from;
this.to = to;
this.mContext = context;
this.mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
@SuppressWarnings("unchecked")
public String get(int position, Object key) {
Map<String, ?> map = (Map<String, ?>) getItem(position);
return map.get(key).toString();
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(resource, null);
Map<String, ?> item = data.get(position);
int count = to.length;
for (int i = 0; i < count; i++) {
View v = convertView.findViewById(to[i]);
bindView(v, item, from[i]);
}
convertView.setTag(position);
return convertView;
}
/**
* 绑定视图
*
* @param view
* @param item
* @param from
*/
private void bindView(View view, Map<String, ?> item, String from) {
Object data = item.get(from);
if (view instanceof ImageView) {
try {
asyncBitmapLoader.loadBitmap((ImageView) view,
data == null ? "" : data.toString(),
new ImageCallBack() {
public void imageLoad(ImageView imageView,
Bitmap bitmap) {
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
imageView
.setImageResource(R.drawable.defaultperson);
}
}
});
} catch (Exception e) {
new EjoyLog(mContext).error(this.getClass().getName()
.toString(), StringUtils.FormatStackTrace(e));
}
} else if (view instanceof TextView) {
((TextView) view).setText(data == null ? "" : data.toString());
}
}
}
使用:
Layoutadpater = new AdapterForLinearLayout(
FrmHomeFoodActivity.this, oldEjoyNewsArrayList,
R.layout.frmfoodtest,
new String[] { listViewItemString },
new int[] { R.id.TextView0002 });
lvListView.setAdapter(Layoutadpater);