我使用的库位于: https://github.com/tjerkw/Android-SlideExpandableListView
提供我的listitems的slidedown视图。我遇到的问题是我的每个列表项都有一个删除按钮,它将从底层适配器中删除该项。如果发生这种情况,如果已删除的项目已展开,则其下方的项目将展开。在挖掘了expandablelistview库的源代码之后,我发现罪魁祸首是一个BitSet,它用于跟踪列表视图的状态(扩展为1,未扩展为0)。当我从列表中删除项目时,状态列表不会更新。它需要降低所有值。问题是我不知道如何通知库我的适配器已从中删除了一个项目。
我的自定义列表适配器扩展了数组适配器,当我删除一个项目时,我调用notifyDataSetChanged。不知何故,我需要在slideExpandablelistview中检测到该调用,该调用包装了我的适配器,以便我可以更新BitSet。如果有人以前曾在这个图书馆工作或者想看看我会帮助你。
我通过
创建了expandandablelistviewmyAdapter= new SlideExpandableListAdapter(new CustomAdapter(getActivity(), new ArrayList<CustomObject>()), R.id.contact_row, R.id.expandable);
谢谢, 森
答案 0 :(得分:0)
&#34;当发生这种情况时,如果已删除的项目已展开,则其下方的项目将展开。&#34;
从ArrayList中删除项目然后执行adapter.notifyDataSetChanged()后,您可以执行以下操作:
((ActionSlideExpandableListView) list).collapse();
以下是我通过修改图书馆随附的作者样本制作的完整示例:
package com.tjerkw.slideexpandable.sample;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.tjerkw.slideexpandable.library.ActionSlideExpandableListView;
public class ExampleActivity extends Activity
{
PersonDB db;
ArrayList<String> people;
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedData)
{
super.onCreate(savedData);
this.setContentView(R.layout.single_expandable_list);
final ActionSlideExpandableListView list = (ActionSlideExpandableListView) this.findViewById(R.id.list);
db = PersonDB.getInstance();
people = db.getPeople();
adapter = new PersonArrayAdapter(people);
list.setAdapter(adapter);
// listen for events in the two buttons for every list item.
// the 'position' var will tell which list item is clicked
list.setItemActionListener(new ActionSlideExpandableListView.OnActionClickListener() {
@Override
public void onClick(View listView, View buttonview, int position)
{
switch (buttonview.getId())
{
case R.id.buttonA:
people.remove(position);
((ArrayAdapter<String>) adapter).notifyDataSetChanged();
((ActionSlideExpandableListView) list).collapse();
break;
case R.id.buttonB:
Toast.makeText(ExampleActivity.this, "You pressed buttonB", Toast.LENGTH_SHORT).show();
}
}
// note that we also add 1 or more ids to the setItemActionListener
// this is needed in order for the listview to discover the buttons
}, R.id.buttonA, R.id.buttonB);
}
private class PersonArrayAdapter extends ArrayAdapter<String>
{
public PersonArrayAdapter(ArrayList<String> people)
{
super(ExampleActivity.this, R.layout.expandable_list_item, people);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// if we weren't given a view, inflate one
if (convertView == null)
{
convertView = getLayoutInflater().inflate(R.layout.expandable_list_item, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.text);
textView.setText(people.get(position));
return convertView;
}
@Override
public int getCount()
{
return people.size();
}
}
}
这是虚拟数据库类:
package com.tjerkw.slideexpandable.sample;
import java.util.ArrayList;
public class PersonDB
{
private static PersonDB db = null;
private ArrayList<String> people;
private PersonDB()
{
people = new ArrayList<String>();
// fill "database" with dummy data
for (int i = 0; i < 20; i++)
people.add(i, "Person " + i);
}
public static PersonDB getInstance()
{
if (db == null)
db = new PersonDB();
return db;
}
public ArrayList<String> getPeople()
{
return people;
}
public String getPerson(int i)
{
return people.get(i);
}
public void deletePerson(int i)
{
people.remove(i);
}
}
编辑:我注意到此代码存在错误。删除行并且其下方的行向上移动以填充该空格时,新行的按钮A上的文本显示为左对齐。我能想到解决这个问题的唯一方法是替换这一行:
((ArrayAdapter<String>) adapter).notifyDataSetChanged();
这两行:
adapter = new PersonArrayAdapter(people);
list.setAdapter(adapter);
如果有人有更好的解决方案,我很乐意看到它。