标签:android style blog io os 使用 java ar for
尽管网上有很多实现方式,但是还是想分享下,已做备忘,实现方式时,通过linearlayout来代替listview
废话少说,上代码:
ExpandListView.java:
public class ExpandListView extends LinearLayout implements OnClickListener { private ListAdapter adapter = null; private OnItemClickListener listener = null; private Drawable dividerDrawable = null; private int dividerHeight = 0; public ExpandListView(Context context) { super(context); // TODO Auto-generated constructor stub initView(null); } public ExpandListView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub initView(attrs); } public void initView(AttributeSet attrs) { setOrientation(VERTICAL); if(attrs != null) { TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.ExpandListView); dividerDrawable = array.getDrawable(R.styleable.ExpandListView_divider_view); dividerHeight = array.getDimensionPixelSize(R.styleable.ExpandListView_divider_height, 0); array.recycle(); } } /** * 绑定数据 */ public void bindData() { if(adapter == null) { Logger.e("adapter is null"); return ; } View convertView = null; LayoutParams params = null; for(int i = 0; i < adapter.getCount(); i++) { //如果这儿不传this(parent)传null的话,adapter在inflate时,parent为null,这样convertView的LayoutParams为空(需要父控件进行测量) convertView = adapter.getView(i, null, this); if(convertView.getLayoutParams() == null) { params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); } else { params = (LayoutParams) convertView.getLayoutParams(); } convertView.setLayoutParams(params); convertView.setId(i); addDivider(i); if(listener != null) { convertView.setOnClickListener(this); } addView(convertView); } } @SuppressWarnings("deprecation") public void addDivider(int position) { if((dividerDrawable != null || dividerHeight != 0) && position != 0) { //添加divider ImageView iv = new ImageView(getContext()); LayoutParams params = null; if(dividerHeight != 0) { params = new LayoutParams(LayoutParams.MATCH_PARENT, dividerHeight); } else { params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); } iv.setLayoutParams(params); iv.setBackgroundDrawable(dividerDrawable); addView(iv); } } /** * 设置adapter * @param adapter */ public void setAdapter(ListAdapter adapter) { this.adapter = adapter; if(getChildCount() != 0) { removeAllViews(); } bindData(); } @Override public void onClick(View v) { // TODO Auto-generated method stub listener.onItemClick(v, v.getId(), adapter); } public void setOnItemClickListener(OnItemClickListener listener) { this.listener = listener; } public interface OnItemClickListener { public void onItemClick(View convertView, int position, ListAdapter adapter); } }
attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="ExpandListView"> <attr name="divider_view" format="reference" /> <attr name="divider_height" format="dimension" /> </declare-styleable> </resources>简单使用:
<com.ydk.mikeadmin.view.ExpandListView android:id="@+id/part_one_list" android:layout_width="match_parent" android:layout_height="wrap_content" mike:divider_height="2dip" />
额,就这么多...只是这里的convertView没有复用起来,暂时还没有想到什么方法去复用
标签:android style blog io os 使用 java ar for
原文地址:http://blog.csdn.net/swust_chenpeng/article/details/39341069