2015-06-13 18 views
7

Co chcę zrobić, pozwolić użytkownikowi na przesuwanie i odświeżanie listy, nie ma znaczenia, czy dane tam są, czy nie. Ale gdy dane znajdują się w widoku listy, widok listy powinien być widoczny, a gdy nie ma tam danych, powinien być widoczny Pusty widok tekstowy. W obu przypadkach użytkownik musi mieć możliwość odświeżenia listy przy pomocy machnięcia.Android SwipeRefreshLayout z pustym TextView nie działa poprawnie

Próbowałem jakieś rozwiązanie danego here in this discussion ale żaden z nich dźwięki pracy, idea biorąc dwa SwipeToRefresh działa dobrze jak podano Here, ale pokazuje pusty pojemnik nawet podczas pobierania danych z serwera.

próbowałem go z moim ListView owijania logika i TextView wewnątrz względnej/FrameLayout ale SwipeToRefresh akceptuje jeden widok tylko za jego zachowanie

Oto fragment kodu xml Próbowałem

<android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/activity_main_swipe_refresh_layout" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.945" > 


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

      <ListView 
       android:id="@+id/event_list_eventlist" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:divider="@color/event_background" 
       android:dividerHeight="1dp" > 
      </ListView> 

      <TextView 
       android:id="@+id/event_txt_nothing_found" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerInParent="true" 
       android:layout_centerVertical="true" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textColor="@android:color/darker_gray" 
       android:visibility="gone" /> 
     </RelativeLayout> 
    </android.support.v4.widget.SwipeRefreshLayout> 

proszę pomoc

Odpowiedz

11

I rozwiązać ten sam problem przy użyciu FrameLayout, spojrzeć na poniższy kod:

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

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

     <ListView 
      android:id="@+id/lv_products" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:divider="@null" /> 

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

    <TextView 
     android:id="@+id/tv_no_product" 
     style="@android:style/TextAppearance.Medium" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_margin="10dp" 
     android:gravity="center" 
     android:text="@string/msg_no_listing" 
     android:textColor="@color/primary_green" 
     android:visibility="gone" /> 
</FrameLayout> 

Można również użyć RelativeLayout, ale kluczem jest umieszczenie listView wewnątrz obiektu SwipeRefreshLayout, a TextView na zewnątrz.

+0

i co robić w działalności? to znaczy, czy ta implementacja umożliwia ponowne przesuwanie, jeśli lista jest pusta? –

+1

Oczywiście, możesz przesunąć ponownie, jeśli lista jest pusta. Jestem pewien, że działa w 100%. –

+0

Czy możesz podać trochę kodu aktywności, jeśli to możliwe? –

12

Miałem również ten problem i rozwiązałem go bez żadnego dodatkowego kodu w działaniu, korzystając z poniższego układu.

Jeśli używasz ListActivity lub ListFragment obsługuje pokazywanie/ukrywanie pusty widok dla ciebie, i orzeźwiające prace z pustą listę, a także z tej struktury układu.

Nie potrzeba żadnych haseł, wszystko jest w wersji SwipeRefreshLayout, tak jak być powinno.

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/Refresher" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <ListView 
      android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:divider="@null" /> 
     <ScrollView 
      android:id="@android:id/empty" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <TextView 
       android:text="Geen formulieren gevonden" 
       style="@style/text.EmptyView" /> 
     </ScrollView> 
    </LinearLayout> 
</android.support.v4.widget.SwipeRefreshLayout> 

Mam nadzieję, że pomoże każdemu, kto zmaga się z tym problemem.

+0

dziękuję, działa jak szkoda! – mklimek

+0

nie szkoda przyjacielu :) – HendraWD

+0

Świetnie, działa dobrze. Zastanawiam się, dlaczego? czy jest jakieś wytłumaczenie tego? – GVillani82

0

w końcu rozwiązać ten problem stosując następujący kod

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

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/recycler_shop_list" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

      <ScrollView 
       android:gravity="center" 
       android:fillViewport="true" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <com.forysta.inloyal.view.textviews.RegularTextView 
        android:id="@+id/tv_no_shop_data" 
        style="@style/text_Size_16" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:gravity="center" 
        android:text="@string/alert_nodatashop" 
        android:visibility="visible" 
        app:layout_collapseMode="parallax" /> 
      </ScrollView> 
     </LinearLayout> 
    </android.support.v4.widget.SwipeRefreshLayout> 
0

Mój problem wydaje się być nieudokumentowana, ale jeśli adapter dla widoku recyklera nie ma danych, i nazwali funkcja setLayoutManager, to widok recyklingowa staje un-swipeable. Moim rozwiązaniem było wywołanie tylko setLayoutManager po otrzymaniu niektórych danych.

Powiązane problemy