Android

확장 리스트뷰(ExpanableListView)

Ms_Tony 2015. 9. 14. 01:41

확장 리스트뷰(Expanable ListView)

리스트뷰 안에 또 하나의 리스트뷰를 만들어 데이터를 저장하는 방식을 뜻한다.

그룹 뷰와 차일드 뷰 두가지로 나뉘며, 각각 그룹이 열릴 떄와 닫힐때, 차일드 뷰를 클릭 했을 경우 모두 리스너 등록이 가능하다.

MainActivity

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends Activity {

private ExpandableListAdapter listAdapter;
private ExpandableListView expListView;
private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 확장 리스트뷰를 가져온다.
expListView = (ExpandableListView) findViewById(R.id.lvExp);

// 리스트뷰에 데이터를 넣는 곳.
ChildListData();

listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

// 리스트어댑터 세팅
expListView.setAdapter(listAdapter);

// 리스트뷰 그룹(부모)뷰를 클릭 했을 경우
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});

// 그룹이 열릴 경우 이벤트 발
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});

// 그룹이 닫힐 경우 이벤트 발생
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();

}
});

// 차일드 뷰를 눌렀을 경우 이벤트 발생
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}

/**
* 부모 뷰 타이틀 및 차일드 뷰 데이터 넣는 곳
*/
private void ChildListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();

// 그룹 생성
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");

// 그룹 내 차일드 뷰 생성
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");

List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");

List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");

//데이터 적용.
listDataChild.put(listDataHeader.get(0), top250);
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
}

ExpandableListAdapter

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private ViewHolder viewHolder = null;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}

//차일드 뷰 반환
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}


//차일드 뷰 ID 반환
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

//차일드 뷰 생성(각 차일드 뷰의 (ROW)
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {

LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}

TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);

txtListChild.setText(childText);

return convertView;
}

//차일드 뷰의 사이즈 반환
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}

//그룹 포지션 반환
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}

//그룹 사이즈를 반환
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}

//그룹 ID를 반환
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

//그룹 뷰 생성(그룹 각 뷰의 ROW)
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder)convertView.getTag();
}
//그룹을 펼칠 때 또는 닫을 때 아이콘 변경
if(isExpanded){
viewHolder.iv_image.setBackgroundColor(Color.GREEN);
}else{
viewHolder.iv_image.setBackgroundColor(Color.WHITE);
}

TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);

return convertView;
}

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

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

//뷰홀더 클래스 생
class ViewHolder {
public ImageView iv_image;
}

}

출처 및 참고 : http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

http://arabiannight.tistory.com/entry/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9CAndroid-ExpandableListView-%EB%A7%8C%EB%93%A4%EA%B8%B0

ExpandableListView.zip


'Android' 카테고리의 다른 글

안드로이드 공유 기능  (0) 2015.10.16
Android Text ClipBoard에 복사하기  (0) 2015.10.15
커스텀 토스트 메시지 사용법  (0) 2015.10.08
Spannable Method(String style, color 변경)  (0) 2015.08.26
레이아웃  (0) 2015.08.20