2016-03-24 44 views
0

因此,我使用ViewPager和其中的2個片段進行活動。在每個片段中,我放置了SwipeToRefreshLayout和一個ListView。 Xml看起來像這樣:使用ViewPager片段中的SwipeToRefresh佈局問題

<?xml version="1.0" encoding="utf-8"?> 

*<android.support.v4.widget.SwipeRefreshLayout 
    android:id="@+id/going_out_activity_swipe_to_refresh" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical">* 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ListView 
     android:id="@+id/going_out_activity_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab_going_out" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_tab_new"/> 


</RelativeLayout> 


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

現在,出現了問題。當列表超出我的屏幕,我不得不向下滾動查看項目,當我嘗試向後滾動時,它不會滾動,它會刷新(調用SwipeToRefreshLayout)。我怎樣才能解決這個問題?

回答

1

android docs摘自:

他們用ListFragment,但你應該能夠做必要的改變: 您應該添加和使用自定義SwipeRefreshLayout,其中定義滾動條件:

private class ListFragmentSwipeRefreshLayout extends SwipeRefreshLayout { 

    public ListFragmentSwipeRefreshLayout(Context context) { 
     super(context); 
    } 

    /** 
    * As mentioned above, we need to override this method to properly signal when a 
    * 'swipe-to-refresh' is possible. 
    * 
    * @return true if the {@link android.widget.ListView} is visible and can scroll up. 
    */ 
    @Override 
    public boolean canChildScrollUp() { 
     final ListView listView = getListView(); 
     if (listView.getVisibility() == View.VISIBLE) { 
      return canListViewScrollUp(listView); 
     } else { 
      return false; 
     } 
    } 

} 

/** 
* Utility method to check whether a {@link ListView} can scroll up from it's current position. 
* Handles platform version differences, providing backwards compatible functionality where 
* needed. 
*/ 
private static boolean canListViewScrollUp(ListView listView) { 
    if (android.os.Build.VERSION.SDK_INT >= 14) { 
     // For ICS and above we can call canScrollVertically() to determine this 
     return ViewCompat.canScrollVertically(listView, -1); 
    } else { 
     // Pre-ICS we need to manually check the first visible item and the child view's top 
     // value 
     return listView.getChildCount() > 0 && 
       (listView.getFirstVisiblePosition() > 0 
         || listView.getChildAt(0).getTop() < listView.getPaddingTop()); 
    } 
} 
+0

這一個工程像列表視圖魅力。謝謝。 –

0

嘗試將滑動刷新佈局添加到您的ListView只。

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
android:id="@+id/going_out_activity_swipe_to_refresh" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical">* 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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


    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab_going_out" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_tab_new"/> 


</RelativeLayout> 

1

這是使用時的錯誤0與SwipeToRefreshLayout。您可以通過使用RecyclerView而不是ListView來輕鬆修復該問題。

但是,如果你想保留使用ListView,嘗試用下面的代碼:

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

      @Override 
      public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
       boolean enable = false; 
       if (ptrListView != null && mListView.getChildCount() > 0) { 
        boolean firstItemVisible = (mListView.getFirstVisiblePosition() == 0); 
        boolean topOfFirstItemVisible = (mListView.getChildAt(0).getTop() == 0); 
        enable = firstItemVisible && topOfFirstItemVisible; 
       } 
       mSwipeRefreshLayout.setEnabled(enable); 
      } 
     }); 
0

這是我做到了,它滾動完美,只有當你向下滑動,當你一路刷新頂端。我希望這有幫助。

<?xml version="1.0" encoding="utf-8"?> 
<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:orientation="vertical" 
tools:context="com.newthinktank.listviewexample2.app.MainActivity" 
android:background="@drawable/background2" 
android:layout_marginTop="65dp" 
android:weightSum="1"> 

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

    <ListView 
     android:id="@+id/lv1" 
     android:layout_marginTop="15dp" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:background="#f0f0f0" 
     android:alpha="0.90" 
     android:layout_weight="1.01" /> 

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

這是我如何使用swipetorefresh:

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout); 

    mSwipeRefreshLayout.setOnRefreshListener(
      new SwipeRefreshLayout.OnRefreshListener() { 
       @Override 
       public void onRefresh() { 
        finish(); 
        startActivity(getIntent()); 
       } 
      } 
    );