1. 程式人生 > >一段ExpandableListView應用,把它寫成函數了

一段ExpandableListView應用,把它寫成函數了

public void Return(){

		
		final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {

        	public String[] groups = {"所獲證書","2013優秀畢業設計","英語4級" };
     
            public int[][] children2 = new int[][] {
                    { R.drawable.a},
                    { R.drawable.b},
                    { R.drawable.c},};
            
            public Object getChild(int groupPosition, int childPosition) {
                return children2[groupPosition][childPosition];
            }
       
            
            public long getChildId(int groupPosition, int childPosition) {
                return childPosition;
            }

            public int getChildrenCount(int groupPosition) {
                return children2[groupPosition].length;
            }

            public TextView getGenericView() {
                // Layout parameters for the ExpandableListView
                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);
                textView.setPadding(36, 0, 0, 0);
                textView.setTextColor(Color.BLACK);
                textView.setTextSize(18);
                return textView;
            }
            
            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);
                textView.setPadding(36, 0, 0, 0);
                textView.setTextSize(20);
                textView.setTextColor(Color.BLACK);
                return textView;
            }
            
            public View getChildView(int groupPosition, int childPosition,
                    boolean isLastChild, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                LinearLayout ll = new LinearLayout(
                		MainActivity.this);
                ll.setOrientation(0);
                ImageView generallogo = new ImageView(
                		MainActivity.this);
                generallogo
                        .setImageResource(children2[groupPosition][childPosition]);
                ll.addView(generallogo);
                TextView textView = getTextView();
                textView.setText(getChild(groupPosition, childPosition)
                        .toString());
               // ll.addView(textView);
                return ll;
            }


            public Object getGroup(int groupPosition) {
                return groups[groupPosition];
            }

            public int getGroupCount() {
                return groups.length;
            }

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

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                    View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                LinearLayout ll = new LinearLayout(
                		MainActivity.this);
                ll.setOrientation(0);

                TextView textView = getGenericView();
                textView.setTextColor(Color.BLACK);
                textView.setText(getGroup(groupPosition).toString());
                ll.addView(textView);

                return ll;
            }

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

            public boolean hasStableIds() {
                return true;
            }
        
        };
        ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
        expandableListView.setAdapter(adapter);
        //設定item點選的監聽器
        expandableListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
            
                Toast.makeText(
                		MainActivity.this,
                        "你點選了" + adapter.getChild(groupPosition, childPosition),
                        Toast.LENGTH_SHORT).show();

                return false;
            }
        });
	}