关于ExpandableListAdapter

来源:互联网 发布:图书管理系统php价格 编辑:程序博客网 时间:2024/06/05 06:36
package jannonx.com.myapplication;import android.database.DataSetObserver;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnChildClickListener;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    ExpandableListView listView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        listView = (ExpandableListView) findViewById(R.id.list);        final MyExpandableListAdapter adapter = new MyExpandableListAdapter();        listView.setAdapter(adapter);        //为ExpandableListView的子列表单击事件设置监听器        listView.setOnChildClickListener(new OnChildClickListener() {            @Override            public boolean onChildClick(ExpandableListView parent, View v,                                        int groupPosition, int childPosition, long id) {                // TODO Auto-generated method stub                Toast.makeText(MainActivity.this, "你单击了:"                        + adapter.getChild(groupPosition, childPosition), Toast.LENGTH_LONG).show();                return true;            }        });        //创建ExpandableListAdapter方式二//        ExpandableListAdapter adapter = new BaseExpandableListAdapter() {//            int[] logos = new int[]{//                    R.drawable.fw02,//                    R.drawable.fw05,//                    R.drawable.fw06,//                    R.drawable.fw07//            };//            private String[] armTypes = new String[]{//                    "WORD", "EXCEL", "EMAIL", "PPT"//            };//            private String[][] arms = new String[][]{//                    {"文档编辑", "文档排版", "文档处理", "文档打印"},//                    {"表格编辑", "表格排版", "表格处理", "表格打印"},//                    {"收发邮件", "管理邮箱", "登录登出", "注册绑定"},//                    {"演示编辑", "演示排版", "演示处理", "演示打印"},//            };////            @Override//            public int getGroupCount() {//                return armTypes.length;//            }////            @Override//            public int getChildrenCount(int groupPosition) {//                return arms[groupPosition].length;//            }////            @Override//            public Object getGroup(int groupPosition) {//                return armTypes[groupPosition];//            }////            @Override//            public Object getChild(int groupPosition, int childPosition) {//                return arms[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;//            }////            @Override//            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {//                LinearLayout ll = new LinearLayout(MainActivity.this);//                ll.setOrientation(LinearLayout.HORIZONTAL);//                ImageView logo = new ImageView(MainActivity.this);//                logo.setImageResource(logos[groupPosition]);//                logo.setPadding(36, 15, 0, 0);//                ll.addView(logo);//                TextView textView = getTextView();//                textView.setText(getGroup(groupPosition).toString());//                textView.setPadding(10, 0, 0, 0);//                ll.addView(textView);//                return ll;//            }////            @Override//            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {//                TextView textView = getTextView();//                textView.setText(getChild(groupPosition, childPosition).toString());//                return textView;//            }////            @Override//            public boolean isChildSelectable(int groupPosition, int childPosition) {//                return true;//            }////            @Override//            public boolean areAllItemsEnabled() {//                return false;//            }////            @Override//            public boolean isEmpty() {//                return false;//            }////            @Override//            public void onGroupExpanded(int groupPosition) {////            }////            @Override//            public void onGroupCollapsed(int groupPosition) {////            }////            @Override//            public long getCombinedChildId(long groupId, long childId) {//                return 0;//            }////            @Override//            public long getCombinedGroupId(long groupId) {//                return 0;//            }////            private TextView getTextView() {//                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);//                TextView textView = new TextView(MainActivity.this);//                textView.setLayoutParams(lp);//                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);//                textView.setPadding(36, 0, 0, 0);//                textView.setTextSize(20);//                return textView;//            }//        };//        listView.setAdapter(adapter);    }    class MyExpandableListAdapter implements ExpandableListAdapter {        int[] logos = new int[]{                R.drawable.fw02,                R.drawable.fw05,                R.drawable.fw06,                R.drawable.fw07        };        private String[] armTypes = new String[]{                "WORD", "EXCEL", "EMAIL", "PPT"        };        private String[][] arms = new String[][]{                {"文档编辑", "文档排版", "文档处理", "文档打印"},                {"表格编辑", "表格排版", "表格处理", "表格打印"},                {"收发邮件", "管理邮箱", "登录登出", "注册绑定"},                {"演示编辑", "演示排版", "演示处理", "演示打印"},        };        private int[][] armsImage=new int[][]{                {R.drawable.fw07,R.drawable.fw06,R.drawable.fw05,R.drawable.fw02,},                {R.drawable.fw07,R.drawable.fw06,R.drawable.fw05,R.drawable.fw02,},                {R.drawable.fw07,R.drawable.fw06,R.drawable.fw05,R.drawable.fw02,},                {R.drawable.fw07,R.drawable.fw06,R.drawable.fw05,R.drawable.fw02,},        };        @Override        public void registerDataSetObserver(DataSetObserver observer) {        }        @Override        public void unregisterDataSetObserver(DataSetObserver observer) {        }        @Override        public int getGroupCount() {            return armTypes.length;        }        @Override        public int getChildrenCount(int groupPosition) {            return arms[groupPosition].length;        }        @Override        public Object getGroup(int groupPosition) {            return armTypes[groupPosition];        }        @Override        public Object getChild(int groupPosition, int childPosition) {            return arms[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;        }        @Override        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {            LinearLayout ll = new LinearLayout(MainActivity.this);            ll.setOrientation(LinearLayout.HORIZONTAL);            ImageView logo = new ImageView(MainActivity.this);            logo.setImageResource(logos[groupPosition]);            logo.setPadding(36, 15, 0, 0);            ll.addView(logo);            TextView textView = getTextView();            textView.setText(getGroup(groupPosition).toString());            textView.setPadding(10, 0, 0, 0);            ll.addView(textView);            return ll;        }        @Override        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {            LinearLayout ll=new LinearLayout(MainActivity.this);            ll.setOrientation(LinearLayout.HORIZONTAL);            ImageView imageView=new ImageView(MainActivity.this);            imageView.setImageResource(armsImage[groupPosition][childPosition]);            imageView.setPadding(50,15,0,0);            ll.addView(imageView);            TextView textView = getTextView();            textView.setText(getChild(groupPosition, childPosition).toString());            textView.setPadding(5,0,0,0);            textView.setTextSize(18);            ll.addView(textView);            return ll;        }        @Override        public boolean isChildSelectable(int groupPosition, int childPosition) {            return true;        }        @Override        public boolean areAllItemsEnabled() {            return false;        }        @Override        public boolean isEmpty() {            return false;        }        @Override        public void onGroupExpanded(int groupPosition) {        }        @Override        public void onGroupCollapsed(int groupPosition) {        }        @Override        public long getCombinedChildId(long groupId, long childId) {            return 0;        }        @Override        public long getCombinedGroupId(long groupId) {            return 0;        }        private TextView getTextView() {            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);            TextView textView = new TextView(MainActivity.this);            textView.setLayoutParams(lp);            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);            textView.setPadding(36, 0, 0, 0);            textView.setTextSize(20);            return textView;        }    }}
0 0