Skip to content

remove an listview item in swipeadater? #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
huchunyu opened this issue Sep 2, 2014 · 16 comments
Closed

remove an listview item in swipeadater? #17

huchunyu opened this issue Sep 2, 2014 · 16 comments

Comments

@huchunyu
Copy link

huchunyu commented Sep 2, 2014

I have a horizontal list view and for each item I want to delete it with swipe gesture. I put the delete logic in onOpen method, but seems it's not working.

@Override
            public void onOpen(SwipeLayout layout) {
                 //when the BottomView totally show.
                Log.d("Position", Integer.toString(position));

                mPictures.remove(position);
                notifyDataSetChanged();

            }
@daimajia
Copy link
Owner

daimajia commented Sep 2, 2014

Could you show me you adapter code?

@huchunyu
Copy link
Author

huchunyu commented Sep 2, 2014

public class CircleAdapter extends SwipeAdapter {

private Context mContext;
private List<Picture> mPictures ;

public CircleAdapter(Context context, List<Picture> pictureList ) {
    super();
    this.mContext = context;
    this.mPictures = pictureList;

}




@Override
public int getSwipeLayoutResourceId(int position) {
    return R.id.circle_swipe_layout;
}

@Override
public View generateView(final int position, final ViewGroup parent) {

    final View v = LayoutInflater.from(mContext).inflate(R.layout.list_item_horizontal_picture,parent,false);

    final SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));


    swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
        @Override
        public void onClose(SwipeLayout layout) {
            //when the SurfaceView totally cover the BottomView.
        }

        @Override
        public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
            //you are swiping.
        }

        @Override
        public void onOpen(SwipeLayout layout) {
             //when the BottomView totally show.
            //Log.d("Position @ onOpen", Integer.toString(position));

             mPictures.remove(position);
             notifyDataSetChanged();



        }

        @Override
        public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
             //when user's hand released.

        }
    });



@Override
public void fillValues(int position, View convertView) {


    final Picture picture = (Picture) getItem(position);

    final ImageView imageView = (ImageView) convertView.findViewById(R.id.list_item_picture_image_view);
    imageView.setImageBitmap(null);
    Picasso.with(mContext).load(picture.mUrl).into(imageView);

    final TextView textView = (TextView) convertView.findViewById(R.id.list_item_picture_text_view);
    textView.setText(picture.mCaption);


}

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

@Override
public Object getItem(int position) {

    return mPictures.get(position);

}

@Override
public long getItemId(int position) {
    return position;
}

}

@huchunyu
Copy link
Author

huchunyu commented Sep 2, 2014

The following is the exception I got. Seems it's related with wrong position value...

FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.remove(ArrayList.java:403)
at xxx.xxx.xxx.CircleAdapter$2.onOpen(CircleAdapter.java:85)
at com.daimajia.swipe.SwipeLayout.dispatchSwipeEvent(SwipeLayout.java:465)
at com.daimajia.swipe.SwipeLayout.open(SwipeLayout.java:1018)
at com.daimajia.swipe.SwipeAdapter$OnLayoutListener.onLayout(SwipeAdapter.java:156)
at com.daimajia.swipe.SwipeLayout.onLayout(SwipeLayout.java:605)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at mobi.parchment.widget.adapterview.listview.ListLayoutManager.layoutCell(ListLayoutManager.java:171)
at mobi.parchment.widget.adapterview.listview.ListLayoutManager.layoutCell(ListLayoutManager.java:18)
at mobi.parchment.widget.adapterview.LayoutManager.layoutCells(LayoutManager.java:597)
at mobi.parchment.widget.adapterview.LayoutManager.layout(LayoutManager.java:221)
at mobi.parchment.widget.adapterview.AbstractAdapterView.onLayout(AbstractAdapterView.java:174)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

@daimajia
Copy link
Owner

daimajia commented Sep 2, 2014

Hey guy,

please move the following code to fillValues

 swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
        @Override
        public void onClose(SwipeLayout layout) {
            //when the SurfaceView totally cover the BottomView.
        }

        @Override
        public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
            //you are swiping.
        }

        @Override
        public void onOpen(SwipeLayout layout) {
             //when the BottomView totally show.
            //Log.d("Position @ onOpen", Integer.toString(position));

             mPictures.remove(position);
             notifyDataSetChanged();



        }

        @Override
        public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
             //when user's hand released.

        }

