Android:如何隐藏 ListView 项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2638160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to hide a ListView Item
提问by Tawani
How can you hide an item in a ListView or at least set its height to zero?
如何隐藏 ListView 中的项目或至少将其高度设置为零?
I have tried setting the visibility of the View to GONE but it still maintains the item's space (height).
我尝试将视图的可见性设置为 GONE,但它仍然保持项目的空间(高度)。
回答by Tonithy
Ressurecting an old question, but I just had this issue where I wanted to hide list items temporarily based upon criteria outside of the list data. What I ended up doing was creating a "null item" layout in xml and returned that based upon my criteria instead of the convert view in getView()...
重新提出一个老问题,但我只是遇到了这个问题,我想根据列表数据之外的条件暂时隐藏列表项。我最终做的是在 xml 中创建一个“空项目”布局,并根据我的标准而不是 getView() 中的转换视图返回该布局...
instead of returning the convertView, I returned my null_item...
我没有返回 convertView,而是返回了我的 null_item ...
return inflater.inflate(R.layout.null_item, null);
null_item.xml:
null_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
回答by taotao
if you want to hide the item like this:
如果你想像这样隐藏项目:
convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,1));
convertView.setVisibility(View.GONE);
can't be AbsListView.LayoutParams(-1,0);
不能是 AbsListView.LayoutParams(-1,0);
if convertview are reused you should add this below to set it height back:
如果重新使用 convertview,您应该在下面添加它以将其高度设置回来:
if(convertView.getVisibility() == View.GONE) {
convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
convertView.setVisibility(View.VISIBLE);
}
回答by Win Myo Htet
When it comes to ListView, to make it efficient, we use ViewHolder pattern. The way to use ViewHolder Pattern and R.layout.row_null of the following xml
说到 ListView,为了提高效率,我们使用 ViewHolder 模式。下面xml的ViewHolder Pattern和R.layout.row_null的使用方式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
is to use with getViewTypeCount()and getItemViewType(int position)as follow.
与getViewTypeCount()和getItemViewType(int position) 一起使用,如下所示。
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return (hideStatusCheck(position)) ? 1 : 0;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
View rowView = convertView;
if (hideStatusCheck(pos)) {
if (rowView == null || rowView.getTag() != null) {
LayoutInflater inflater = mActivity.getLayoutInflater();
rowView = inflater.inflate(R.layout.row_null, parent, false);
}
} else {
if (rowView == null) {
rowView = inflateNormalView(parent);
} else if (rowView.getTag() == null) {
rowView = inflateNormalView(parent);
} else {
ViewHolder holderToCheck = (ViewHolder) rowView.getTag();
Integer storedPos = (Integer) holderToCheck.getTag(POSITION);
if (storedPos == null || storedPos != pos)
rowView = inflateNormalView(parent);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.setTag(POSITION,pos);
/*
Populate data
*/
return rowView;
}
private View inflateNormalView(ViewGroup parent) {
View rowView;
LayoutInflater inflater = mActivity.getLayoutInflater();
rowView = inflater.inflate(R.layout.normal_item, parent, false);
ViewHolder viewHolder = new ViewHolder();
assert rowView != null;
/* Initiate normal findViewById thing*/
rowView.setTag(viewHolder);
return rowView;
}
We do the checking of the item's View type and if it meets the hide check, it will return 1, otherwise 0. The ListView knows that there will be 2 types of View from getViewTypeCount. Now, the getView will return the approriate View depending on the hideStatusCheck. To make a robust ListView, we want to use the ViewHolder pattern. We don't need to use ViewHolder when it is hidden. We simply inflate the R.layout.row_null and return it. We will use the ViewHolder for the R.layout.normal_item. Here is the tricky part assuming the hiding check is not static. The first check of rowView==null
is standard. The second check of rowView.getTag()==null
is to see if the View is coming back to normal
from hiding. The third check in the last else
clause is to check if the ViewHolder retained in the tag is the right ViewHolder. If these conditions are met, we always inflate the view again. Yes, it is true that, the ViewHolder pattern is not used throughout but it uses to certain extends. It is better than nothing.
我们对item的View类型进行检查,如果满足hide检查,则返回1,否则返回0。ListView从getViewTypeCount中知道会有2种View。现在,getView 将根据 hideStatusCheck 返回适当的 View。为了制作一个健壮的 ListView,我们想使用 ViewHolder 模式。当它被隐藏时,我们不需要使用 ViewHolder。我们只是将 R.layout.row_null 膨胀并返回它。我们将为 R.layout.normal_item 使用 ViewHolder。这是假设隐藏检查不是静态的棘手部分。第一次检查rowView==null
是标准的。第二个检查rowView.getTag()==null
是查看视图是否从隐藏状态恢复正常。第三次检查在最后else
子句是检查标签中保留的ViewHolder是否是正确的ViewHolder。如果满足这些条件,我们总是会再次膨胀视图。是的,确实如此,ViewHolder 模式并未始终使用,但它用于某些扩展。总比没有好。
回答by jqpubliq
I did some tinkering with a drag and drop list from here. When an item is popped out of the list to be moved around the cell space it occupied has it's height set to 1px (see line 238) so it appears "gone". I couldn't find a way to handle this better as setting height to 0 fails as does visibility GONE.
我从这里修改了一个拖放列表。当一个项目从列表中弹出并在它占据的单元格空间周围移动时,它的高度设置为 1px(参见第 238 行),因此它看起来“消失了”。我找不到更好的方法来处理这个问题,因为将高度设置为 0 失败,可见性 GONE 也是如此。
That said, If you really want to get rid of a row less temporarily, it might be a good idea to change the backing of the Adapter
and call notifyDataSetChanged()
on it.
也就是说,如果你真的想暂时减少一行,改变 的支持Adapter
并调用它可能是一个好主意notifyDataSetChanged()
。
回答by A-IV
I have look at source code. And there is only one way to hide item without notifyDataSetChanged()
. You must set visibility GONE
for all inner views and remove background image and paddings for item's view.
我看过源代码。并且只有一种方法可以在没有notifyDataSetChanged()
. 您必须GONE
为所有内部视图设置可见性并删除项目视图的背景图像和填充。
Note: Row with such invisible element will be selectable.
注意:具有此类不可见元素的行将是可选的。
P.S: This is very usefull for ExpandableListView
if you want to hide group view it self.
PS:ExpandableListView
如果您想隐藏组视图,这非常有用。
回答by Rakesh Jha
To Hide whole raw from listview in android:-
在android中从listview隐藏整个raw:-
RelativeLayout parentLayout = (RelativeLayout) view.findViewById(R.id.relative);
if (productPojoList.get(position).getSERSERVICETYPE().toString().equals("Group|Promotional")){
view.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,1));
view.setVisibility(View.GONE);
} else {
if(view.getVisibility() == View.GONE) {
view.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
view.setVisibility(View.VISIBLE);
}
}
回答by X-QlusioN
add to your ListView object: android:dividerHeight="0px" android:divider="#FFFFFF"
添加到您的 ListView 对象: android:dividerHeight="0px" android:divider="#FFFFFF"
Divider color doesn't matter only setting dividerHeight doesn't work
分隔线颜色无关紧要仅设置dividerHeight 不起作用
This does remove the divider though...
虽然这确实删除了分隔符......
回答by Arnaud SmartFun
I think I have a much easier / safer solution: you just have to "embed" your item in a Layout, and change the visibility of this parent layout.
我想我有一个更简单/更安全的解决方案:您只需将您的项目“嵌入”在布局中,并更改此父布局的可见性。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- Embed ListView Item into a "parent" Layout -->
<LinearLayout
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- This is the normal content of your ListView Item -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="World" />
</LinearLayout>
</LinearLayout>
Then in your code just do:
然后在您的代码中执行以下操作:
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater li = mActivity.getLayoutInflater();
view = li.inflate(R.layout.my_listview_item, null);
}
LinearLayout parentLayout = (LinearLayout) view.findViewById(R.id.parentLayout);
if (shouldDisplayItem(position)) {
parentLayout.setVisibility(View.VISIBLE);
} else {
parentLayout.setVisibility(View.GONE);
}
return view;
}
This way you always use/reuse the same item, and just hide/show it.
通过这种方式,您始终可以使用/重用相同的项目,而只需隐藏/显示它。