Android ListView自定义标题 [英] Android ListView custom header
问题描述
我有一个自定义列表视图,它是可重新排列的(拖放和拖放),显然是可滚动的。我需要添加一个自定义标题与此列表视图应该滚动与列表。
I have a custom listview, which is re-arrangeable(drag & drop) and obviously scrollable. I need to add a custom header with this listview which should scroll with the list.
我已经尝试了listView.addHeaderView(标题)与自定义标题布局。但是在列表重新排列期间,标题变得混乱了。所以我不想使用addHeaderView(...)。
I have tried "listView.addHeaderView(header)" with a custom header layout. But header becomes messed up during list re-arrangement. So I don't want to use addHeaderView(...).
我还试图把我的活动布局是一个LinearLayout,在ScrollView中,并展开列表如下所示 - 我如何将ListView放入ScrollView而不折叠?但是,滚动似乎很慢,ScrollView在列表项重新排列中创建了问题。
I have also tried to put my activity layout which is a LinearLayout, inside a ScrollView and expand the list as suggested here - How can I put a ListView into a ScrollView without it collapsing?. But, scrolling seems slow and ScrollView is creating issue in list item re-arrangement.
还有其他方法可以实现这个效果吗?我更喜欢在布局中使用一些解决方案。
So is there any other way to accomplish this effect? I would prefer some solution in layout.
我在Android界面上没有经验。感谢提前。
I am fairly inexperienced in Android UI. Thanks in advance.
推荐答案
您可以使用以下代码添加标题视图到您的列表,
You can add a Header View to your list, using the following code,
View header = getLayoutInflater().inflate(R.layout.header, null);
ListView listView = getListView();
listView.addHeaderView(header);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,android.R.id.text1, names));
但是,为此,您需要为标题视图定义layour,请检查这个使用页眉或页脚链接Android ListActivity
But for this, you need to define layour for the Header View, check this Link Android ListActivity with a header or footer
编辑 - 根据评论,我想提一下,使用 ListView
不是一个好习惯里面有一个 ScrollView
。到目前为止,我发现最好的解决方案是将 HeaderView
添加到您的 ListView
中。如果OP可以提到,他会遇到什么问题? HeaderView
Edit- As per comment, I would like to mention that, It is not a good practice to use a ListView
inside a ScrollView
. The best possible solution, I found so far is to add HeaderView
to your ListView
. It would be better if the OP could mention, what problems he is facing with the HeaderView
如果你想要一个可滚动标题,你一定要用HeaderView去。但是如果要在ListView上方有一个静态标题,可以使用TextView作为ListView的标题。
If you want a scrollable header, you should definitely go with the HeaderView. But if you want to have a static header above the ListView, you could use a TextView as a Heading of the ListView.
这篇关于Android ListView自定义标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!