亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android使用ExpandableListView控件實現(xiàn)小說目錄效果的例子

 更新時間:2014年07月10日 09:20:59   投稿:junjie  
這篇文章主要介紹了android使用ExpandableListView控件實現(xiàn)小說目錄效果的例子,還可以實現(xiàn)二級列表展示效果,需要的朋友可以參考下

今天給大家講講android的目錄實現(xiàn)方法,就像大家看到的小說目錄一樣,android 提供了ExpandableListView控件可以實現(xiàn)二級列表展示效果,現(xiàn)在給大家講講這個控件的用法,下面是XML定義:

復制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFFFFF"
  >
  <ExpandableListView
       android:id="@+id/elv_journal_catalog"
       android:layout_height="fill_parent"
       android:layout_width="fill_parent"
       android:cacheColorHint="#FFFFFF"
       />
</LinearLayout>

這代碼很簡單,和寫listView的方法差不多,接下來是ExpandableListView在activity中的代碼:
復制代碼 代碼如下:

 private ExpandableListView elv_journal_catalog;
    private List<List<Article>> childrenObj;
         private JournalCatalogListAdapter adapter;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.journal_catalog);
        init();
        elv_journal_catalog.setGroupIndicator(null);
        elv_journal_catalog.setDivider(null);

        loadData();
    }

    private void init() {
        elv_journal_catalog = (ExpandableListView) findViewById(R.id.elv_journal_catalog);
        elv_journal_catalog.setOnChildClickListener(listener);
    }
    private void loadData() {
        Message msg = handler.obtainMessage();
        msg.what = 1;
        msg.sendToTarget();

        childrenObj = new ArrayList<List<Article>>();
        new Thread() {

            @Override
            public void run() {
                if (!isLoading) {
                    queryArticleList();
                } else {
                    queryArticleListFromSqlite();
                }
            }

        }.start();

        adapter = new JournalCatalogListAdapter(this, childrenObj);
        elv_journal_catalog.setAdapter(adapter);
    }

ExpandableListView展示數(shù)據(jù)的時候默認是每個模塊下的列表項是閉合狀態(tài)的,如果要實現(xiàn)初始化的時候就展開可以通過ExpandableListView.expandGroup(location)方法來實現(xiàn),而且每個父級列表項左邊會出現(xiàn)一個系統(tǒng)自帶的圖標,這個圖標是用來表示列表展開和閉合的狀態(tài)的,如果不顯示或者要替換這個圖標可以用
ExpandableListView.setGroupIndicator(Drawable icon)方法來實現(xiàn),我這里是直接是沒有使用任何圖標,你也可以在adapter中自己在xml中定義自己的圖標.
ExpandableListView填充數(shù)據(jù)需要是二級菜單的模式所以數(shù)據(jù)結(jié)構大家可以根據(jù)項目情況而定,我這里由于標題是定死的所以只傳的每個標題下的數(shù)據(jù),下面是JournalCatalogListAdapter的代碼:

復制代碼 代碼如下:

public class JournalCatalogListAdapter extends BaseExpandableListAdapter {

    private LayoutInflater inflater;

    private String[] parent = new String[] { "美顏美體", "潮流單品", "娛樂八卦", "情感",
            "觀點", "健康生活" };

    private List<List<Article>> clildren = new ArrayList<List<Article>>();

    public JournalCatalogListAdapter(Context context,
            List<List<Article>> clildren) {
        this.clildren = clildren;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return clildren.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(
                    R.layout.journal_catalog_list_item_content, null);
        }
        TextView textView = (TextView) convertView
                .findViewById(R.id.tv_journal_catalog_list_item_content);
        Article a = (Article) getChild(groupPosition, childPosition);
        textView.setText(a.getTitle());
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return clildren.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parent[groupPosition];
    }

    @Override
    public int getGroupCount() {
        return parent.length;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(
                    R.layout.journal_catalog_list_item_title, null);
        }
        TextView textView = (TextView) convertView
                .findViewById(R.id.tv_journal_catalog_list_item_title);

        String title = String.valueOf(getGroup(groupPosition));
        textView.setText(title);
        convertView.setOnClickListener(null);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

相關文章

最新評論