'[안드로이드] ExpandableListView'에 해당되는 글 2건

  1. 2011.10.31 [안드로이드] ExpandableListView - 리스트 안에 리스트 (2)
  2. 2011.10.27 [안드로이드] ExpandableListView listview안에 listview을
  


Activity.class

package kr.co.test2;
 
import java.util.ArrayList;
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
 
public class test2 extends Activity {
 
private ArrayList<String> groups;
private ArrayList<ArrayList<ArrayList<String>>> childs;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.main);
 
        ExpandableListView l = (ExpandableListView) findViewById(R.id.ExpandableListView01);
        
        l .setGroupIndicator(null);
        
        loadData();
 
        myExpandableAdapter adapter = new myExpandableAdapter(this, groups, childs);
l.setAdapter(adapter);
    }
 
    public class myExpandableAdapter extends BaseExpandableListAdapter {
 
    private ArrayList<String> groups;
 
        private ArrayList<ArrayList<ArrayList<String>>> children;
 
    private Context context;
 
    public myExpandableAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<ArrayList<String>>> children) {
            this.context = context;
            this.groups = groups;
            this.children = childs;
        }
 
 
    @Override
        public boolean areAllItemsEnabled()
        {
            return true;
        }
 
 
        @Override
        public ArrayList<String> getChild(int groupPosition, int childPosition) {
            return children.get(groupPosition).get(childPosition);
        }
 
        //Action되는곳
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            Log.v("부모Position", Integer.toString(groupPosition));
            Log.v("자식Position", Integer.toString(childPosition));
            return childPosition;

        //    
        }
 
 
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {
 
        String child = (String) ((ArrayList<String>)getChild(groupPosition, childPosition)).get(0);
 
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expandablelistview_child, null);
            }
 
            TextView childtxt = (TextView) convertView.findViewById(R.id.TextViewChild01);
 
            childtxt.setText(child);        
 
            return convertView;
        }
 
        @Override
        public int getChildrenCount(int groupPosition) {
            return children.get(groupPosition).size();
        }
 
        @Override
        public String getGroup(int groupPosition) {
            return groups.get(groupPosition);
        }
 
        @Override
        public int getGroupCount() {
            return groups.size();
        }
 
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
 
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
 
        String group = (String) getGroup(groupPosition);
 
        if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expandablelistview_group, null);
            }
 
            TextView grouptxt = (TextView) convertView.findViewById(R.id.TextViewGroup);
 
            grouptxt.setText(group);
 
            return convertView;
        }
 
        @Override
        public boolean hasStableIds() {
            return true;
        }
 
        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }
 
    }
 
    private void loadData(){
    groups= new ArrayList<String>();
    childs= new ArrayList<ArrayList<ArrayList<String>>>();
 
    groups.add("Group 1");
        groups.add("Group 2");
        groups.add("Group 3");
        groups.add("Group 4");
        groups.add("Group 5");
 
        childs.add(new ArrayList<ArrayList<String>>());       
        childs.get(0).add(new ArrayList<String>());
        childs.get(0).get(0).add("Child 1 group 1");
        childs.get(0).add(new ArrayList<String>());
        childs.get(0).get(1).add("Child 2 group 1");
        childs.get(0).add(new ArrayList<String>());
        childs.get(0).get(2).add("Child 3 group 1");
 
        childs.add(new ArrayList<ArrayList<String>>());
        childs.get(1).add(new ArrayList<String>());
        childs.get(1).get(0).add("Child 1 group 2");
        childs.get(1).add(new ArrayList<String>());
        childs.get(1).get(1).add("Child 2 group 2");
        
 
        childs.add(new ArrayList<ArrayList<String>>());
        childs.get(2).add(new ArrayList<String>());
        childs.get(2).get(0).add("Child 1 group 3");
        childs.get(2).add(new ArrayList<String>());
        childs.get(2).get(1).add("Child 2 group 3");
        childs.get(2).add(new ArrayList<String>());
        childs.get(2).get(2).add("Child 3 group 3");
        
        childs.add(new ArrayList<ArrayList<String>>());
        
        childs.add(new ArrayList<ArrayList<String>>());
        childs.get(4).add(new ArrayList<String>());
        childs.get(4).get(0).add("Child 1 group 3");
        childs.get(4).add(new ArrayList<String>());
        childs.get(4).get(1).add("Child 2 group 3");
        childs.get(4).add(new ArrayList<String>());
        childs.get(4).get(2).add("Child 3 group 3");
        
    }
}



expandablelistview_child.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="96px"
android:background="@drawable/bg_mlist_2depth1"
>
 
<TextView
android:id="@+id/TextViewChild01" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30px"
android:layout_gravity="center_vertical|center_horizontal"
>
</TextView>
 
<TextView
android:id="@+id/TextViewChild02" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
>
</TextView>
 
<TextView
android:id="@+id/TextViewChild03" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
>
</TextView>
 
</LinearLayout>

 

expandablelistview_group.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:background="@drawable/bg_mlist_1depth_on1"
>
 
<TextView
android:id="@+id/TextViewGroup" 
android:layout_width="wrap_content"
android:layout_height="50px"
android:layout_marginLeft="50px"
android:gravity="center_vertical"
android:layout_gravity="center_vertical|center_horizontal"
>
</TextView>
 
</LinearLayout>
 

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
 
<TextView  
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="WikiCode - Custom ExpandableListView" 
    />
 
<ExpandableListView 
android:id="@+id/ExpandableListView01" 
android:layout_height="wrap_content"
  android:layout_width="fill_parent"
  >
  </ExpandableListView>
</LinearLayout>

 
Posted by 예배하는 프로그래머
  

customexpandablelistview.zip

ExpandableListViewSample.zip

listview 안에 listview을 ExpandableListView 라 한다. 
난 custom listview 에 listview을 넣는다고 생각을 했지만, android api 에서 간단하게 제공해주는 ExpandableListview가 있다... 
 
Posted by 예배하는 프로그래머