如何的Andr​​oid重用一个头布局在ListView空视图 [英] Android How to reuse a header layout as an empty view in a ListView

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

问题描述

我一直在努力解决我的整个项目生命周期这个问题。我有很多的名单在我的项目,其中大部分都有头。我一直在一个单独的布局文件和使用addHeaderView()将其添​​加到列表中。的问题是,当该数据(ArrayList中,在我的情况)是空的,首标不会出现。经过寻找一种重用头布局空观点小时,我放弃了,决定复制在code中的布局,并使用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.

这里是$ C $下包含列表列表视图中的布局:

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是不是很有效,我尝试使用合并或其他提高效率的措施,但这是工作,所以我用它去为现在的第一个版本。最后,code:

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的每个实例,然后参考他们两个人用一个ID:R.id.my_list_button在我的onClick方法。我需要测试这个多很多,但它似乎工作现在。我还没有实现它在我所有的名单还没有,只是一所以它可能并不适用于所有情况。如果您有任何建议,请让我知道,因为这一直是个问题,我很长一段时间了。一个问题可能是,如果你想要实例从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);

您可以很容易测试用下面的code:

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.

这篇关于如何的Andr​​oid重用一个头布局在ListView空视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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