2015-07-11 145 views
4

我已經在我的列表視圖之一上實現了刷卡刷新佈局,但是當我滑動並且圖標不可見時,視圖不會滑落。即使它看起來不起作用,它確實會觸發OnRefresh偵聽器。刷新刷新佈局onRefresh工作但不顯示刷新指示器

另外我覺得值得一提的是我在一個片段上使用了ListView,因此我正在初始化下面顯示的片段活動中的監聽器。

刷卡刷新XML

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

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/listView" 
     android:layout_gravity="center_horizontal" 
     android:dividerHeight="1sp" 
     android:translationZ="5dp" 
     android:background="@color/background_gray" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:clickable="true" 
     /> 

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

片段活動

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 

    super.onViewCreated(view, savedInstanceState); 

    final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipe_refresh_layout); 
    ListView lView = (ListView) getActivity().findViewById(R.id.listView); 

    mSwipeRefreshLayout.setColorScheme(android.R.color.holo_blue_bright, 
      android.R.color.holo_green_light, 
      android.R.color.holo_orange_light, 
      android.R.color.holo_red_light); 

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
     @Override 
     public void onRefresh() { 
      mSwipeRefreshLayout.setRefreshing(true); 
      ((MainActivity) getActivity()).refresh(); 
      new Handler().postDelayed(new Runnable() { 
       @Override public void run() { 
        mSwipeRefreshLayout.setRefreshing(false); 
       } 
      }, 5000); 




     } 
    }); 
    lView.setOnScrollListener(new AbsListView.OnScrollListener() { 
     @Override 
     public void onScrollStateChanged(AbsListView absListView, int i) { 

     } 

     @Override 
     public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
      if (firstVisibleItem == 0) 
       mSwipeRefreshLayout.setEnabled(true); 
      else 

       mSwipeRefreshLayout.setEnabled(false); 
     } 
    }); 

} 
+0

也許這將幫助:http://stackoverflow.com/questions/26858692/swiperefreshlayout-setrefreshing-not-showing-indicator-initially – Kay

+0

感謝@Kay但這並不解決任何的問題我。該線程上的解決方案與我目前的解決方案一樣。 –

回答

4

我解決了它,它看來,這是當前的錯誤與SwipeRefreshLayout。它只會在Listview包裝在FrameLayout中時才起作用。

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

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/swipe_refresh_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/listView" 
     android:layout_gravity="center_horizontal" 
     android:dividerHeight="1sp" 
     android:translationZ="5dp" 
     android:background="@color/background_gray" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:clickable="true" 
     /> 
    </FrameLayout> 

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

只需設置屬性[機器人:可點擊=「真」]爲SwipeRefreshLayout直屬clild佈局佔據完整屏幕area.i.e.其寬度和高度是match_parent。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/srlayout_Container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true"> 

     <RelativeLayout 
      android:id="@+id/rl_MainContainer" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:clickable="true"> 
      <!-- Placeholder for Other View or ViewGroup as Clild --> 
     </RelativeLayout> 
    </android.support.v4.widget.SwipeRefreshLayout> 
</RelativeLayout> 
+0

工程很好,非常感謝。 – 0neel