2015-01-21 46 views
1

我指的是this教程刷新ListView ,它的工作很好。但問題是我有一個Layout以上ListView因此,當我運行我的應用程序Listview獲得覆蓋Layout。爲了避免這種情況,我將這個佈局作爲標題ListView,但隨後出現了一個新問題。當列表爲空時ListViewHeader也未顯示。我也在Adapter中使用isEmpty(),它仍然沒有顯示Header使用PullToRefreshLayout時顯示列表視圖標題

類:

 @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    activityView = layoutInflater.inflate(R.layout.groups_activity_layout, null, false); 

    setContentView(activityView); 



    /******************************************************************************/ 
    ViewGroup viewGroup = (ViewGroup) activityView; 
    // As we're using a ListFragment we create a PullToRefreshLayout manually 
    mPullToRefreshLayout = new PullToRefreshLayout(ItemscreenActivity.this); 

    // We can now setup the PullToRefreshLayout 
    ActionBarPullToRefresh.from(this) 
    // We need to insert the PullToRefreshLayout into the Fragment's ViewGroup 
    .insertLayoutInto(viewGroup) 
    // Here we mark just the ListView and it's Empty View as pullable 
    .theseChildrenArePullable(R.id.listView_demo_Items_gsal, android.R.id.empty).listener(this).setup(mPullToRefreshLayout); 

    /******************************************************************************/ 

    arrayListOfAItems.clear(); 
    populateItemsArrayList(ItemscreenActivity.this); 

    ItemsListWrtCnt++; 
    // Getting the reference of Button and ListView 
    listViewOfItems = (ListView) findViewById(R.id.listView_demo_Items_gsal); 

    // Get Data in Adapter to set on ListView 
    ItemscreenActivityAdapter = new ItemscreenAdapter(ItemscreenActivity.this, R.layout.group_screen_adapter_layout, arrayListOfAItems); 
    listViewOfItems.setTextFilterEnabled(false); 
    listViewOfItems.setScrollingCacheEnabled(false); 
    listViewOfItems.setCacheColorHint(Color.TRANSPARENT); 

    LayoutInflater inflater = getLayoutInflater(); 
    ViewGroup header = (ViewGroup) inflater.inflate(R.layout.group_activity_listview_header, listViewOfItems, false); 
    listViewOfItems.addHeaderView(header, null, false); 
    listViewOfItems.setEmptyView(header); 
    listViewOfItems.setVisibility(View.VISIBLE); 

    if (arrayListOfAItems.size() > 0) { 
     // Set Adapter to ListView of group if there are Items 
     ItemscreenActivityAdapter.notifyDataSetChanged(); 
     listViewOfItems.setAdapter(ItemscreenActivityAdapter); 
     ItemsListWrtCnt--; 
    } else { 
     // Show Toast if no group to display 
     Toast.makeText(ItemscreenActivity.this, "No Items", Toast.LENGTH_SHORT).show(); 
    } 


} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<com.oi.demo.actionbarpulltorefresh.library.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layout_Items" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical" 
    tools:ignore="UselessParent" > 

    <ListView 
     android:id="@+id/listView_demo_Items_gsal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/view_layout_footer_gsal" 
     android:animationCache="false" 
     android:background="@android:color/transparent" 
     android:cacheColorHint="@android:color/transparent" 
     android:divider="@null" 
     android:persistentDrawingCache="scrolling" 
     android:scrollbarStyle="outsideOverlay" 
     android:scrollbars="vertical" 
     android:scrollingCache="false" 
     android:smoothScrollbar="true" /> 

    <View 
     android:id="@+id/view_layout_footer_gsal" 
     android:layout_width="fill_parent" 
     android:layout_height="5dp" 
     android:layout_gravity="bottom" 
     android:background="#952d4a" /> 

</com.oi.demo.actionbarpulltorefresh.library.PullToRefreshLayout> 
+0

請張貼您的xml文件代碼。同時在你的列表視圖中顯示綁定數據的代碼。 – GrIsHu 2015-01-21 06:19:06

+0

@GrlsHu我加了我的代碼,請檢查它.. – Akshay 2015-01-21 06:41:42

+0

@Akki試着改變這個'android:layout_height =「wrap_content」'到'android:layout_height =「match_parent」'看看是否有幫助(雖然沒有嘗試,但這是我最好的猜測) – kha 2015-01-21 08:21:47

回答

2

您需要做的僅僅是設置適配器出方的,如果條件。如:

if (arrayListOfAItems.size() > 0) { 
    // Set Adapter to ListView of group if there are Items 
    ItemscreenActivityAdapter.notifyDataSetChanged(); 

    ItemsListWrtCnt--; 
} else { 
    // Show Toast if no group to display 
    Toast.makeText(ItemscreenActivity.this, "No Items", Toast.LENGTH_SHORT).show(); 
} 

listViewOfItems.setAdapter(ItemscreenActivityAdapter); 

至於顯示標題,你需要設置適配器的任何方式。 雖然列表爲空,但未設置適配器,因此您無法看到標題。

試試吧。它會工作肯定。

+0

我忘記從條件移動... – Akshay 2015-01-21 10:20:29