< 개발 환경 > < 프로젝트 적용 > |
안드로이드/Android ExpandableListView 만들기 - 2 - (속성 추가)
안드로이드 ExpandableListView 예제 입니다. 기존의 ExpandableListView 예제에서 몇가지 속성 사항들만 변형해서 만든 소스 입니다. ExpandableListView 자체에 대한 설명은 [안드로이드/Android ExpandableListView 만들기] 를 참고하시기 바랍니다.
자 그럼 예제를 살펴 보겠습니다.
package arabiannight.tistory.com.expandablelistviewattribute.activity;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
import arabiannight.tistory.com.expandablelistviewattribute.R;
import arabiannight.tistory.com.expandablelistviewattribute.adapter.BaseExpandableAdapter;
public class TestExpandableListViewActivity extends Activity {
private ArrayList mGroupList = null;
private ArrayList> mChildList = null;
private ArrayList mChildListContent = null;
private BaseExpandableAdapter mBaseExpandableAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_expandable_list_view);
setLayout();
mGroupList = new ArrayList();
mChildList = new ArrayList>();
mChildListContent = new ArrayList();
mGroupList.add("가위");
mGroupList.add("바위");
mGroupList.add("보");
mChildListContent.add("1");
mChildListContent.add("2");
mChildListContent.add("3");
mChildList.add(mChildListContent);
mChildList.add(mChildListContent);
mChildList.add(mChildListContent);
mBaseExpandableAdapter = new BaseExpandableAdapter(this, mGroupList, mChildList);
mListView.setAdapter(mBaseExpandableAdapter);
// 그룹 Indiacator 삭제 (그룹 왼쪽에 기본제공되는 화살표 아이콘 삭제)
mListView.setGroupIndicator(null);
// 처음 시작시 그룹 모두 열기 (expandGroup)
// int groupCount = (int) mBaseExpandableAdapter.getGroupCount();
// for (int i = 0; i < groupCount; i++) {
// mListView.expandGroup(i);
// }
// 처음 시작시 그룹 모두 닫기 (collapseGroup)
// for (int i = 0; i < groupCount; i++) {
// mListView.collapseGroup(i);
// }
// 그룹 클릭 했을 경우 이벤트
mListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(getApplicationContext(), "g click = " + groupPosition,
Toast.LENGTH_SHORT).show();
// Listener 에서 Adapter 사용법 (getExpandableListAdapter 사용해야함.)
// BaseExpandableAdpater에 오버라이드 된 함수들을 사용할 수 있다.
int groupCount = (int) parent.getExpandableListAdapter().getGroupCount();
int childCount = (int) parent.getExpandableListAdapter().getChildrenCount(groupPosition);
return false;
}
});
// 차일드 클릭 했을 경우 이벤트
mListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(getApplicationContext(), "c click = " + childPosition,
Toast.LENGTH_SHORT).show();
return false;
}
});
// 그룹이 닫힐 경우 이벤트
mListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(), "g Collapse = " + groupPosition,
Toast.LENGTH_SHORT).show();
}
});
// 그룹이 열릴 경우 이벤트
mListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(), "g Expand = " + groupPosition,
Toast.LENGTH_SHORT).show();
int groupCount = mBaseExpandableAdapter.getGroupCount();
// 한 그룹을 클릭하면 나머지 그룹들은 닫힌다.
for (int i = 0; i < groupCount; i++) {
if (!(i == groupPosition))
mListView.collapseGroup(i);
}
}
});
}
/*
* Layout
*/
private ExpandableListView mListView;
private void setLayout(){
mListView = (ExpandableListView) findViewById(R.id.elv_list);
}
}
// 예제 에서 보시는 바와 같이 마지막으로 그룹을 클릭한 경우에만 펼쳐지고 나머지 그룹들은 닫히게 되는 코드를 추가 하였고 어플 처음실행시 Group 들은 모두 펼침 상태와, 모두 닫힘 상태로 만들 수 있는 코드를 추가 하였습니다. 주석만 풀고 사용하시기 바랍니다.
그리고 예제들을 조금씩만 변형시키면 원하시는 기능을 만들 수 있으니 유용하게 활용 하시기 바랍니다.
파일첨부 :
TestExpandableListViewAttribute.zip
'Android > Listview' 카테고리의 다른 글
| 안드로이드/Android ListView 클릭시 색상(배경) 변경 하기 ~! (0) | 2013.02.26 |
|---|---|
| 안드로이드/Android ListView Header, Footer 설정 하기 ~! (3) | 2013.01.10 |
| 안드로이드/Android AdapterView.onItemClickListener 사용법 (2) | 2012.04.03 |
| 안드로이드/Android ExpandableListView 만들기 (31) | 2012.03.29 |
| 안드로이드/Android ArrayAdapter, BaseAdapter를 이용한 ListView 구현 (3) | 2012.03.28 |