3

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;
    }
NoWar
  • 36,338
  • 80
  • 323
  • 498

2 Answers2

2

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

  1. Implement AbsListView.OnScrollListener like public class FragmentTwo extends ListFragment implements SearchView.OnQueryTextListener, AbsListView.OnScrollListener

  2. 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

NoWar
  • 36,338
  • 80
  • 323
  • 498
1

this library allows you to choose between pull-up and pull-down :

https://github.com/chrisbanes/Android-PullToRefresh

Pull Up to Refresh By default this library is set to Pull Down to Refresh, but if you want to allow Pulling Up to Refresh then you can do so. You can even set the View to enable both Pulling Up and Pulling Down using the 'both' setting. See the Customisation page for more information on how to set this.

I don't think the SwipeRefreshLayout does allow that.

Chosen
  • 847
  • 2
  • 9
  • 21