taltstidl / Swipeable-RecyclerView

A library that provides an easy and customizable way to implement a swipe to dismiss pattern with RecyclerView.
Apache License 2.0
87 stars 17 forks source link

I've keep getting this error when with normal adapter it's works. #8

Open GuyDviri opened 8 years ago

GuyDviri commented 8 years ago

FATAL EXCEPTION: main 11-18 02:14:16.350 23166-23166/com.gracetech.wedates E/AndroidRuntime: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 11-18 02:14:16.350 23166-23166/com.gracetech.wedates E/AndroidRuntime: at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 11-18 02:14:16.350 23166-23166/com.gracetech.wedates E/AndroidRuntime: at java.util.ArrayList.get(ArrayList.java:304) 11-18 02:14:16.350 23166-23166/com.gracetech.wedates E/AndroidRuntime: at com.tr4android.recyclerviewslideitem.SwipeAdapter.onBindViewHolder(SwipeAdapter.java:120)

taltstidl commented 8 years ago

@GuyDviri Thanks for reporting this. I'll take a look at this as soon as I can.

Hemant0601 commented 8 years ago

Any update on this issue?

taltstidl commented 8 years ago

@GuyDviri @Hemant0601 Haven't been able to reproduce this error yet. Are you adding/removing/moving any items in your adapter?

Hemant0601 commented 8 years ago

I am gettting below error, it seems that I have to use swipeitem instead of Relativelayout for my adapter item, but after using that I get some error in onMeasure function of SwipeItem.

java.lang.ClassCastException: android.support.v7.widget.CardView cannot be cast to com.tr4android.recyclerviewslideitem.SwipeItem at com.tr4android.recyclerviewslideitem.SwipeAdapter.onBindViewHolder(SwipeAdapter.java:45) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071) at android.view.View.layout(View.java:15761) at android.view.ViewGroup.layout(ViewGroup.java:5039) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) at android.view.View.layout(View.java:15761) at android.view.Vi

taltstidl commented 8 years ago

@Hemant0601 Would you mind posting your adapter code and item layout so I can help resolve your issue? Thanks!

akoikelov commented 8 years ago

did you set third parameter on inflater.inflate() method to true?

jossiwolf commented 8 years ago

I keep getting this error too. I am using Adapter Delegates by Hannes Dorfmann

This is working:

public class MyListAdapter extends RecyclerView.Adapter {

    private AdapterDelegatesManager<List<DisplayableItem>> delegatesManager;
    public List<DisplayableItem> mItems;

    @Inject
    public MyListAdapter() {
        super();
        mItems = new ArrayList<>();
        Activity activity = MainActivity.getMainActivity();
        // Delegates
        delegatesManager = new AdapterDelegatesManager<>();
        delegatesManager.addDelegate(new HauptspeisenAdapterDelegate(activity));
        delegatesManager.addDelegate(new DividerAdapterDelegate(activity));

    }

    public void add(DisplayableItem item) {
        mItems.add(item);
        notifyItemInserted(mItems.size()); //We just inserted the item at the last position, so the size is the position of the item
    }

    public void sort() {
        Collections.sort(mItems, new Comparator<Object>() {
            @Override
            public int compare(Object m1, Object m2) {
                if (m1 instanceof Hauptspeisen && m2 instanceof Hauptspeisen) {
                    return ((Hauptspeisen) m1).getDatum().compareTo(((Hauptspeisen) m2).getDatum());
                } else if (m1 instanceof DayItem && m2 instanceof DayItem) {
                    return ((DayItem) m1).getDatum().compareTo(((DayItem) m2).getDatum());
                } else if (m1 instanceof Hauptspeisen && m2 instanceof DayItem) {
                    return ((Hauptspeisen) m1).getDatum().compareTo(((DayItem) m2).getDatum());
                } else if (m1 instanceof DayItem && m2 instanceof Hauptspeisen) {
                    return ((DayItem) m1).getDatum().compareTo(((Hauptspeisen) m2).getDatum());
                }
                return 0;
            }
        });
    }

    public void insertAtPosition(int position, DisplayableItem item) {
        mItems.add(position, item);
    }

    public void clear() {
        mItems.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return delegatesManager.getItemViewType(mItems, position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return delegatesManager.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        delegatesManager.onBindViewHolder(mItems, position, holder);
    }

    @Override
    public int getItemCount() {
        try {
            return mItems.size();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return 0;
    }

}

This is throwing an IndexOutOfBoundsException:

public class MyListAdapter extends SwipeAdapter {

    private AdapterDelegatesManager<List<DisplayableItem>> delegatesManager;
    public List<DisplayableItem> mItems;

    @Inject
    public MyListAdapter() {
        super();
        mItems = new ArrayList<>();
        Activity activity = MainActivity.getMainActivity();
        // Delegates
        delegatesManager = new AdapterDelegatesManager<>();
        delegatesManager.addDelegate(new HauptspeisenAdapterDelegate(activity));
        delegatesManager.addDelegate(new DividerAdapterDelegate(activity));

    }

    public void add(DisplayableItem item) {
        mItems.add(item);
        notifyItemInserted(mItems.size()); //We just inserted the item at the last position, so the size is the position of the item
    }

    public void sort() {
        Collections.sort(mItems, new Comparator<Object>() {
            @Override
            public int compare(Object m1, Object m2) {
                if (m1 instanceof Hauptspeisen && m2 instanceof Hauptspeisen) {
                    return ((Hauptspeisen) m1).getDatum().compareTo(((Hauptspeisen) m2).getDatum());
                } else if (m1 instanceof DayItem && m2 instanceof DayItem) {
                    return ((DayItem) m1).getDatum().compareTo(((DayItem) m2).getDatum());
                } else if (m1 instanceof Hauptspeisen && m2 instanceof DayItem) {
                    return ((Hauptspeisen) m1).getDatum().compareTo(((DayItem) m2).getDatum());
                } else if (m1 instanceof DayItem && m2 instanceof Hauptspeisen) {
                    return ((DayItem) m1).getDatum().compareTo(((Hauptspeisen) m2).getDatum());
                }
                return 0;
            }
        });
    }

    public void insertAtPosition(int position, DisplayableItem item) {
        mItems.add(position, item);
    }

    public void clear() {
        mItems.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return delegatesManager.getItemViewType(mItems, position);
    }
    @Override
    public RecyclerView.ViewHolder onCreateSwipeViewHolder(ViewGroup parent, int viewType) {
        return delegatesManager.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindSwipeViewHolder(RecyclerView.ViewHolder holder, int position) {
        delegatesManager.onBindViewHolder(mItems, position, holder);
    }

    @Override
    public int getItemCount() {
        try {
            return mItems.size();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public SwipeConfiguration onCreateSwipeConfiguration(Context context, int i) {
        return null;
    }

    @Override
    public void onSwipe(int i, int i1) {

    }

}

This is the exception:

E/MainPresenterImpl.java: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0

taltstidl commented 8 years ago

@jossiwolf Sorry for the late reply, I'm not sure what's causing this. There are a few things that come in mind when reading your adapter code:

Also, anyone happy with a modern, yet not as customizable approach should take a look at the ItemTouchHelper in the support library.