View之ExpandableLists

ExpandableLists

2016/2/1 10:16:27

1. Custom Adapter

实现

    public class Expandable1 extends ExpandableListActivity {


        private MyExpandableListAdapter mAdapter;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            mAdapter = new MyExpandableListAdapter();
            setListAdapter(mAdapter);
         //   registerForContextMenu(getExpandableListView());


        }

        /**
         * 监听条目选择事件
         * @param parent
         * @param v
         * @param groupPosition
         * @param childPosition
         * @param id
         * @return
         */
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

            Toast.makeText(this,groupPosition+";"+childPosition,Toast.LENGTH_SHORT).show();

            return super.onChildClick(parent, v, groupPosition, childPosition, id);
        }

        public class MyExpandableListAdapter extends BaseExpandableListAdapter {

            // Sample data set.  children[i] contains the children (String[]) for groups[i].
            private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
            private String[][] children = {
                    { "Arnold", "Barry", "Chuck", "David" },
                    { "Ace", "Bandit", "Cha-Cha", "Deuce" },
                    { "Fluffy", "Snuggles" },
                    { "Goldy", "Bubbles" }
            };

            /**
             * Group数量
             * @return
             */
            @Override
            public int getGroupCount() {
                return groups.length;
            }

            /**
             * 子条目数量
             * @param groupPosition
             * @return
             */
            @Override
            public int getChildrenCount(int groupPosition) {
                return children[groupPosition].length;
            }

            /**
             * Group条目
             * @param groupPosition
             * @return
             */
            @Override
            public Object getGroup(int groupPosition) {
                return groups[groupPosition];
            }

            /**
             * 获取子条目
             * @param groupPosition
             * @param childPosition
             * @return
             */
            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return children[groupPosition][childPosition];
            }

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

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

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

            /**
             * Group条目视图
             * @param groupPosition
             * @param isExpanded
             * @param convertView
             * @param parent
             * @return
             */
            @Override
            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                TextView textView = getGenericView();
                textView.setText(getGroup(groupPosition).toString());
                return textView;
            }

            /**
             * 子条目视图
             * @param groupPosition
             * @param childPosition
             * @param isLastChild
             * @param convertView
             * @param parent
             * @return
             */
            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                TextView textView = getGenericView();
                textView.setText(getChild(groupPosition, childPosition).toString());
                return textView;
            }

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


            public TextView getGenericView() {
                // Layout parameters for the ExpandableListView
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, 64);

                TextView textView = new TextView(Expandable1.this);
                textView.setLayoutParams(lp);
                // Center the text vertically
                textView.setGravity(Gravity.CENTER_VERTICAL);
                // Set the text starting position
                textView.setPaddingRelative(36, 0, 0, 0);
                // Set the text alignment
                textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                return textView;
            }
        }
    }

效果

这里写图片描述

2. Cursor