the reason is here #14

@daimajia
Copy link
Owner

daimajia commented Sep 2, 2014

have a try and give me feedback.

@daimajia
Copy link
Owner

daimajia commented Sep 2, 2014

BTW. you can use SimpleSwipeListener to make your code clean.

@huchunyu
Copy link
Author

huchunyu commented Sep 2, 2014

Thanks for quick reply. I still got java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 .

@daimajia
Copy link
Owner

daimajia commented Sep 2, 2014

hi, this is addSwipeListener instead of setSwipeListener.
please check if you add multiple swipeListener? If there are multiple swipeListeners they may delete items together, and trigger IndexOutOfBoundsException .

@huchunyu
Copy link
Author

huchunyu commented Sep 2, 2014

You are right. seems getView will add a default swipeMemory...? I put a removeAllSwipeListener() before add my own swipeListener and now it's working. Huge thanks!

@daimajia
Copy link
Owner

daimajia commented Sep 2, 2014

Great !

@daimajia daimajia closed this as completed Sep 2, 2014
@daimajia
Copy link
Owner

daimajia commented Sep 2, 2014

I think I should remove removeAllSwipeListener method from the code.

@huchunyu
Copy link
Author

huchunyu commented Sep 2, 2014

if I put remove item logic in onOpen method, I got null pointer exception from time to time. If I put that into button onclicklistener, it'll be fine.

@rcdev67
Copy link

rcdev67 commented Feb 25, 2015

Because i ran into same issues, my question, what is the answer to this ?
if a item is removed from adapter, for every item the onOpen Method in SwipeListener is fired.

@m2kk
Copy link

m2kk commented Mar 18, 2015

Hey guys,

so there's definitely some problem with this:
Target: get swipe2delete working
first: if you add swipelisteners you definitly got multiple of them… and on swiping you delete multiple items
second: there is not method: removeAllSwipeListener (even searched the github project, does not exist)
third: even on making one instance of swipelistener, remove and add it oneByOne to every view, so that there should be just "this one listener" at each view, after scrolling a bit, you got multiple onOpen calls at an item.
fourth: for instance there are 40 items, deleting one by one beginning at top works, but if you like scroll a bit down, up, swipe2delete some items and continue, i bet, within 30 seconds doing this, you definitly get one time, that the bottomView stays opened for the next item.

I used the code given in a sample (demo folder):
mItemManger.removeShownLayouts(layout);
data.remove(dataItem);
int itemPosition = getItemIndex(dataItem);
notifyItemRemoved(itemPosition);
notifyItemRangeChanged(itemPosition, data.size());
mItemManger.closeAllItems();

fifth: so sorry, for now I didn't get it to work constantly. at least on 15 deletions, one makes some broken state :(
any advice would be perfect.

btw: using android L on a Nexus 5

@basith0304
Copy link

Hi,

When I’m using Kitkat Version . If i swipe the list item, delete & open button appeared and suddenly its closed .(Your Demo Application also same )other versions its working good .

Your Application Link : https://github.com/daimajia/AndroidSwipeLayout/releases/download/v1.1.8/AndroidSwipeLayout-v1.1.8.apk

Please help me

@Michal-easycore
Copy link

Hey,
don't know guys if you are still facing this issue, but right now I am having exact same problem using lib version 1.2.0. I resolved the problem simulating removeAllSwipeListeners() by having my own List.

public class FixedSwipeLayout extends SwipeLayout {

private List<SwipeListener> swipeListeners;

public FixedSwipeLayout(Context context) {
    super(context);
    swipeListeners = new ArrayList<>();
}

public FixedSwipeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    swipeListeners = new ArrayList<>();
}

public FixedSwipeLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    swipeListeners = new ArrayList<>();
}

@Override
public void addSwipeListener(SwipeListener listener) {
    for (SwipeListener l : swipeListeners) {
        removeSwipeListener(l);
    }
    swipeListeners.clear();

    swipeListeners.add(listener);
    super.addSwipeListener(listener);
}

}

I think correctly solving this problem is to have Set instead of List inside SwipeLayout. I hope it will be fixed soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants