ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Custom ExpandableListView 만들기
    안드로이드/학습&강좌 2011. 8. 23. 19:52

    카테고리관리 , 제목별 정렬 아이템 숨겨놓기 등등 사용하는 방법은 다양하다.

    ExpandableListView
     



    구조를 보면 ListView를 상속하는 구조이다.

    ListView에서 사용자가 Adapter를 컨트롤해서 UI를 구성하듯이.

    ExpandableListView  도 마찬가지 이다.

    일단 만들어둔 화면은 이러하다.



    요새 원피스 동영상에 심취해 있는지라...

    이런식으로 구성했다.

    ExpandableListView 의 기본속성은 닫혀 있는 것이다.

    이부분에 대해서 열기 닫기 이벤트를 직접적으로 제공한다기 보단 리스너에서는 닫혔을때/ 열릴때의 이벤트를 반환하기 때문에 직접적으로 열어 주려면 어뎁터에서 수동으로 처리 할 수 있다.

    잘 알고 싶다면 질문해 주시길 바라고,

    Adapter에 관한 부분만 소스를 보자.

    package bb.sample;

    import java.util.ArrayList;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

        @Override
        public boolean areAllItemsEnabled()
        {
            return false;
        }

        private Context context;

        private ArrayList<String> groups;

        private ArrayList<ArrayList<Vehicle>> children;

        public ExpandableListAdapter(Context context, ArrayList<String> groups,
                ArrayList<ArrayList<Vehicle>> children) {
            this.context = context;
            this.groups = groups;
            this.children = children;
        }

        public void addItem(Vehicle vehicle) {
            if (!groups.contains(vehicle.getGroup())) {
                groups.add(vehicle.getGroup());
            }
            int index = groups.indexOf(vehicle.getGroup());
            if (children.size() < index + 1) {
                children.add(new ArrayList<Vehicle>());
            }
            children.get(index).add(vehicle);
        }

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

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
       
        // Return a child view. You can load your custom layout here.
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {
         
            Vehicle vehicle = (Vehicle) getChild(groupPosition, childPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.child_layout, null);
            }
           
           
         ImageView child = (ImageView)convertView.findViewById(R.id.ImageView01);
         if(vehicle.getName().equals("루피")){
          child.setBackgroundResource(R.drawable.a1);
            }
         else if(vehicle.getName().equals("조로")){
          child.setBackgroundResource(R.drawable.a2);
         }
         else if(vehicle.getName().equals("샹디")){
          child.setBackgroundResource(R.drawable.sang);
         }
         else if(vehicle.getName().equals("쵸파")){
          child.setBackgroundResource(R.drawable.cho);
         }
         else if(vehicle.getName().equals("우솝")){
          child.setBackgroundResource(R.drawable.woo);
         }
         else if(vehicle.getName().equals("나미")){
          child.setBackgroundResource(R.drawable.a3);
         }
         else{
          child.setBackgroundResource(R.drawable.a1);
         }
         
            TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
            tv.setText("   " + vehicle.getName());
           
            // Depending upon the child type, set the imageTextView01
            return convertView;
        }

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

        @Override
        public Object getGroup(int groupPosition) {
            return groups.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return groups.size();
        }
       
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        // Return a group view. You can load your custom layout here.
        @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.group_layout, null);
            }
            TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
            tv.setText(group);
            return convertView;
        }

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

        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }

    }



    어느 영문판 StackOverFlow 에서인가 본 어댑터를 약간 바꾼것 뿐이다. ㅎ

    ArrayList<ArrayList<class>> 의 구조를 갖기 때문에 여기에서 무엇인가 계산을해야한다면.. 머리좀 아플 거 같다.

    머리좀 아프다..

    *소스요청은 받으니 필요하신분들은 댓글*

    소스는 공개 해두도록 하겠습니다. 퍼가시는 분은 댓글 매너요~ ㅎ.ㅎ 

     

    댓글

COPYRIGHT 2010 EpoNg. ALL RIGHTS RESERVED.