i++

プログラム系のメモ書きなど

Android : SwipeRefreshLayout で ListView に EmptyView を設定すると ListView が空の時に Refresh 表示が上手くされない問題を解消する

ListView.setEmptyView で Empty View を設定していると、ListView が空の状態で SwipeRefresh (PullToRefresh) を実行すると Refresh の Indicator (上の方でくるくる回る、setRefreshing でコントロールされるもの)が、更新が終わった瞬間辺りに一瞬表示されるだけになるなど、不自然な見た目になってしまいました。

これはどうやら

  • SwipeRefreshView の Refresh 表示は ListView の Visibility に依存している
  • Empty View を設定していると、ListView は空になると自動で非表示にされる

という組み合わせで起こる挙動のようです。

この挙動を「Empty View をセットしても、空の状態で非表示にならない ListView」を作成・使用することで回避します。

カスタム ListView

カスタムといっても、setVisibility を override して Visibility の変更を防ぐ、ただそれだけです。

私の用途では ListView の Visibility を変える予定がない(常に Visible で問題ない)ので、全く何もしないようにしていますが、「空である / 空ではない」以外の要件で Visibility を自分でコントロールする必要がある場合は、参考先(下記記載)のように項目数で if 分岐をつけると良いようです。

※ Kotlin

public class ListViewForSwipeRefreshLayout : ListView {
    public constructor(context: Context) : super(context) {}
    public constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
    public constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    override fun setVisibility(visibility: Int) {}
}

layout.xml

ListView を使っているところを、上記のカスタム ListView で置き換えます(パッケージ名は適当です。自分のものに合わせて下さい)。それ以外は SwipeRefreshLayout の標準的な使い方と同じだと思います。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/empty"
        android:textAlignment="center"
        android:textColor="@android:color/primary_text_light"
        android:layout_centerInParent="true"/>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.example.swiperefresh.ListViewForSwipeRefreshLayout 
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false">
        </com.example.swiperefresh.ListViewForSwipeRefreshLayout>
    </android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>

参考