gabrielemariotti / cardslib

Android Library to build a UI Card
4.66k stars 1.19k forks source link

CardListView setOnScrollListener #19

Closed nelsonrc closed 11 years ago

nelsonrc commented 11 years ago

I try to implement infinite scroll. But when setOnScrollListener it fires some tines a stop, not firing any more. I test with standard ListView and work well:

Binding:

listView.setOnScrollListener(

new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        //what is the bottom iten that is visible
        int lastInScreen = firstVisibleItem + visibleItemCount;

        Debug.Log(TAG, "lastInScreen." + lastInScreen);
    }
});

/// Adapter: public class NewsCardAdapter extends CardArrayAdapter{ private int lastPosition = -1; private List mcards;

    /**
     * Constructor
     *
     * @param context The current context.
     * @param cards   The cards to represent in the ListView.
     */
    public NewsCardAdapter(Context context, List<Card> cards) {
        super(context, cards);
        mcards = cards;
    }

    @Override
    public int getCount() {
        Debug.Log(TAG, "Adapter.getCount." + mcards.size());
        return mcards.size();
    }

    @Override
    public Card getItem(int position) {
        Debug.Log(TAG, "Adapter.getItem");
        return mcards.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        return view;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }
}
gabrielemariotti commented 11 years ago

I tried your code and I can't replicate this issue. The scrollListener continues to output degub strings. Can you add some details?

matthew-reilly commented 10 years ago

I run into the same issue. Here is my scroll listener. Neither method is ever called.

listView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {

                Log.d("test", "" );;

            }
        });
matthew-reilly commented 10 years ago

I believe it has to do with CardArrayAdapter:

if (card.isSwipeable()){
            if (mOnTouchListener == null){
                mOnTouchListener = new SwipeDismissListViewTouchListener(mCardListView, mCallback);
                // Setting this scroll listener is required to ensure that during
                // ListView scrolling, we don't look for swipes.
                mCardListView.setOnScrollListener(mOnTouchListener.makeScrollListener());
            }

            cardView.setOnTouchListener(mOnTouchListener);

        }else{
            //prevent issue with recycle view
            cardView.setOnTouchListener(null);
        }
matthew-reilly commented 10 years ago

Nevermind. Use this:

SwipeDismissListViewTouchListener touchListener =
 *         new SwipeDismissListViewTouchListener(
 *                 listView,
 *                 new SwipeDismissListViewTouchListener.OnDismissCallback() {
 *                     public void onDismiss(ListView listView, int[] reverseSortedPositions) {
 *                         for (int position : reverseSortedPositions) {
 *                             adapter.remove(adapter.getItem(position));
 *                         }
 *                         adapter.notifyDataSetChanged();
 *                     }
 *                 });
 * listView.setOnTouchListener(touchListener);
 * listView.setOnScrollListener(touchListener.makeScrollListener());
gabrielemariotti commented 10 years ago

I need to check it better. I think that "swipeable" cards and own ScrollListeners are incompatible.

Does scrollListener work without swipeable cards (I can't test it now) ?

matthew-reilly commented 10 years ago

Yep.

mCardListView.setOnScrollListener(mOnTouchListener.makeScrollListener());

Removing this makes it work if I define my own scrollListener. It seems there is a conflict. I'll take a look sometime this week.

Thanks for all your hard work!

ashdavies commented 10 years ago

I think storing the scroll listener and then checking for it on registering the touch listener solves it. Works for me. #98

gabrielemariotti commented 10 years ago

Merged with #98

gabrielemariotti commented 10 years ago

Sorry for delay. I've pushed a new feature to manage this case.

https://github.com/gabrielemariotti/cardslib/pull/98#issuecomment-37445397

You can also use a snapshot with gradle:

repositories {
    mavenCentral()
    maven {
        url "https://oss.sonatype.org/content/repositories/snapshots"
    }
}
dependencies {
   compile 'com.github.gabrielemariotti.cards:library:1.4.3-SNAPSHOT'
}