2016-03-03 15 views
1

我想實現一個功能,如果填充列表視圖的遊標是空的,將顯示一個圖像而不是列表視圖。Android如果沒有結果替換Listview與圖像

我試圖把圖像中,但它與上面或下面的列表視圖和遺址佈局

我使用的是listFragment的時刻,但我的問題是定位的圖像。我如何放置它,使圖像在被調用之前「不可見」?

XML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/coordinatorLayout"> 
<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/filterButton" 
    app:backgroundTint="@color/floatingButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|right" 
    android:clickable="true" 
    android:src="@drawable/ic_filter" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    android:layout_marginBottom="63dp" 
    android:layout_marginRight="16dp" /> 


</android.support.design.widget.CoordinatorLayout> 

代碼

if(cursor!=null && cursor.getCount()>0) 
         { 
          //call to populate the image? 
          Toast.makeText(getActivity(), "Sorry no match, please refine search", Toast.LENGTH_SHORT).show(); 

         } 

         else 
         { 
          //display list as normal 

         } 

回答

1

將imageview添加到您的xml 中,將其可見性消除。

你的光標-if空集imageview.setVisibility(View.VISIBLE)listView.setVisibility(View.GONE) 在你的快樂狀況中做相反的事情。

不要忘記預先通過它們的ID來初始化變量imageView和listView。

你的XML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/coordinatorLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"></ListView> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/yourpicture" 
     android:visibility="gone" 
     android:id="@+id/imageview"/> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/filterButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_gravity="bottom|right" 
     android:layout_marginBottom="63dp" 
     android:layout_marginRight="16dp" 
     android:clickable="true" 
     android:src="@drawable/ic_filter" 
     app:backgroundTint="@color/floatingButton" /> 


</android.support.design.widget.CoordinatorLayout> 

代碼

ImageView imageView = (ImageView) findViewById(R.id.imageview); 
    ListView listView = (ListView) findViewById(R.id.listview); 
    if(cursor!=null && cursor.getCount()>0){ 

     imageView.setVisibility(View.VISIBLE); 
     listView.setVisibility(View.GONE); 

    }else{ 
     imageView.setVisibility(View.GONE); 
     listView.setVisibility(View.VISIBLE); 
    } 
+0

您選擇welcome..happy編碼! (: – yUdoDis

1

您可以設置ListView的能見度消失,圖像視圖時可見沒有數據。有數據時做相反的事情。

或者,您可以使用列表適配器的setEmptyView API。

+0

感謝您的建議,得到了它的工作現在+1 – JJSmith

2

到@yUdoDis答案相似,但我建議ViewSwitcher

你layout.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/coordinatorLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ViewSwitcher 
    android:id="@android:id/empty" 
    android:visibility="gone" 
    android:layout_weight="0" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"></ListView> 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/yourpicture" 
    android:visibility="gone" 
    android:id="@+id/imageview"/> 

</ViewSwitcher> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/filterButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    android:layout_gravity="bottom|right" 
    android:layout_marginBottom="63dp" 
    android:layout_marginRight="16dp" 
    android:clickable="true" 
    android:src="@drawable/ic_filter" 
    app:backgroundTint="@color/floatingButton" /> 


</android.support.design.widget.CoordinatorLayout> 

在你的活動代碼:

ViewSwitcher switcher = (ViewSwitcher) findViewById(android.R.id.empty); 
if(cursor!=null && cursor.getCount()>0){ 
    switcher.setDisplayedChild(1); 
}else{ 
    switcher.setDisplayedChild(0); 
} 
+1

感謝您的建議,遇到了一些問題,所以去了yUdoDis的答案,加上一個雖然! – JJSmith

+1

我真的很喜歡使用viewswitcher的概念...在這裏拿我的餅乾。 – yUdoDis