首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用PullToRefreshLayout时显示Listview标题

使用PullToRefreshLayout时显示Listview标题
EN

Stack Overflow用户
提问于 2015-01-21 05:34:34
回答 1查看 148关注 0票数 1

我指的是教程来刷新ListView,它运行得很好。但问题是,我在ListView之上有一个ListView,所以当我运行应用程序时,Listview会重写该Layout。为了避免这种情况,我将布局作为ListView的头,但随后出现了一个新的问题。当列表为空时,ListView也不会显示Header。我还在Adapter中使用了isEmpty(),但它没有显示Header

类:

代码语言:javascript
运行
复制
        @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:

代码语言:javascript
运行
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-21 10:18:36

您需要做的只是将适配器设置在if条件的外侧。作为:

代码语言:javascript
运行
复制
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);

至于显示标题,您需要以任何方式设置适配器。当您的列表为空时,适配器没有设置,因此无法看到标头。

试试看。它一定会起作用的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28060288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档