「出現這種情況時,如果被刪除的項目擴大,然後在它下面的項目將成爲擴大。」
從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);
}
}
編輯:我注意到這個代碼的錯誤。當一行被刪除並且它下面的行向上移動以填充該空格時,新行的buttonA上的文本顯示爲左對齊。我能想到的解決這個問題的唯一辦法就是更換這行:
((ArrayAdapter<String>) adapter).notifyDataSetChanged();
與這兩條線:
adapter = new PersonArrayAdapter(people);
list.setAdapter(adapter);
如果任何人有一個更好的解決方案,我希望能看到它。
來源
2014-05-29 11:02:10
JDJ
我遇到同樣的問題。你能使它起作用嗎?如果沒有,還有其他建議嗎? – Shmuel
我想你只需要調用'((ActionSlideExpandableListView)list).collapse();'我在下面添加了一個例子。 – JDJ