在RecyclerView中隐藏视图 [英] Hiding views in RecyclerView

查看:89
本文介绍了在RecyclerView中隐藏视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码

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中隐藏视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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