自訂控制項之android列表分組及字母導航

來源:互聯網
上載者:User

標籤:android   listview   分組   索引   自訂控制項   

有了以上兩篇文章的重構,現在把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();          }}

此系列完畢。

自訂控制項之android列表分組及字母導航

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.