实现

    /**
     * Demonstrates expandable lists backed by Cursors
     */
    public class ExpandableList2 extends ExpandableListActivity {

        private static final String[] CONTACTS_PROJECTION = new String[] {
            Contacts._ID,
            Contacts.DISPLAY_NAME
        };
        private static final int GROUP_ID_COLUMN_INDEX = 0;

        private static final String[] PHONE_NUMBER_PROJECTION = new String[] {
                Phone._ID,
                Phone.NUMBER
        };

        private static final int TOKEN_GROUP = 0;
        private static final int TOKEN_CHILD = 1;

        private static final class QueryHandler extends AsyncQueryHandler {
            private CursorTreeAdapter mAdapter;

            public QueryHandler(Context context, CursorTreeAdapter adapter) {
                super(context.getContentResolver());
                this.mAdapter = adapter;
            }

            @Override
            protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
                switch (token) {
                case TOKEN_GROUP:
                    mAdapter.setGroupCursor(cursor);
                    break;

                case TOKEN_CHILD:
                    int groupPosition = (Integer) cookie;
                    mAdapter.setChildrenCursor(groupPosition, cursor);
                    break;
                }
            }
        }

        public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

            // Note that the constructor does not take a Cursor. This is done to avoid querying the 
            // database on the main thread.
            public MyExpandableListAdapter(Context context, int groupLayout,
                    int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
                    int[] childrenTo) {

                super(context, null, groupLayout, groupFrom, groupTo, childLayout, childrenFrom,
                        childrenTo);
            }

            @Override
            protected Cursor getChildrenCursor(Cursor groupCursor) {
                // Given the group, we return a cursor for all the children within that group 

                // Return a cursor that points to this contact's phone numbers
                Uri.Builder builder = Contacts.CONTENT_URI.buildUpon();
                ContentUris.appendId(builder, groupCursor.getLong(GROUP_ID_COLUMN_INDEX));
                builder.appendEncodedPath(Contacts.Data.CONTENT_DIRECTORY);
                Uri phoneNumbersUri = builder.build();

                mQueryHandler.startQuery(TOKEN_CHILD, groupCursor.getPosition(), phoneNumbersUri, 
                        PHONE_NUMBER_PROJECTION, Phone.MIMETYPE + "=?", 
                        new String[] { Phone.CONTENT_ITEM_TYPE }, null);

                return null;
            }
        }

        private QueryHandler mQueryHandler;
        private CursorTreeAdapter mAdapter;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Set up our adapter
            mAdapter = new MyExpandableListAdapter(
                    this,
                    android.R.layout.simple_expandable_list_item_1,
                    android.R.layout.simple_expandable_list_item_1,
                    new String[] { Contacts.DISPLAY_NAME }, // Name for group layouts
                    new int[] { android.R.id.text1 },
                    new String[] { Phone.NUMBER }, // Number for child layouts
                    new int[] { android.R.id.text1 });

            setListAdapter(mAdapter);

            mQueryHandler = new QueryHandler(this, mAdapter);

            // Query for people
            mQueryHandler.startQuery(TOKEN_GROUP, null, Contacts.CONTENT_URI, CONTACTS_PROJECTION, 
                    Contacts.HAS_PHONE_NUMBER + "=1", null, null);
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();

            // Null out the group cursor. This will cause the group cursor and all of the child cursors
            // to be closed.
            mAdapter.changeCursor(null);
            mAdapter = null;
        }
    }

效果

这里写图片描述

3. Simple Adapter

实现


    /**
     * Demonstrates expandable lists backed by a Simple Map-based adapter
     */
    public class ExpandableList3 extends ExpandableListActivity {
        private static final String NAME = "NAME";
        private static final String IS_EVEN = "IS_EVEN";

        private ExpandableListAdapter mAdapter;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
            List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
            for (int i = 0; i < 20; i++) {
                Map<String, String> curGroupMap = new HashMap<String, String>();
                groupData.add(curGroupMap);
                curGroupMap.put(NAME, "Group " + i);
                curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");

                List<Map<String, String>> children = new ArrayList<Map<String, String>>();
                for (int j = 0; j < 15; j++) {
                    Map<String, String> curChildMap = new HashMap<String, String>();
                    children.add(curChildMap);
                    curChildMap.put(NAME, "Child " + j);
                    curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
                }
                childData.add(children);
            }

            // Set up our adapter
            mAdapter = new SimpleExpandableListAdapter(
                    this,
                    groupData,
                    android.R.layout.simple_expandable_list_item_1,
                    new String[] { NAME, IS_EVEN },
                    new int[] { android.R.id.text1, android.R.id.text2 },
                    childData,
                    android.R.layout.simple_expandable_list_item_2,
                    new String[] { NAME, IS_EVEN },
                    new int[] { android.R.id.text1, android.R.id.text2 }
                    );
            setListAdapter(mAdapter);
        }

    }

效果

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值