【问题标题】:Android SlideExpandableListView Remove ItemAndroid Slide ExpandableListView 移除项目
【发布时间】:2014-01-16 00:16:30
【问题描述】:

我正在使用位于以下位置的库: https://github.com/tjerkw/Android-SlideExpandableListView

在我的列表项上提供滑动视图。我遇到的问题是我的每个列表项都有一个删除按钮,它将从底层适配器中删除该项目。发生这种情况时,如果已删除的项目已展开,则其下方的项目将展开。在挖掘了可扩展列表视图库的源代码后,我发现罪魁祸首是一个 BitSet,它被用来跟踪列表视图的状态(扩展 1,而不是扩展 0)。当我从列表中删除一个项目时,状态列表不会更新。它需要将所有值向下移动。问题是我不确定如何通知库我的适配器已从其中删除了一个项目。

我的自定义列表适配器扩展了数组适配器,当我删除一个项目时,我调用 notifyDataSetChanged。不知何故,我需要在包含我的适配器的 slideExpandablelistview 中检测到该调用,以便我可以更新 BitSet。如果有人以前使用过这个库或者想看看,我会很乐意提供帮助。

我通过这样做来创建可扩展列表视图

myAdapter= new SlideExpandableListAdapter(new CustomAdapter(getActivity(), new  ArrayList<CustomObject>()), R.id.contact_row, R.id.expandable);

谢谢, 内森

【问题讨论】:

  • 我遇到了同样的问题。你能让它工作吗?如果没有,还有其他建议吗?
  • 我想你只需致电((ActionSlideExpandableListView) list).collapse(); 我在下面添加了一个示例。

标签: android android-arrayadapter expandablelistview expandablelistadapter


【解决方案1】:

“当这种情况发生时,如果被删除的项目被展开,那么它下面的项目将被展开。”

从 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); 

如果有人有更好的解决方案,我很乐意看到。

【讨论】:

    猜你喜欢
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多