首页 > 移动开发 > ScrollView中嵌套listview

ScrollView中嵌套listview

尽管网上有很多实现方式,但是还是想分享下,已做备忘,实现方式时,通过linearlayout来代替listview

废话少说,上代码:

ExpandListView.java:

[java]

  1. public class ExpandListView extends LinearLayout implements OnClickListener {  
  2.   
  3.     private ListAdapter adapter = null;  
  4.     private OnItemClickListener listener = null;  
  5.     private Drawable dividerDrawable = null;  
  6.     private int dividerHeight = 0;  
  7.       
  8.     public ExpandListView(Context context) {  
  9.         super(context);  
  10.         // TODO Auto-generated constructor stub  
  11.         initView(null);  
  12.     }  
  13.     public ExpandListView(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.         // TODO Auto-generated constructor stub  
  16.         initView(attrs);  
  17.     }  
  18.       
  19.     public void initView(AttributeSet attrs) {  
  20.         setOrientation(VERTICAL);   
  21.         if(attrs != null) {  
  22.             TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.ExpandListView);  
  23.             dividerDrawable = array.getDrawable(R.styleable.ExpandListView_divider_view);  
  24.             dividerHeight = array.getDimensionPixelSize(R.styleable.ExpandListView_divider_height, 0);  
  25.             array.recycle();  
  26.         }  
  27.     }  
  28.       
  29.     /** 
  30.      * 绑定数据 
  31.      */  
  32.     public void bindData() {  
  33.         if(adapter == null) {  
  34.             Logger.e("adapter is null");  
  35.             return ;  
  36.         }  
  37.         View convertView = null;  
  38.         LayoutParams params = null;  
  39.         for(int i = 0; i < adapter.getCount(); i++) {  
  40.             //如果这儿不传this(parent)传null的话,adapter在inflate时,parent为null,这样convertView的LayoutParams为空(需要父控件进行测量)  
  41.             convertView = adapter.getView(i, nullthis);  
  42.             if(convertView.getLayoutParams() == null) {  
  43.                 params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);  
  44.             } else {  
  45.                 params = (LayoutParams) convertView.getLayoutParams();  
  46.             }  
  47.             convertView.setLayoutParams(params);  
  48.             convertView.setId(i);  
  49.             addDivider(i);  
  50.             if(listener != null) {  
  51.                 convertView.setOnClickListener(this);  
  52.             }  
  53.             addView(convertView);  
  54.         }  
  55.     }  
  56.       
  57.     @SuppressWarnings("deprecation")  
  58.     public void addDivider(int position) {  
  59.         if((dividerDrawable != null || dividerHeight != 0) && position != 0) {  
  60.             //添加divider  
  61.             ImageView iv = new ImageView(getContext());  
  62.             LayoutParams params = null;  
  63.             if(dividerHeight != 0) {  
  64.                 params = new LayoutParams(LayoutParams.MATCH_PARENT, dividerHeight);  
  65.             } else {  
  66.                 params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);  
  67.             }  
  68.             iv.setLayoutParams(params);  
  69.             iv.setBackgroundDrawable(dividerDrawable);  
  70.             addView(iv);  
  71.         }   
  72.     }  
  73.       
  74.     /** 
  75.      * 设置adapter 
  76.      * @param adapter 
  77.      */  
  78.     public void setAdapter(ListAdapter adapter) {  
  79.         this.adapter = adapter;  
  80.         if(getChildCount() != 0) {  
  81.             removeAllViews();  
  82.         }  
  83.         bindData();  
  84.     }  
  85.     @Override  
  86.     public void onClick(View v) {  
  87.         // TODO Auto-generated method stub  
  88.         listener.onItemClick(v, v.getId(), adapter);  
  89.     }  
  90.       
  91.     public void setOnItemClickListener(OnItemClickListener listener) {  
  92.         this.listener = listener;  
  93.     }  
  94.       
  95.     public interface OnItemClickListener {  
  96.         public void onItemClick(View convertView, int position, ListAdapter adapter);  
  97.     }  
  98. }  

attrs.xml

[java]

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <declare-styleable name="ExpandListView">  
  5.         <attr name="divider_view" format="reference" />  
  6.         <attr name="divider_height" format="dimension" />  
  7.     </declare-styleable>  
  8.   
  9. </resources>  

简单使用:

[html]

  1. <com.ydk.mikeadmin.view.ExpandListView  
  2.        android:id="@+id/part_one_list"  
  3.        android:layout_width="match_parent"  
  4.        android:layout_height="wrap_content"  
  5.        mike:divider_height="2dip" />  


然后就直接在代码中,用ExpandListView.setAdapter设置下adapter就行了

额,就这么多...只是这里的convertView没有复用起来,暂时还没有想到什么方法去复用


本文固定链接: http://www.devba.com/index.php/archives/5948.html | 开发吧

报歉!评论已关闭.