我是Android新手,我正在努力使用ExpandableListView
进行以下操作。
我希望有3个小组 - “在线”,“离线”和“离开”。这些组将包含将从Internet加载的子自定义对象(联系人)。这些对象一次只能在一个组中(虽然这可能会在以后更改)。
我在没有寻求帮助的情况下尝试了这个,我将粘贴代码 - 它让我显示列表视图与正确的组,但孩子们在所有组中。
在代码中,联系人项目当前只在另一个类的矢量中。
我真的很感激一些帮助,因为我真的被卡住了。
public class MyTreeAdaptor extends BaseExpandableListAdapter{
@Override
public boolean areAllItemsEnabled()
{
return true;
}
private Context context;
private ArrayList<String> groups;
private ArrayList<ArrayList<Contact>> children;
public MyTreeAdaptor(Context context, ArrayList<String> groups,
ArrayList<ArrayList<Contact>> children) {
this.context = context;
this.groups = groups;
this.children = children;
}
/**
* A general add method, that allows you to add a Contact to this list
*
* Depending on if the category opf the Contact is present or not,
* the corresponding item will either be added to an existing group if it
* exists, else the group will be created and then the item will be added
* @param Contact
*/
public void addItem(Contact aContact) {
try {
Log.e("additem", ""+aContact.show);
if (!groups.contains(aContact.show)) {
groups.add(aContact.show);
}
int index = groups.indexOf(aContact.show);
if (children.size() < index + 1) {
children.add(new ArrayList<Contact>());
}
children.get(index).add(aContact);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("additem error", " "+e);
}
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return children.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
// Return a child view. You can load your custom layout here.
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
try {
Contact contact = (Contact) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.treeview, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.text1);
try {
tv.setText(" " + contact.name);
tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Depending upon the child type, set the imageTextView01
/*if (Contact instanceof Car) {
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.car, 0, 0, 0);
} else if (Contact instanceof Bus) {
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bus, 0, 0, 0);
} else if (Contact instanceof Bike) {
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bike, 0, 0, 0);
}*/
return convertView;
} catch (Exception e) {
Log.e("error thr", " "+e);
e.printStackTrace();
return null;
}
}
@Override
public int getChildrenCount(int groupPosition) {
try
{
return children.get(groupPosition).size();
}
catch(Exception hh)
{
return 0;
}
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
// Return a group view. You can load your custom layout here.
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
try {
Log.e(groupPosition+"groups size is", " "+groups.size());
String group;
if(groups.size() > 0)
{
group = (String) getGroup(groupPosition);
}
else
{
group = "null";//(String) getGroup(groupPosition);
}
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.treeview, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.text1);
try {
tv.setText(group);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
答案 0 :(得分:0)
如果您有两种不同类型的子视图,则必须在适配器中实现这些方法:
@Override
public int getChildTypeCount() {
return 2;
}
@Override
public int getChildType(int groupPosition, int childPosition) {
return groupPosition == 4 ? 1 : 0;
}
然后对getChildView
方法进行以下更改:
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = convertView;
if(v == null) {
if (groupPosition == 4) {
v = layoutInflater.inflate(R.layout.treeitem, null);
} else {
v = layoutInflater.inflate(R.layout.treeview, null);
}
TextView tt = null;
if (groupPosition != 4) {
tt = (TextView) v.findViewById(R.id.text1);
// rest of the method
要了解其工作原理,请花一些时间观看this视频和also此答案。为ListView解释的概念同样适用于ExpandableListView。