繁体   English   中英

删除可扩展列表中的组

[英]Delete group in Expandable List

我试图在我的可扩展列表中选择时删除组,并在删除发生后刷新列表。

我从谷歌获取源代码:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html

我尝试了几种方法来删除它,但没有成功,任何人都已经实现了这一点?

谢谢,雷。

回答第二个问题(向子和数据结构添加行)

对于数据结构:

public class GroupItem {
 private String mItemText;
 private List<ChildItem> mChildItems=new ArrayList<ChildItem>();    

 public GroupItem(String itemText) {
  mItemText=itemText;
 }

 public String getItemText() {
  return mItemText;
 }

 public ChildItem getChild(int childPosition) {
  return mChildItems.get(childPosition);
 }

 public void addChild(ChildItem childItem) {
  return mChildItems.add(childItem)
 }

 public void removeChild(int childPosition) {
  return mChildItems.remove(childPosition);
 }

}

public class ChildItem {
 private String mItemText;
 public ChildItem(itemText) {
  mItemText=itemText;
 }

 public String getItemText() {
  return mItemText;
 }
}

现在设置它你会做类似的事情:

List<GroupItem> items = new ArrayList<GroupItem>();

GroupItem item1 = new GroupItem("This is group 1");
item1.addChild(new ChildItem("This is a child item of group 1"));
item1.addChild(new ChildItem("This is another child item of group 1"));

等等...

然后,在适配器中,您需要返回适当的数据。

有关行的问题:在Google示例中,它们返回TextView。 但是,您可以根据自己喜欢的内容制作自己的布局。 例如:

<ImageView android:id="@+id/RowIcon" 
  android:layout_width="56px" 
  android:layout_height="56px" 
  android:layout_marginLeft="12px" 
  android:layout_marginTop="2px">
</ImageView>
<TextView android:id="@+id/RowTextView"
     android:paddingLeft="10px"
     android:paddingTop="10px"       
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" android:textColor="#666666" android:textSize="14px"/>


</LinearLayout>

然后在适配器中使用此布局。 而不是返回,例如,TextView,您只需将其作为视图返回:

public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {

LayoutInflater mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = mInflater.inflate(YOUR XML FILE FROM ABOVE, null);
    ImageView rowIcon = (ImageView)rowView.findViewById(R.id.RowIcon);
    iconType.setBackgroundResource(AN IMAGE HERE);
    TextView rowText =(TextView)rowView.findViewById(R.id.RowTextView);
    textAddress.setText(items.get(groupPosition).getChild(childPosition));

    return rowView;
 }

所以,我认为应该让你去。

另外,如果他们满意,请接受我的答案。

干杯。

将功能添加到适配器以删除项目:

    public void removeGroup(int group) {
        //TODO: Remove the according group. Dont forget to remove the children aswell!
        Log.v("Adapter", "Removing group"+group);
        notifyDataSetChanged();
    } 

    public void removeChild(int group, int child) {
        //TODO: Remove the according child
        Log.v("Adapter", "Removing child "+child+" in group "+group);
        notifyDataSetChanged();
    }

通过更改以下内容确保您可以访问新方法:

ExpandableListAdapter mAdapter;

MyExpandableListAdapter mAdapter;

需要时调用方法:

 @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); 
        mAdapter.removeChild(groupPos, childPos);
        return true;
    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
        int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
        mAdapter.removeGroup(groupPos);
        return true;
    }

    return false;
}

所以,希望有所帮助。 干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM