0

this is my xml code .As you can see ,there is a listview inside SwipeRefreshLayout .

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#EEEEEE"
        android:dividerHeight="10dp"
        android:visibility="gone" >
    </ListView>
</LinearLayout>

the problem is , when I scroll down and for example 20 new items add to my list ,I don't want to SwipeRefreshLayout calls in the middle of list ,I want user goes to the top of the list and if he was in the top of the list , then swiperefreshlayout calls .

this is the java code:

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            new GetContacts(0).execute();// 
            mSwipeRefreshLayout.setColorScheme(R.color.orange, R.color.green, R.color.blue, R.color.purple);
        }
    });

it calls for an asyncTask and when async finished ,it'll disappear

How can I only call for swiperefreshlayout when user reach at the top of the list?

thanks

mohamad bagheri
  • 499
  • 1
  • 10
  • 25

1 Answers1

0

This post is duplicated. Here is a solution for your problem.

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

  }

  @Override
  public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    int topRowVerticalPosition = 
      (guidesList == null || guidesList.getChildCount() == 0) ? 
        0 : guidesList.getChildAt(0).getTop();
    swipeContainer.setEnabled(firstVisibleItem == 0 && topRowVerticalPosition >= 0);
  }
});

You can find more about that here:

  1. http://nlopez.io/swiperefreshlayout-with-listview-done-right/
  2. SwipeRefreshLayout - swipe down to refresh but not move the view pull down
Pang
  • 9,564
  • 146
  • 81
  • 122
Eliasz Kubala
  • 3,836
  • 1
  • 23
  • 28