ExpandableListView中的重复项

时间:2015-02-25 19:47:04

标签: android expandablelistview

ExpandableListView中创建两个包含一个孩子的组。展开两个组。从一个组中删除子项(基本上删除该组)并调用适配器的notifyDataSetChanged()BaseExpandableListAdapter的内容)。

显示剩余组中儿童的副本。折叠唯一剩余的组,重复停留在屏幕上。再次单击组标题(重新折叠它),副本消失。到目前为止,我发现的唯一解决方案是控制所有点击,并且一次只允许扩展一个组。在屏幕适配器上有一个真实子节点和一个副本调用getChildView() 4(?!)次总是请求相同组0和子0的视图。对适配器回调的所有响应都是正确的。使用简单的测试项目很容易复制(参见下面的代码)。有任何想法吗?这是Android 4.4.4。

public class MyAdapter extends BaseExpandableListAdapter {

@Override
public int getGroupCount() {
    return MyController.getInstance().getGroupCount();
}

@Override
public int getChildrenCount(int i) {
    return 1;
}

@Override
public Object getGroup(int i) {
    return null;
}

@Override
public Object getChild(int i, int i2) {
    return null;
}

@Override
public long getGroupId(int i) {
    return 0;
}

@Override
public long getChildId(int i, int i2) {
    return 0;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
    if (view == null) {LayoutInflater inflater = LayoutInflater.from(MainActivity.getMainContext());
    view = inflater.inflate(R.layout.group, null);
    }

    TextView groupHeader = (TextView) view.findViewById(R.id.lblListHeader);
    groupHeader.setText(MyController.getInstance().getGroupName(i));

    return view;
}

@Override
public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(MainActivity.getMainContext());
            view = inflater.inflate(R.layout.list_item, null);
        }

   (...set view fields...)

    return view;
}

@Override
public boolean isChildSelectable(int i, int i2) {
    return true;
}

在测试项目中,有两个按钮可添加/删除组buttonNewbuttonRemove,代码为:

public class MyController extends ActionBarActivity {

private static Context mMainContext;
myAdapter adapter;
int groupCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mMainContext = MyController.this;
    ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
    adapter = new myAdapter();
    expandableListView.setAdapter(adapter);
    Button buttonNew = (Button)findViewById(R.id.buttonAddGroup);
    buttonNew.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            groupCount += 1;
            adapter.notifyDataSetInvalidated();
        }
    });
    Button buttonRemove = (Button)findViewById(R.id.buttonRemoveGroup);
    buttonRemove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            groupCount -= 1;
            adapter.notifyDataSetInvalidated();
        }
    });
}

public static Context getInstance()
{
    return mMainContext;
}

public int getGroupCount()
{
    return groupCount;
}

0 个答案:

没有答案