안드로이드 개발 질문/답변
(글 수 45,052)
expandablielist에서 child 가 하나도 없는 그룹일경우
선택했을때 바로 이벤트를 받고 싶은데
어떻게 해야될지 아시는분은 좀 알려주세요.
2010.10.20 17:49:50
답변 감사합니다.
child는 그렇게 해서 콜백을 등록했고, group도 onGroupClick 메소드로 등록을 했는데요.
제가 indicator를 없애기 위해서 child가 없는 그룹은 getGroupView를 할때 expand되게 만들었습니다.
이것때문에 child가 없는 그룹들은 기본적으로 expanded된 상태로 나타나게되고,
expand된 그룹을 선택했을때 처리는 onGroupClick메소드로 처리가 안됩니다.
왜냐하면 onGroupClick메소드는 기본적으로 collapse된 그룹을 클릭했을때 처리해주는 콜백이더라구요.
그래서 님께서 알려주신 setOnItemClickListener를 사용해보았지만, 처리가 안되네요. 이유가 뭔지 모르겟구요.
다음처럼 로그를 띄어봣습니다. 근데 반응이 없더라구요.
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.i(TAG, "onItemClick " + position );
}
2010.10.20 17:55:54
혹시나 몰라서 제가 만든 소스를 보여드릴께요
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.util.Log;
import android.view.LayoutInflater;
import android.content.Context;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ExpandableListView.OnGroupClickListener;
public class ExpandableListMenu extends ExpandableListActivity
implements OnGroupClickListener, OnItemClickListener
{
ExpandableListAdapter mAdapter;
private static final String TAG = "ExpandableListMenu";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up our adapter
mAdapter = new MyExpandableListAdapter(getApplication());
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
StateListDrawable groupIndicator = (StateListDrawable)getResources().getDrawable(R.drawable.group_indicator_01);
getExpandableListView().setGroupIndicator(groupIndicator);
((MyExpandableListAdapter) mAdapter).expandGroup();
getExpandableListView().setOnGroupClickListener(this);
getExpandableListView().setOnItemClickListener(this);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
Log.i(TAG, "child clicked");
Intent intent = new Intent();
intent.setAction("MenuSelected");
//intent.putExtra("groupPosition", groupPosition);
//intent.putExtra("childPosition", childPosition);
intent.putExtra("id", mAdapter.getChildId(groupPosition, childPosition));
setResult(RESULT_OK, intent);
finish();
return true;
}
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Log.i(TAG, "group Clicked");
if(mAdapter.getChildrenCount(groupPosition) == 0) {
Intent intent = new Intent();
intent.setAction("MenuSelected");
intent.putExtra("id", mAdapter.getGroupId(groupPosition));
setResult(RESULT_OK, intent);
finish();
}
return false;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.i(TAG, "onItemClick " + position );
if(mAdapter.getChildrenCount(position) == 0) {
Intent intent = new Intent();
intent.setAction("MenuSelected");
//intent.putExtra("groupPosition", groupPosition);
//intent.putExtra("childPosition", childPosition);
intent.putExtra("id", mAdapter.getGroupId(position));
setResult(RESULT_OK, intent);
finish();
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Sample menu");
menu.add(0, 0, 0, R.string.expandable_list_sample_action);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) info.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,
Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
/**
* A simple adapter which maintains an ArrayList of photo resource Ids.
* Each photo is displayed as an image. This adapter supports clearing the
* list of photos and adding a new photo.
*
*/
private class MyExpandableListAdapter extends BaseExpandableListAdapter {
private String[] mGroupData;
private String[][] mChildData;
private LayoutInflater mInflater;
private int[] mGroupId;
private int[][] mChildId;
public MyExpandableListAdapter(Context context) {
mGroupData = context.getResources().getStringArray(R.array.search_module_groupList);
mChildData = new String[mGroupData.length][];
for(int i=0; i<mGroupData.length; i++) {
mChildData[i] = context.getResources().getStringArray(R.array.search_module_childList_00 + i);
}
mGroupId = context.getResources().getIntArray(R.array.search_module_groupId);
mChildId = new int[mGroupId.length][];
for(int i=0; i<mChildId.length; i++) {
mChildId[i] = context.getResources().getIntArray(R.array.search_module_childId_00 + i);
}
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public Object getChild(int groupPosition, int childPosition) {
return mChildData[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return mChildId[groupPosition][childPosition];
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = newChildView(isLastChild, parent);
} else {
view = convertView;
}
TextView textView = (TextView)view.findViewById(android.R.id.text1);
textView.setText(mChildData[groupPosition][childPosition]);
return view;
}
/**
* Instantiates a new View for a child.
* @param isLastChild Whether the child is the last child within its group.
* @param parent The eventual parent of this new View.
* @return A new child View
*/
public View newChildView(boolean isLastChild, ViewGroup parent) {
return mInflater.inflate(R.layout.my_expandable_list_2, parent, false);
}
public int getChildrenCount(int groupPosition) {
return mChildData[groupPosition].length;
}
public Object getGroup(int groupPosition) {
return mGroupData[groupPosition];
}
public int getGroupCount() {
return mGroupData.length;
}
public long getGroupId(int groupPosition) {
return mGroupId[groupPosition];
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
View view;
if (convertView == null) {
view = newGroupView(isExpanded, parent);
} else {
view = convertView;
}
TextView textView = (TextView)view.findViewById(android.R.id.text1);
textView.setText(mGroupData[groupPosition]);
if(getChildrenCount(groupPosition) == 0){
getExpandableListView().expandGroup(groupPosition);
}
return view;
}
public void expandGroup() {
for(int i=0; i<mGroupData.length; i++) {
if(getChildrenCount(i) == 0){
getExpandableListView().expandGroup(i);
}
}
}
/**
* Instantiates a new View for a group.
* @param isExpanded Whether the group is currently expanded.
* @param parent The eventual parent of this new View.
* @return A new group View
*/
public View newGroupView(boolean isExpanded, ViewGroup parent) {
return mInflater.inflate(R.layout.my_expandable_list_1,
parent, false);
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}
2010.10.20 18:00:49
onChildClickListener는 ExpandableListActivity 클래스가 기본적으로 상속받고 있더라구요.
메소드만 위에처럼 구현해놓으면 되는거 같더라구요.




구조를 보면 알겠지만, ExpandableListView 도 ListView 를 기본 베이스로 하여 확장된 컴포넌트입니다.
때문에 setOnItemClickListener 를 이용하면, 해당 ItemClick에 대한 이벤트를 가져올 수 있습니다.
ExpandableListView의 경우 ExpandableListView.OnChildClickListener 를 지원하므로,
해당 callBack인 onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) 로
문제를 해결 하실수 있을듯 합니다.