如何刷新刷新布局android

问题描述:

我有一个swipeRefresh里面的回收器视图每当我尝试在顶部滚动滑动刷新方法被调用和布局刷新,但我只想刷新布局滚动到屏幕的顶部。有没有人有这个问题的答案。如何刷新刷新布局android

任何帮助表示赞赏,谢谢

我的布局如下所示。

<android.support.v4.widget.SwipeRefreshLayout 
    android:id="@+id/swipe_container" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
     android:id="@+id/rel" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@id/toolbar" 
      android:scrollbars="vertical" /> 
    </RelativeLayout> 

</android.support.v4.widget.SwipeRefreshLayout> 
SwipeRefreshLayout

upload_swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
     @Override 
     public void onRefresh() { 

      webServiceCall(); 
      upload_swipe_refresh_layout.setRefreshing(false); 
     } 
    }); 

+0

为什么'RecyclerView'和'SwipeRefreshLayout'之间有'RelativeLayout'? “SwipeRefreshLayout”不应该是“RecyclerView”的直接父代吗?请参阅https://developer.android.com/training/swipe/add-swipe-in​​terface.html – frozenkoi

我解决我的问题。我不得不重新安排我的代码才能正常工作。 现在,在到达屏幕顶部时,刷新刷新。

<RelativeLayout 
    android:id="@+id/rel" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     app:layout_collapseMode="pin" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/swipe_container" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/toolbar"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@id/swipe_container" 
      android:scrollbars="vertical" /> 

    </android.support.v4.widget.SwipeRefreshLayout> 
</RelativeLayout> 

使用setOnRefreshListener方法对于不断刷新

upload_swipe_refresh_layout.post(new Runnable() { 
    @Override 
    public void run() { 
     upload_swipe_refresh_layout.setRefreshing(true); 
    } 
}); 
+0

我正在使用setOnRefreshListener,但它每次尝试向上滚动时都会调用它。而我想刷新到达屏幕的顶部。 –

+0

然后你可以保持它一直刷新 –

+0

我编辑了我的答案 –