DeX*_*X03 48 android expandablelistview
我正在尝试编写一个Android界面,它在侧面使用可扩展列表,在另一侧使用片段,因此我可以通过单击可扩展列表的子项来加载不同的资源.但不幸的是,我无法在任何地方找到关于此列表的任何好的教程.是的,我查看了API演示,并且我已经使用BaseExpandableListAdapter创建了一个普通的列表,但仍然理解这些列表,如果没有一个好的教程,它有点难,你有什么好的或我可以检查的信息吗?
Dmy*_*lyk 63
创建项目列表
List<ParentItem> itemList = new ArrayList<ParentItem>();
ParentItem parent1 = new ParentItem();
parent1.getChildItemList().add(new ChildItem());
parent1.getChildItemList().add(new ChildItem());
parent1.getChildItemList().add(new ChildItem());
ParentItem parent2 = new ParentItem();
parent2.getChildItemList().add(new ChildItem());
parent2.getChildItemList().add(new ChildItem());
parent2.getChildItemList().add(new ChildItem());
itemList.add(parent1);
itemList.add(parent2);
ExpandableListViewAdapter adapter = new ExpandableListViewAdapter(context, itemList);
Run Code Online (Sandbox Code Playgroud)
数据对象
public class ParentItem {
private List<ChildItem> childItemList;
public ParentItem() {
childItemList = new ArrayList<ChildItem>();
}
public List<ChildItem> getChildItemList() {
return childItemList;
}
}
public class ChildItem {
// filll with your data
}
Run Code Online (Sandbox Code Playgroud)
适配器
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
private static final class ViewHolder {
TextView textLabel;
}
private final List<ParentItem> itemList;
private final LayoutInflater inflater;
public ExpandableListViewAdapter(Context context, List<ParentItem> itemList) {
this.inflater = LayoutInflater.from(context);
this.itemList = itemList;
}
@Override
public ChildItem getChild(int groupPosition, int childPosition) {
return itemList.get(groupPosition).getChildItemList().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
return itemList.get(groupPosition).getChildItemList().size();
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
final ViewGroup parent) {
View resultView = convertView;
ViewHolder holder;
if (resultView == null) {
resultView = inflater.inflate(android.R.layout.test_list_item, null); //TODO change layout id
holder = new ViewHolder();
holder.textLabel = (TextView) resultView.findViewById(android.R.id.title); //TODO change view id
resultView.setTag(holder);
} else {
holder = (ViewHolder) resultView.getTag();
}
final ChildItem item = getChild(groupPosition, childPosition);
holder.textLabel.setText(item.toString());
return resultView;
}
@Override
public ParentItem getGroup(int groupPosition) {
return itemList.get(groupPosition);
}
@Override
public int getGroupCount() {
return itemList.size();
}
@Override
public long getGroupId(final int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View theConvertView, ViewGroup parent) {
View resultView = theConvertView;
ViewHolder holder;
if (resultView == null) {
resultView = inflater.inflate(android.R.layout.test_list_item, null); //TODO change layout id
holder = new ViewHolder();
holder.textLabel = (TextView) resultView.findViewById(android.R.id.title); //TODO change view id
resultView.setTag(holder);
} else {
holder = (ViewHolder) resultView.getTag();
}
final ParentItem item = getGroup(groupPosition);
holder.textLabel.setText(item.toString());
return resultView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
给你
==============================
+Parent 1
==============================
-child 1.1
==============================
-child 1.2
==============================
-child 1.3
==============================
+Parent 2
==============================
-child 2.1
==============================
-child 2.2
==============================
-child 2.3
==============================
Run Code Online (Sandbox Code Playgroud)
Nev*_*uit 51
您可以通过以下链接找到可扩展列表视图的工作示例:
点击孩子,你可以这样处理.
getExpandableListView().setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// your code...
}
});
Run Code Online (Sandbox Code Playgroud)
希望这会帮助你.谢谢..
| 归档时间: |
|
| 查看次数: |
90301 次 |
| 最近记录: |