问题描述
我有这样的代码
public static class MyViewHolder extends RecyclerView.ViewHolder {
@InjectView(R.id.text)
TextView label;
public MyViewHolder(View itemView) {
super(itemView);
ButterKnife.inject(this, itemView);
}
public void hide(boolean hide) {
label.setVisibility(hide ? View.GONE : View.VISIBLE);
}
}
映射到RecyclerView
中的单行.实际上,R.id.text
是布局的根视图,该布局被夸大并传递到此处的构造函数中.
which maps to a single row in a RecyclerView
. R.id.text
is in fact the root view of the layout that gets inflated and passed in to the constructor here.
我正在使用LinearLayoutManager
的默认实现.
I'm using the default implementation of LinearLayoutManager
.
在bindViewHolder
中,我在MyViewHolder
的实例上调用hide(true)
,但是没有像预期的那样折叠该行,而是使该行变得不可见,并保持其在RecyclerView
中的高度和位置.还有其他人遇到这个问题吗?
In bindViewHolder
, I call hide(true)
on an instance of MyViewHolder
, but instead of collapsing the row as expected, the row becomes invisible, maintaining its height and position in the RecyclerView
. Has anyone else run into this issue?
如何在RecyclerView中隐藏项目?
How do you hide items in a RecyclerView?
推荐答案
在RV中没有内置的隐藏儿童的方法,但是,如果其高度变为0,则它是不可见的:).我假设您的根目录布局确实有一些最小高度(或确切的高度),即使它已经消失了,也仍然需要占用空间.
There is no built in way to hide a child in RV but of course if its height becomes 0, it won't be visible :). I assume your root layout does have some min height (or exact height) that makes it still take space even though it is GONE.
此外,如果要删除视图,请从适配器中删除它,不要隐藏它.您是否有理由要隐藏而不是删除?
Also, if you want to remove a view, remove it from the adapter, don't hide it. Is there a reason why you want to hide instead of remove ?
这篇关于在RecyclerView中隐藏视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!