Android 如何在 ListView 中将标题布局重用为空视图 [英] Android How to reuse a header layout as an empty view in a ListView

查看:19
本文介绍了Android 如何在 ListView 中将标题布局重用为空视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目的整个生命周期中一直在努力解决这个问题.我的项目中有很多列表,其中大多数都有标题.我一直在制作一个单独的布局文件并使用 addHeaderView() 将其添加到列表中.问题是当数据(ArrayList,在我的例子中)为空时,标题不会出现.经过数小时寻找将标题布局重用为空视图的方法后,我放弃并决定在代码中复制布局并使用 setEmptyView() 将其分配为空视图.这样做的问题是很多标题都包含可点击的视图,所以我必须为每个重复的视图加倍我的所有可点击逻辑.我之前尝试包含标题但失败了,主要是因为我仍然不太了解布局如何膨胀等.

I've been grappling with this problem throughout the life of my project. I have many lists in my project and most of them have headers. I have been making a separate layout file and adding it to the list using addHeaderView(). The problem is that when the data (ArrayList, in my case) is empty, the header doesn't appear. After hours of searching for a way to reuse the header layout as an empty view, I gave up and decided to replicate the layout in code and assign it as an empty view using setEmptyView(). The problem with this is that a lot of the headers contain clickable views and so I have to double all of my clickable logic for every view that is duplicated. I tried to include the header before but failed, mostly because I still don't quite understand how layouts are inflated, etc.

最后,我想出了一个解决方案,我想与社区分享,以防其他人遇到类似问题.我不知道这是否是解决此问题的最佳方法,任何反馈或建议肯定会受到欢迎.

Finally, I have come up with a solution that I would like to share with the community, in case others are having a similar problem. I don't know if this is the best way to solve this problem and any feedback or suggestions would definitely be welcomed.

这是包含列表列表视图的布局代码:

here is the code for the layout that contains the list list view:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
>
    <ListView
        android:id="@+id/lv_my_list"
        style="@style/ListStyle"
        android:headerDividersEnabled="false"
    />
    <include
        layout="@layout/my_list_header"
        android:id="@+id/my_list_empty"
    />
</LinearLayout>

样式定义了布局的宽度和高度.

the style defines the layout width and height among other things.

现在我有了包含标题视图的布局文件

now I have the layout file that contains the header view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/my_list_header"
>
    <Button 
        android:id="@+id/my_list_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click Me"
    />
</LinearLayout>

我知道 LinearLayout 效率不高,我正在尝试使用合并或其他效率措施,但这是第一个有效的版本,所以我现在就使用它.最后是代码:

I know LinearLayout is not very efficient and I am experimenting with using merge or other efficiency measures, but this is the first version that works so I'm going with it for now. Finally the code:

// set up the header
myListView = (ListView)findViewById(R.id.lv_my_list);
View header = View.inflate(this, R.layout.my_list_header, null);
Button b = (Button)header.findViewById(R.id.my_list_button);
b.setOnClickListener(this);
myListView.addHeaderView(header);

// set up the empty view        
LinearLayout ll = (LinearLayout)findViewById(R.id.my_list_empty);
b = (Button)ll.findViewById(R.id.my_list_button);
b.setOnClickListener(this);
meLocalListView.setEmptyView(ll);

我将按钮包装在布局中的原因是因为我可以将按钮的每个实例设置为将其用作 OnClickListener,然后在我的 onClick 方法中使用一个 id 引用它们:R.id.my_list_button.我需要对此进行更多测试,但它现在似乎有效.我还没有在我的所有列表上实现它,只是一个,所以它可能不适用于所有情况.如果您有任何建议,请告诉我,因为这对我来说已经困扰了很长时间.一个问题可能是,如果您想从 ID 实例化按钮,您可能必须再次完成整个过程才能访问正确的 ID?

The reason I wrapped the button in a layout is because I can set each instance of the button to use this as an OnClickListener and then refer to both of them with a single id: R.id.my_list_button in my onClick method. I need to test this a lot more but it seems to work for now. I haven't implemented it on all my lists yet, just the one so it might not work in all situations. If you have any suggestions please let me know because this has been a problem for me for a long time now. One problem might be that if you want to instantiate the button from the ID you would probably have to go through this entire process again to access the correct IDs?

推荐答案

如果你想在列表为空的时候显示一个 ListView 的头部,你真正想做的就是显示列出本身而不是单独的空视图.因此,最简单的方法是检查您的列表是否为空,如果还没有,则将列表设置为可见:

If you want to show the header of a ListView when the list is empty, what you really want to do is to show the list itself rather than a separate empty view. So, the simplest way is to check whether your list is empty and set the list visible if it is not already:

getListView().setVisibility(View.VISIBLE);

您可以使用以下代码非常轻松地对此进行测试:

You can test this very easily with the following code:

public class TestListActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_listview);

        // create the empty grid item mapping
        String[] from = new String[] {};
        int[] to = new int[] {};

        // prepare the empty list
        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

        // fill in the grid_item layout
        SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.test_listitem, from, to);

        View listHeader = 
                ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.test_listheader, null, false);

        getListView().addHeaderView(listHeader);
        getListView().setAdapter(adapter);
    }
}

列表为空,但标题可见.

The list is empty, but the header is visible.

这篇关于Android 如何在 ListView 中将标题布局重用为空视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