I have implemented SwipeRefreshLayout and it works fine basically. https://developer.android.com/training/swipe/add-swipe-interface.html
I am facing weired issue when you go to the bottom of the list and when you try to scroll to top the SwipeRefreshLayout refreshes.
That is not what I need. I need just scroll to the top of the List and not refresh the ListView.
I mean it should be some condition when to refresh depending where we stand. Right? Because the expecting behavior is allow refresh when we at the top of the ListView.
Please help.
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<SearchView
android:id="@+id/editSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
android:queryHint="Input the number"
/>
<ListView
android:paddingTop="5dp"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
And the code
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment_two, container, false);
swiperefresh = (SwipeRefreshLayout) view.findViewById(R.id.swiperefresh);
swiperefresh.setColorScheme(R.color.colorPrimary, R.color.colorAccent, R.color.colorPrimaryDark, R.color.colorPrimary);
swiperefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swiperefresh.setRefreshing(true);
Log.d("Swipe", "Refreshing Number");
// Get data from WebAPI or database cache
}
});
return view;
}
Well... Finally I found solution here Scroll up does not work with SwipeRefreshLayout in Listview
So my layout is not gonna be changed but the code yes.
We have to do following
Implement AbsListView.OnScrollListener like public
class FragmentTwo extends ListFragment implements SearchView.OnQueryTextListener, AbsListView.OnScrollListener
Add two methods of the implemented interface AbsListView.OnScrollListener
like here
@Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { swiperefresh.setEnabled(firstVisibleItem == 0); }
And the key here is
swiperefresh.setEnabled(firstVisibleItem == 0);
implements OnScrollListener inside listFragment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With