Everytime I scroll the list up or down I hide (or unhide) some views with OnScrollListener
. Here is the code attached to my ListView
.
lv.setOnScrollListener(new OnScrollListener() {
private int mLastFirstVisibleItem;
private boolean mIsScrollingUp = true;
private LinearLayout ll = (LinearLayout) getActivity()
.findViewById(R.id.llSearchPlaces);
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (view.getId() == lv.getId()) {
final int currentFirstVisibleItem = lv
.getFirstVisiblePosition();
if (currentFirstVisibleItem > mLastFirstVisibleItem) {
if (mIsScrollingUp == true) {
mIsScrollingUp = false;
Log.i("a", "scrolling down...");
floatingActionButton.hide();
Animation animation = new TranslateAnimation(0, 0,
0, 200);
animation.setDuration(300);
animation
.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(
Animation animation) {
ll.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationRepeat(
Animation animation) {
}
@Override
public void onAnimationStart(
Animation animation) {
}
});
ll.startAnimation(animation);
}
} else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
if (mIsScrollingUp == false) {
mIsScrollingUp = true;
floatingActionButton.show();
Log.i("a", "scrolling up...");
Animation animation = new TranslateAnimation(0, 0,
200, 0);
animation.setDuration(400);
animation
.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(
Animation animation) {
}
@Override
public void onAnimationRepeat(
Animation animation) {
}
@Override
public void onAnimationStart(
Animation animation) {
ll.setVisibility(View.VISIBLE);
}
});
ll.startAnimation(animation);
}
}
mLastFirstVisibleItem = currentFirstVisibleItem;
}
}
});
Layout:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="true"
android:listSelector="#00000000"
android:scrollbars="none" />
</android.support.v4.widget.SwipeRefreshLayout>
Ever since I added the SwipeRefreshLayout
, I do not get anything when I Log
inside the listener above. How can I use both of these items together?
EDIT: It seems like this is what I need but I can't make it work still
As part of that article I added this in onScroll
, tho it doesn't seem to work.
if (firstVisibleItem == 0) {
swipeLayout.setEnabled(true);
} else {
swipeLayout.setEnabled(false);
}
EDIT2: THIS IS THE HEART OF THE ISSUE: It seems the onScroll
method fires when the Activity first starts and the list loads and then never more again.
You should override
canChildScrollUp
ofSwipeRefreshView
. This method is polled for the purpose of knowing whether the contained View wants to scroll up:An extension with a settable interface for any arbritrary contained view:
Usage (
ListView
also has a nice method:canScrollVertically()
):I had the same issue with
RecyclerView
andListView
. Scrolling downwards for whatever amount of items, it was impossible to return to the top of the list.This will disable the
SwipeRefreshLayout
until the first visible item or any item position is visible. You can also bind different scroll listeners along this one. Make sure you enable (if previously disabled)SwipeRefreshLayout
whenever you repopulate the listivew.Edit:
By default : private boolean flag_loading = false; and in your pagination async if(YourListview.size() < 20) { flag_loading = true; } else{ flag_loading = false; }