Android 适配器行隐藏
在 arrayAdptor 中,我们使用以下代码:
final LayoutInflater inflater = activity.getLayoutInflater();
row = (LinearLayoutCustom) inflater.inflate(R.layout.row, null);
final TextView label = (TextView) row.findViewById(R.id.title);
label.setText(position + "" + items[position]);
return row;
现在假设某些值为 null (例如在位置 2 , items[2] = null ),所以我不想在行中显示它。我想隐藏它。如果我使用
row.setVisibility(View.GONE)
它,则会在这一行留下我不想要的空白。那我该怎么办呢?
In arrayAdptor we use following code:
final LayoutInflater inflater = activity.getLayoutInflater();
row = (LinearLayoutCustom) inflater.inflate(R.layout.row, null);
final TextView label = (TextView) row.findViewById(R.id.title);
label.setText(position + "" + items[position]);
return row;
Now suppose some value are null (for example at position 2 , items[2] = null ) so i dont want to show it in row. i want to hide it. if i use
row.setVisibility(View.GONE)
it leaves a blank space at this row which i dont want. so what should i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
AFAIK,您不能从
getView
返回空视图,但您可以使视图不可见且高度为 1。尽管使用getCount
进行操作可能是首选方式。AFAIK you can't return a null view from
getView
, but you could just make the view invisible and height 1. Although manipulating using thegetCount
is probably the preferred way.您需要让适配器使用
getCount
返回非空项的总数,然后保留位置到内部数据结构的映射。例如。
您有一个列表
,当调用 getCount 时,它返回 3。
然后,当在位置 1 上调用
getView
时,您将返回list[1]
处的项目。位置 2 上的
getView
返回list[3]
(因为它是第二个非空),等等。
这是我发现的唯一方法。
You'll need to have the adapter return the total number of non-null items with
getCount
and then keep a mapping of position to your internal data structure.For example.
You have a list
When getCount is called it returns 3.
Then when
getView
is called on position 1 you return the item atlist[1]
.getView
on position 2 returnslist[3]
(as it's the 2nd non-null),and so forth.
This is the only way I've found to do this.
您可以使用没有“隐藏”项目高度的视图,这样您就不必执行所有模型管理和映射。例如,假设您有一个“过滤器”EditText 字段,当输入数据时,它仅保留匹配的项目:
You can use a View that has no height for the "hidden" items so that you don't have to do all the model housekeeping and mapping. For example, suppose you had a "filter" EditText field that when data is entered it only keeps matching items:
在这里,您需要在 getCount()< 中编写逻辑/a>,getItemId() 和 <一个href="http://developer.android.com/reference/android/widget/Adapter.html#getItem%28int%29" rel="nofollow">getItem(),
它将创建 getCount 返回的行数
并
编辑:所以在您的 getview 中
Here you need to write the logic in your getCount(),getItemId() and getItem(),
It will create the no of rows what the getCount return
And
Edit:So in your getview
这是我实现的解决方案,这里有一个代码示例供每个正在寻找它的人使用:
希望这会有所帮助!
This is the solution I implemented, here is a code example for everyone that is looking it:
hope this helps!