自定義控件之android列表分組及字母導航

有了以上兩篇文章的重構,現在把ListView分組列表重構爲自定義控件就會非常簡單,只需要把初始化操作放在自定義控件的構造函數裏面。重構後的自定義控件以上一篇的註解重構爲基礎。

基本結構

這裏首先貼上一張上篇文章重構後的activity的代碼結構,相關的方法實現在之前兩篇文章中都有貼出。


再貼一張重構後的View的結構。可見兩者的結構都及其相似。不同的是上邊的activity中有抽象方法getDataList(),而下邊的沒有,但是多了一個ILoadRulerData<T> iLoadData 類型的接口。用於加載數據。


且自定義View中把activity 中initdata()方法改爲loaddata(),以供主動調用後開始加載數據。

重構後基本代碼如下,省略了一部分和之前重複的代碼:

首先是數據加載接口:

public interface ILoadRulerData<T> {
 
    public List<T> getDataList();
   
}


然後是自定義view的基本代碼:

/**  
 * @Description: 需要先調用 setiLoadData,設置獲取數據的接口,然後再調用  loadData方法
 */
public class RulerView<T> extends LinearLayout{
 
    private View baseView;
    private TextView noDataView;
   
    private TextView RulerTag;
    private ProgressBarWithText progress;
   
    private ListView listView;
   
    private RulerWidget ruler;
 
    private ILoadRulerData<T> iLoadData;
    private List<T> originalList;
    public  List<RulerListDataWrapper<T>> dealedList;
    private HashMap<String, Integer> tagLocation = new HashMap<String, Integer>();
    private RulerAdapter<T> rulerAdapter;
 
    public RulerView(Context context) {
       super(context);
       init( context);
    }
   
    public RulerView(Context context, AttributeSet attrs) {
       super(context, attrs);
       init( context);
    }
   
    public RulerView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs);
       init( context);
    }
 
   
    /**  
    * @Description: 設置數據加載接口
    * @param  
    * @return void
    */
    public void setiLoadData(ILoadRulerData<T> iLoadData) {
       this.iLoadData = iLoadData;
    }
 
    private void init(Context context){
       baseView = LayoutInflater.from(context).inflate(R.layout.g_ruler, null);
       noDataView = (TextView) baseView.findViewById(R.id.g_base_list_nodata);
       RulerTag = (TextView) baseView.findViewById(R.id.g_ruler_tag);
       progress = (ProgressBarWithText) baseView.findViewById(R.id.g_base_progressbar_withtext);
       listView = (ListView) baseView.findViewById(R.id.g_base_list);
       ruler = (RulerWidget) baseView.findViewById(R.id.g_ruler);
       progress.setVisibility(View.VISIBLE);
       RulerTag.setVisibility(View.GONE);
       noDataView.setVisibility(View.GONE);
       listView.setVisibility(View.GONE);
       ruler.setVisibility(View.GONE);
       addView(baseView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    }
   
 
    public void loadData(){
       new GetDataAsyTask().execute();
    }
   
    。。。。。
        }

測試

佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    
   <TextView 
       android:layout_marginTop="10dp"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="wbxtest view"
       android:textColor="@color/g_black"
       />
   <******.ruler.view.RulerView
       android:id="@+id/test_ruler_view_id"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       />
</LinearLayout>

Activity

public class TestViewActivity extends Activity{
 
   
   
    private RulerView<TestAnnotationBo> rulerView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.test_ruler_view);
        rulerView = (RulerView<TestAnnotationBo>) findViewById(R.id.test_ruler_view_id);
        rulerView.setiLoadData(new ILoadRulerData<TestAnnotationBo>() {
          
           @Override
           public List<TestAnnotationBo> getDataList() {
             
              return new TestAnnnoDao().getBoList();
           }
       });
        rulerView.loadData();
      
    }
}

此係列完畢。

發佈了35 篇原創文章 · 獲贊 18 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章