java - 如何在 Android 上删除 RecyclerView 的 header

标签 java android android-recyclerview

基本上我想在一个RecyclerView中添加一个TextView作为header部分,但同时我也想在字符串为null的情况下删除这个header。在这种情况下,仅显示常规 ViewItems。

这是我的代码:

public class HeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    String[] data;

    public HeaderAdapter(String[] data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            //inflate your layout and pass it to view holder
            return new VHItem(null);
        } else if (viewType == TYPE_HEADER) {
            //inflate your layout and pass it to view holder
            return new VHHeader(null);
        }

        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VHItem) {
            String dataItem = getItem(position);

            //cast holder to VHItem and set data
        } else if (holder instanceof VHHeader) {
            //cast holder to VHHeader and set data for header.
        }
    }

    @Override
    public int getItemCount() {
        return data.length + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position))
            return TYPE_HEADER;

        return TYPE_ITEM;
    }

    private boolean isPositionHeader(int position) {
        return position == 0;
    }

    private String getItem(int position) {
        return data[position - 1];
    }

    class VHItem extends RecyclerView.ViewHolder {
        TextView title;

        public VHItem(View itemView) {
            super(itemView);
        }
    }

    class VHHeader extends RecyclerView.ViewHolder {
        Button button;

        public VHHeader(View itemView) {
            super(itemView);
        }
    }
}

最佳答案

粗略而简单的方法是使用 yourInflatedLayout.setVisibility(View.GONE) 隐藏第一个项目,或者使用 yourInflatedLayout.setLayoutParams(new ViewGroup.LayoutParams) 将其设置为零尺寸(0,0)) 在检查所需条件(在您的情况下为空字符串)之后——尽管这样做您的 View 仍然会膨胀,但没有用。

检查只是一个像这样的简单条件(来自您的代码 fragment - 假设您还添加了自己的方式来扩充布局,因为在您的代码中您只是将 null 传递给 ViewHolder 的构造函数):

    } else if (viewType == TYPE_HEADER) {
        //inflate your layout and pass it to view holder
        yourInflatedLayout = LayoutInflater
                    .from(yourContext)
                    .inflate(R.layout.your_layout, parent, false);

        // here you perform the check
        if (yourCheckedString == null) {
            yourInflatedLayout.setVisibility(View.GONE);
        }

        return new VHHeader(yourInflatedLayout);
    }
}

更好的方法(根据我的观点)是在适配器的逻辑中处理问题。像这样的东西(只是为了说明):

// you'll have to introduce a way to find out whether the desired string is null – a method is just one way to do this
private boolean isStringNull() {
    return yourCheckedString == null;
}

@Override
public int getItemCount() {
    // if the string is null and, we won't display the "header" item and we thus don't want to increment the item count by 1
    return data.length + (isStringNull() ? 0 : 1);
}

@Override
public int getItemViewType(int position) {
    // we want to display the header only if the string is not null, so we need to incorporate this requirement into the condition
    if (isPositionHeader(position) && !isStringNull())
        return TYPE_HEADER;

    return TYPE_ITEM;
}

private boolean isPositionHeader(int position) {
    return position == 0;
}

private String getItem(int position) {
    // we've already specified that the item count in the adapter won't be incremented if the string is null, so we have to reflect the change also here to avoid going out of the bounds of our data array
    return data[position - (isStringNull() ? 0 : 1)];
}

关于java - 如何在 Android 上删除 RecyclerView 的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34247505/

相关文章:

java - Android,将已经开始的 Activity 带回到前面

android - 更改 Android 短信的文本

java - 延迟回收器查看每个项目的点击功能

java - Java 构建路径中的 Windows 环境变量

java - Google Maps Android API 如何检查是否达到配额限制?

java - 退休的mvn eclipse :eclipse in m2e?相当于什么

java - 如何在android中实现java中的Kotlin类?

android - 在 RecyclerView Adapter 中使用带有 Activity 作为参数的方法

android - RecyclerView 在 SoftKeyboard 可见时隐藏 Toolbar 和布局

java - 是否推荐使用 StringUtils.EMPTY?