2015-07-11 101 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