Drag Drop and Reorder elements in ListView

心已入冬 提交于 2020-01-06 16:48:25

问题


I'm following this library to add drag - drop functionality my listview. Everything is fine when moving first item to second place. But when I move the listview first element to third place app crashes. In order to explain my situation I've added image. All I think it is caused at Swap items line but i think my code right. When I try to replace first item to third item, app is crashing. Any help will be valuable for me. Thanks for helpings.

Here is my adapter code.

public class NavigationDrawerListViewAdapter extends BaseAdapter implements Communicator,Swappable{

    private LayoutInflater mInflater;
    public ArrayList<NavigationDrawerFragment.ListItem> myItems;



    public NavigationDrawerListViewAdapter(Context activity, ArrayList<NavigationDrawerFragment.ListItem> listItemElements) {
        mInflater = (LayoutInflater) activity.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        myItems = listItemElements;

    }


    @Override
    public int getCount() {
        return myItems.size();
    }

    @Override
    public NavigationDrawerFragment.ListItem getItem(int position) {
        return myItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return myItems.get(position).hashCode();
    }

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

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View v = convertView;
        final ViewHolder holder;
        final NavigationDrawerFragment.ListItem i = myItems.get(position);
        if (i != null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.navigation_drawer_listview_simple, null);
                holder.text = (TextView) convertView.findViewById(R.id.textView123);
                holder.text.setText(i.textdata);
                convertView.setTag(holder);
                parent.setTag(holder);
                holder.text.setTag(position);


        }
        return convertView;
    }

    @Override
    public void respond(String name) {

    }

    @Override
    public void sendData(String url, String type) {

    }

    @Override
    public void sendMapValues(Geometry featureGeometry, Symbol featureSymbol, Object featureAttrValues) {
            //doinNothing
    }

    @Override
    public void swapItems(int i, int i2) {

        NavigationDrawerFragment.ListItem obj1 = myItems.get(i);
        myItems.remove(i);
        myItems.add(i2,obj1);
        this.notifyDataSetChanged();

    }


    class ViewHolder {
        TextView text;
    }

}

回答1:


I think your code keeps adding the first item to the second position, but the second item gets removed. This happens internally in the ArrayList, not in the GUI.

I suggest a technique that just swaps the two items, basic swapping using a temporary object. This time the temp is obj1. Sample code using your style:

public void swapItems(int i, int i2) {
   NavigationDrawerFragment.ListItem obj1 = myItems.get(i);
   myItems.set(i, myItems.get(i2);
   myItems.set(i2, obj1);
}

Note: I am using the ArrayList set method instead of add().



来源:https://stackoverflow.com/questions/29791195/drag-drop-and-reorder-elements-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!