daimajia / AndroidSwipeLayout

The Most Powerful Swipe Layout!
MIT License
12.38k stars 2.67k forks source link

Return wrong position. #455

Open Kristaps89 opened 7 years ago

Kristaps89 commented 7 years ago

When click delete button on listview sample , return wrong position. @Override public View generateView(final int position, ViewGroup parent) { View v = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null); SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position)); swipeLayout.addSwipeListener(new SimpleSwipeListener() { @Override public void onOpen(SwipeLayout layout) { YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash)); Toast.makeText(mContext, "SWIPE: " + position, Toast.LENGTH_SHORT).show(); } }); v.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(mContext, "click delete: " + position, Toast.LENGTH_SHORT).show(); } }); return v; } how can I get correct position?

ashishg-dev commented 7 years ago

Hi Sir,

Same issue for me as well. java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 but index size must be 0 becz i have one item in the listview.

I can't write addSwipeListener in fillValues() because it gets called again and again (Depend upon the size of ArrayList) when mAdapter.notifyDataSetChanged() is called, So I have written addSwipeListener in generateView

Please help me to resolve this problem. Thanks in advance

Senocico commented 7 years ago

The same issues! Any solution?

Senocico commented 7 years ago

I found the temporarily solution for this issue: In the BaseSwipeAdapter.java I comment two lines to generate the correct position anytime. I do not say that this is the right solution, but it works...

@Override public final View getView(int position, View convertView, ViewGroup parent) { View v = convertView; //if(v == null){ v = generateView(position, parent); //} mItemManger.bind(v, position); fillValues(position, v); return v; }

ashishg-dev commented 7 years ago

I have tried with RecyclerView and its working for me fine to get the correct position. I have shared the adapter code and the DemoActivity which handle addSwipeListener event Hope this will help

import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView;

import com.daimajia.swipe.SimpleSwipeListener; import com.daimajia.swipe.SwipeLayout; import com.daimajia.swipe.adapters.RecyclerSwipeAdapter; import com.serve.moo.R; import com.serve.moo.database.DBContract;

import java.util.ArrayList; import java.util.HashMap;

/**

public class SwipeAdapter extends RecyclerSwipeAdapter {

public static final String TAG = SwipeAdapter.class.getSimpleName();
private static SwipLayoutListener swipLayoutListener;

public static class SimpleViewHolder extends RecyclerView.ViewHolder{
    SwipeLayout swipeLayout;
    TextView itemname;
    TextView itemprice;
    TextView itemID;
    TextView orderItemID;
    LinearLayout linearLayout;

    public SimpleViewHolder(View itemView) {
        super(itemView);
        swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
        linearLayout = (LinearLayout) itemView.findViewById(R.id.swipe_linearlayout_bg_left);
        itemname = (TextView) itemView.findViewById(R.id.swipe_item_name);
        itemprice = (TextView) itemView.findViewById(R.id.swipe_item_price);
        itemID = (TextView) itemView.findViewById(R.id.swipe_item_id);
        orderItemID = (TextView) itemView.findViewById(R.id.swipe_order_item_id);
        swipeLayout.addDrag(SwipeLayout.DragEdge.Left, linearLayout);

        swipeLayout.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(SwipeLayout layout) {
                if (swipLayoutListener != null) {
                    swipLayoutListener.onOpen(layout, getAdapterPosition());
                }
            }
        });
    }
}

private Context mContext;
ArrayList<HashMap<String, String>> mDataset;

public SwipeAdapter(Context context, ArrayList<HashMap<String, String>> objects) {
    this.mContext = context;
    this.mDataset = objects;
}

@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item_swip, parent, false);
    return new SimpleViewHolder(view);
}

@Override
public void onBindViewHolder(final SimpleViewHolder viewHolder, final int position) {
    viewHolder.itemname.setText(mDataset.get(position).get(DBContract.OrderTable.ITEMNAME));
    viewHolder.itemprice.setText(mDataset.get(position).get(DBContract.OrderTable.ITEMPRICE));
    viewHolder.itemID.setText(mDataset.get(position).get(DBContract.OrderTable.ITEMID));
    viewHolder.orderItemID.setText(mDataset.get(position).get(DBContract.OrderTable.ORDERITEMID));
}

@Override
public int getItemCount() {
    if (mDataset == null) {
        Log.d(TAG, "getCount: 0");
        return 0;
    } else {
        Log.d(TAG, "getCount: " + mDataset.size());
        return mDataset.size();
    }
}

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

public void addSwipeListener(SwipLayoutListener swipLayoutListener) {
    SwipeAdapter.swipLayoutListener = swipLayoutListener;
}

public interface SwipLayoutListener {
    void onOpen(SwipeLayout layout, int position);
}

}

============DemoActivity.java======

// create the object of SwipeAdapter and handeling event

    SwipeAdapter mAdapter = new SwipeAdapter (getActivity(), arrayListData);
    recyclerView.setAdapter(mAdapter);
    mAdapter.setMode(Attributes.Mode.Single);
    SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(mAdapter);
    mAdapter.addSwipeListener(new SwipeAdapter.SwipLayoutListener() {
        @Override
        public void onOpen(SwipeLayout layout, int position) {
            SwipeLayout.DragEdge edge = layout.getDragEdge();
            if (edge.equals(SwipeLayout.DragEdge.Right)) {
                // do some thing
            } else if (edge.equals(SwipeLayout.DragEdge.Left)) {
                // do some thing
            } 
        }
    });
Senocico commented 7 years ago

Try in listview; scroll it down, and then check the position: is wrong!

terranology commented 6 years ago

I have the same problem, when scroll down and show the hidden rows, the adadpter doesn't get the correct convertview

terranology commented 6 years ago

How can I edit the BaseSwipeAdapter.java ?

Senocico commented 6 years ago

You can import manually the TinyDB.java in your project, and then edit it.

mindfocus commented 6 years ago

when it fillvalues , put position into button's tag, and get position from tag, that works for me.

Azinnilchi commented 5 years ago

I have tried with RecyclerView and its working for me fine to get the correct position. I have shared the adapter code and the DemoActivity which handle addSwipeListener event Hope this will help

import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView;

import com.daimajia.swipe.SimpleSwipeListener; import com.daimajia.swipe.SwipeLayout; import com.daimajia.swipe.adapters.RecyclerSwipeAdapter; import com.serve.moo.R; import com.serve.moo.database.DBContract;

import java.util.ArrayList; import java.util.HashMap;

/**

  • Created by Ashish on 8/7/2017. */

public class SwipeAdapter extends RecyclerSwipeAdapter {

public static final String TAG = SwipeAdapter.class.getSimpleName();
private static SwipLayoutListener swipLayoutListener;

public static class SimpleViewHolder extends RecyclerView.ViewHolder{
    SwipeLayout swipeLayout;
    TextView itemname;
    TextView itemprice;
    TextView itemID;
    TextView orderItemID;
    LinearLayout linearLayout;

    public SimpleViewHolder(View itemView) {
        super(itemView);
        swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
        linearLayout = (LinearLayout) itemView.findViewById(R.id.swipe_linearlayout_bg_left);
        itemname = (TextView) itemView.findViewById(R.id.swipe_item_name);
        itemprice = (TextView) itemView.findViewById(R.id.swipe_item_price);
        itemID = (TextView) itemView.findViewById(R.id.swipe_item_id);
        orderItemID = (TextView) itemView.findViewById(R.id.swipe_order_item_id);
        swipeLayout.addDrag(SwipeLayout.DragEdge.Left, linearLayout);

        swipeLayout.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(SwipeLayout layout) {
                if (swipLayoutListener != null) {
                    swipLayoutListener.onOpen(layout, getAdapterPosition());
                }
            }
        });
    }
}

