我有一个BaseExpandableListAdapter
,其第一个Group
包含Buttons
的元素。我想点击任意Child
之后删除或隐藏Button
视图。我在下面的代码中试过这些。
public class ProfileExpandableAdapter extends BaseExpandableListAdapter{
private Context context;
private ArrayList<ExpandableGroup> groups;
private ImageLoader imageLoader = AppController.getInstance().getImageLoader();
private String type = "update_user_friend", userName = "";
private JSONParser jsonParser;
private String wpedenUrl = "", TAG_SUCCESS = "success";
public ProfileExpandableAdapter(Context context, ArrayList<ExpandableGroup> groups, String userName) {
this.context = context;
this.groups = groups;
this.userName = userName;
jsonParser = new JSONParser();
wpedenUrl = context.getResources().getString(R.string.site);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<ExpandableChild> chList = groups.get(groupPosition).getItems();
return chList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final ExpandableChild child = (ExpandableChild) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.wpedenyo_profile_expandable_group_child, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.wpedenyo_profile_group_child);
tv.setText(child.getName().toString());
String image = child.getThumbnailUrl();
if(groupPosition == 0){
NetworkImageView notifiedUserImage = (NetworkImageView) convertView.findViewById(R.id.wpedenyo_notified_friend_image);
notifiedUserImage.setVisibility(View.VISIBLE);
notifiedUserImage.setImageUrl(image, imageLoader);
Button confirmButton = (Button) convertView.findViewById(R.id.wpedenyo_profile_button_confirm);
confirmButton.setVisibility(View.VISIBLE);
Button cancelButton = (Button) convertView.findViewById(R.id.wpedenyo_profile_button_cancel);
cancelButton.setVisibility(View.VISIBLE);
confirmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
groups.remove(child);
notifyDataSetChanged();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
groups.remove(child);
notifyDataSetChanged();
}
});
}
return convertView;
}
public void removeChild(int groupPosition, int childPosition) {
if (getChildrenCount(groupPosition)>0 && getChildrenCount(groupPosition)-1 >= childPosition )
{
Object task = this.getChild(groupPosition, childPosition);
groups.remove(task);
this.notifyDataSetChanged();
}
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<ExpandableChild> chList = groups.get(groupPosition).getItems();
return chList.size();
}
@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;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ExpandableGroup group = (ExpandableGroup) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.wpedenyo_profile_expandable_group, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.wpedenyo_profile_list_header);
tv.setText(group.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
但点击Button
后,无法从Child
删除Group
。我怎样才能做到这一点?提前谢谢。
答案 0 :(得分:0)
而不是groups.remove(child)
(我想你试图在这里删除群组)尝试类似
获取当前组
ExpandableGroup currentGroup=groups.get(groupPosition);
让孩子们(你必须实施getChildren
方法):
currentGroup.getChildren().remove(child);
希望它有所帮助。
答案 1 :(得分:0)
您需要致电collapseGroup()。同样在您的构造函数中,您需要ExpandableListView
public class YourExapandableAdapter extends BaseExpandableListAdapter {
private Context mContext;
ExpandableListView mExpandableListView;
int mLastExpandedGroupPosition = -1;
public YourExapandableAdapter(Context context, ExpandableListView expandableListView) {
mContext = context;
mExpandableListView = expandableListView;
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
if(groupPosition != mLastExpandedGroupPosition){
mExpandableListView.collapseGroup(mLastExpandedGroupPosition);
}
mLastExpandedGroupPosition = groupPosition;
}
在这种情况下,我隐藏了最后一组,但您可以使用当前组或任何您想要的组。