Help us understand the problem. What is going on with this article?

SwipeRefreshLayout内のListViewにEmptyViewを設定する時の注意点

More than 1 year has passed since last update.

SwipeRefreshLayout内のListViewにEmptyViewを設定しようとしたら詰まったのでメモ。

初めのミスはListViewと同じ階層にEmptyView用のTextViewを置いていたことだった。SwipeRefreshLayoutは最初のViewしか認識してくれず、2つ目のViewは特にエラーも出ずに非表示になる。

<LinearLayout android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:id="@+id/ll_xxx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <!-- 他のView -->
    </LinearLayout>
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/srl_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/your_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:drawSelectorOnTop="false"/>
        <TextView
            android:id="@+id/empty_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/no_data_message" />
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

次にTextViewの階層をSwipeRefreshLayoutと同じにしたが、表示されなかった。これはLinearLayoutで縦にViewを並べているのが原因。SwipeRefreshLayoutを表示した段階でViewが全画面に描画されてしまっていた。Previewに表示されていない時点で気付こう・・・

<LinearLayout android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:id="@+id/ll_xxx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <!-- 他のView -->
    </LinearLayout>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/srl_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/your_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:drawSelectorOnTop="false"/>
    </android.support.v4.widget.SwipeRefreshLayout>
    <TextView
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/no_data_message" />
</LinearLayout>

最終的にSwipeRefreshLayoutとTextViewをRelativeLayoutに入れることで無事表示されるようになった。

<LinearLayout android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:id="@+id/ll_xxx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <!-- 他のView -->
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/srl_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ListView
                android:id="@+id/your_list"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:drawSelectorOnTop="false"/>
        </android.support.v4.widget.SwipeRefreshLayout>
        <TextView
            android:id="@+id/empty_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/no_data_message" />
    </RelativeLayout>
</LinearLayout>
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away