private Context mContext;
ArrayList<HashMap<String, String>> mDataset;

public SwipeAdapter(Context context, ArrayList<HashMap<String, String>> objects) {
    this.mContext = context;
    this.mDataset = objects;
}

@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item_swip, parent, false);
    return new SimpleViewHolder(view);
}

@Override
public void onBindViewHolder(final SimpleViewHolder viewHolder, final int position) {
    viewHolder.itemname.setText(mDataset.get(position).get(DBContract.OrderTable.ITEMNAME));
    viewHolder.itemprice.setText(mDataset.get(position).get(DBContract.OrderTable.ITEMPRICE));
    viewHolder.itemID.setText(mDataset.get(position).get(DBContract.OrderTable.ITEMID));
    viewHolder.orderItemID.setText(mDataset.get(position).get(DBContract.OrderTable.ORDERITEMID));
}

@Override
public int getItemCount() {
    if (mDataset == null) {
        Log.d(TAG, "getCount: 0");
        return 0;
    } else {
        Log.d(TAG, "getCount: " + mDataset.size());
        return mDataset.size();
    }
}

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

public void addSwipeListener(SwipLayoutListener swipLayoutListener) {
    SwipeAdapter.swipLayoutListener = swipLayoutListener;
}

public interface SwipLayoutListener {
    void onOpen(SwipeLayout layout, int position);
}

}

============DemoActivity.java======

// create the object of SwipeAdapter and handeling event

    SwipeAdapter mAdapter = new SwipeAdapter (getActivity(), arrayListData);
    recyclerView.setAdapter(mAdapter);
    mAdapter.setMode(Attributes.Mode.Single);
    SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(mAdapter);
    mAdapter.addSwipeListener(new SwipeAdapter.SwipLayoutListener() {
        @Override
        public void onOpen(SwipeLayout layout, int position) {
            SwipeLayout.DragEdge edge = layout.getDragEdge();
            if (edge.equals(SwipeLayout.DragEdge.Right)) {
                // do some thing
            } else if (edge.equals(SwipeLayout.DragEdge.Left)) {
                // do some thing
            } 
        }
    });

what is this line mean in your adapter?! linearLayout = (LinearLayout) itemView.findViewById(R.id.swipe_linearlayout_bg_left);