-
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>> 의 구조를 갖기 때문에 여기에서 무엇인가 계산을해야한다면.. 머리좀 아플 거 같다.
머리좀 아프다..
*소스요청은 받으니 필요하신분들은 댓글*
소스는 공개 해두도록 하겠습니다. 퍼가시는 분은 댓글 매너요~ ㅎ.ㅎ
'안드로이드 > 학습&강좌' 카테고리의 다른 글
EditText InputType 속성 정의 (0) 2012.11.19 인텐트로 클래스 데이터 넘기기 (1) 2011.12.17 내 App 리스트 보기 & Mp3 파일 Album_Art 연결하기 (3) 2011.11.23 WebView 로딩시 프로그래스바 표시 (2) 2011.08.31 CoverFlow Version2 (Matrix 를 이용) (0) 2011.08.25 EXTERNAL_CONTENT_URI 를 이용한 Media File 목록 불러오기 (11) 2011.08.17 NDK-Build 하기 ( NDK 시작 ) (0) 2011.08.09 EditText Styling 하기(코너 라인드 처리) (6) 2011.08.06 DB SQLite (update) (4) 2011.08.02 DB SQLite (insert/select) (1) 2011.08.02 댓글