刷新刷新布局onRefresh工作但不显示刷新指示器

时间:2015-07-11 02:24:41

标签: android android-fragments android-listview

我在其中一个列表视图上实现了滑动刷新布局,但是当我滑动并且图标不可见时,视图不会滑落。即使看起来它不起作用,它确实会触发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);
        }
    });

}

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:2)

只需为SwipeRefreshLayout的立即clild布局设置属性[ android:clickable =“true”],该布局占据整个屏幕区域.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>