네트워크를 통해 외부 데이터를 가져오고 ExpandableListAdapter
을 통해 ExpandableListView
을 채우는 간단한 예제를 작성하려고합니다. 나는 몇 가지 예제를 시도하고 꽤 많이 잃어 버렸다. 나는 ArrayAdapter
클래스로 작업하는 것에 익숙하지만, ExpandableListAdapter
은 다른 것 같습니다.ExpandableListAdapter를 알 수없는 것처럼 보일 수 없습니다.
MyExpandableListAdapter :
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
public static class GroupHolder {
TextView title;
}
public static class ChildHolder {
TextView title;
ImageView icon;
}
// groups.getChildren() returns the children
private List<Group> groups;
private Context context;
private LayoutInflater inflater;
public MyExpandableListAdapter(Context context, List<Group> groups) {
this.context = context;
this.groups = groups;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public boolean hasStableIds() {
return true;
}
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
GroupHolder holder;
if (convertView != null) {
holder = (GroupHolder)convertView.getTag();
} else {
convertView = inflater.inflate(R.layout.group_item, parent, false);
holder = new GroupHolder();
holder.title = (TextView) convertView.findViewById(R.id.group_item_title);
convertView.setTag(holder);
}
holder.title.setText(this.groups.get(groupPosition).getName());
return convertView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).getChildren().get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).getChildren().size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
if (convertView != null) {
holder = (ChildHolder) convertView.getTag();
} else {
convertView = inflater.inflate(R.layout.child_item, parent, false);
holder = new ChildHolder();
holder.title = (TextView) convertView.findViewById(R.id.child_item_title);
holder.icon = (ImageView) convertView.findViewById(R.id.child_item_icon);
convertView.setTag(holder);
}
holder.title.setText(groups.get(groupPosition).getChildren().get(childPosition).getName());
// TODO add in image loading.
return convertView;
}
}
MyExpandableListActivity :
public class MyExpandableListActivity extends ExpandableListActivity {
private List<Group> groups;
private ExpandableListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.group_list);
this.groups = new ArrayList<Group>();
Group group = new Group("$1", "Colors");
Child child = new Child("$2", "Red");
groups.getChildren().add(child);
this.adapter = new MyExpandableListAdapter(this, groups);
setListAdapter(this.adapter);
}
}
R.layout.group_list :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/android:empty"
android:text="EMPTY! DOOM!"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
여기 내 현재 응용 프로그램 코드가 표시되어 아무것도 아니다 6,
R.layout.group_item :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/group_item_title"/>
</LinearLayout>
R.layout.child_item :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView android:id="@+id/child_item_icon"
android:layout_width="48px" android:layout_height="48px"/>
<TextView android:id="@+id/child_item_title"/>
</LinearLayout>
나는 물론, "보는 EMPTY있다! DOOM! "이라는 메시지가 표시됩니다. 내가 뭘 잘못 했나요?
또한 SimpleExpandableListAdapter
은 일을 쉽게하기로되어있는 것 같지만 완전히 작동하는지 그것을 사용하는 방법 누군가가 나를 알아내는 데 도움이 될 수 있습니까?
는 match_parent''에 그 레이아웃에있는 모든 변경. 아직 아무것도. –
내 목록을 밀어 내고 있던 항목에 대해 wrap_content로 변경해야했습니다. getView()가 호출되지 않으면 getGroupCount와 같은 소리가 호출되지 않거나 0을 반환합니다. –
이 섹션에서는 나에게 맞는 모양이 아니지만 그룹을 사용하는 곳을 볼 수 없습니다. 하위 항목을 추가하고 있지만 그룹을 추가하는 위치 또는 그룹 내의 하위 항목은 어디에 있습니까? \ n this.groups = new ArrayList(); 그룹 그룹 = 새로운 그룹 ("$ 1", "색상"); 자식 하위 = 새 하위 ("$ 2", "빨강"); groups.getChildren(). add (child); this.adapter = new MyExpandableListAdapter (this, groups); –