滑动以刷新图标未正确显示

时间:2015-01-23 09:28:31

标签: android listview pull-to-refresh swiperefreshlayout

我正在使用ListView并在使用SwipeRefresh向下滑动时刷新它,直到此处它才能正常工作。当我尝试在TextView上设置mListview.setEmptyView()时,会出现问题,而在滑动时未显示Refresh Icon

它看起来像TextView。但是,如果我不将TextView放在同一个XML上,它可以正常工作,但我需要在列表为空时显示一条消息。 导致此行为的原因是什么?

这是我使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/padding_light"
    android:paddingRight="@dimen/padding_light">

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

        <ListView
            android:id="@+id/mListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

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

    <TextView
        android:id="@+id/swipeToRefreshTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="15dp"
        android:gravity="center_horizontal"
        android:text="@string/msg_swipe_to_refresh"
        android:visibility="gone" />

</RelativeLayout>

这是Refresh监听器:

//Listeners
mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {

        //Here I do some job
        beaconScanSwipeRefresh.setRefreshing(false);
    }
});

这是我设置ListView和onEmptyView:

的适配器的地方
//UI
mListLv = (ListView) view.findViewById(R.id.mListView);
mSwipeRefresh=(SwipeRefreshLayout)view.findViewById(R.id.swipeRefreshLayout);
swipeToRefreshTv = (TextView)view.findViewById(R.id.swipeToRefreshTextView);

//Set Adapter
mListLv.setAdapter(mBaseAdapter);

//Set Message when list is empty
mListLv.setEmptyView(swipeToRefreshTv);

我试图将TextView移到<android.support.v4.widget.SwipeRefreshLayout标签内或其上方。结果还是一样。

3 个答案:

答案 0 :(得分:1)

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/padding_light"
    android:paddingRight="@dimen/padding_light">

<TextView
        android:id="@+id/swipeToRefreshTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"        
        android:gravity="center_horizontal"
        android:text="@string/msg_swipe_to_refresh"
        android:visibility="gone" />

    <android.support.v4.widget.SwipeRefreshLayout
        //below = swipeToRefreshTextView
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">

        <ListView
            android:id="@+id/mListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

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



</RelativeLayout>

请以这种方式更新您的布局并检查。

答案 1 :(得分:0)

只要您的SwipeRefreshLayoutTextView位于同一RelativeLayout下,其中一个就会相互重叠。

我会尝试将它放在与ListView相同的级别,然后将它们放在容器内SwipeRefreshLayout内。由于SwipeRefreshLayout是布局中的单个项目,因此您可以删除外部RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/padding_light"
    android:paddingRight="@dimen/padding_light">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView
            android:id="@+id/mListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <TextView
            android:id="@+id/swipeToRefreshTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="15dp"
            android:gravity="center_horizontal"
            android:text="@string/msg_swipe_to_refresh"
            android:visibility="gone" />
    </FrameLayout>

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

答案 2 :(得分:0)

您可以尝试将ListViewRecyclerView放入ScrollView。对我而言,这就是诀窍。

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/mListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>
相关问题