Java 如何移除回收视图';Android上的s头

Java 如何移除回收视图';Android上的s头,java,android,android-recyclerview,Java,Android,Android Recyclerview,基本上,我想在RecyclerView中添加一个TextView作为标题部分,但同时我还想在字符串为null的情况下删除此标题。在这种情况下,仅显示常规视图项 这是我的密码: public class HeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private static final int TYPE_HEADER = 0; private static final in

基本上,我想在RecyclerView中添加一个TextView作为标题部分,但同时我还想在字符串为
null
的情况下删除此标题。在这种情况下,仅显示常规视图项

这是我的密码:

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);
        }
    }
}
公共类HeaderAdapter扩展了RecyclerView.Adapter{
私有静态最终int TYPE_头=0;
私有静态最终整数类型\ u项=1;
字符串[]数据;
公共HeaderAdapter(字符串[]数据){
这个数据=数据;
}
@凌驾
public RecyclerView.ViewHolder onCreateViewHolder(视图组父级,int-viewType){
if(viewType==类型\项){
//充气布局并将其传递给视图保持架
返回新项目(空);
}else if(viewType==类型\标题){
//充气布局并将其传递给视图保持架
返回新的VHHeader(空);
}
抛出新的RuntimeException(“没有与类型“+viewType+”+匹配的类型,请确保正确使用类型”);
}
@凌驾
BindViewHolder上的公共无效(RecyclerView.ViewHolder,int位置){
if(VHF项目的持有人实例){
字符串dataItem=getItem(位置);
//将支架铸造到项目并设置数据
}else if(VHHeader的保持架实例){
//将支架铸造到收割台,并设置收割台数据。
}
}
@凌驾
public int getItemCount(){
返回数据长度+1;
}
@凌驾
public int getItemViewType(int位置){
if(isPositionHeader(位置))
返回类型_头;
返回类型\ U项目;
}
专用布尔值isPositionHeader(int位置){
返回位置==0;
}
私有字符串getItem(int位置){
返回数据[位置-1];
}
VHItem类扩展了RecyclerView.ViewHolder{
文本视图标题;
公共项目(查看项目视图){
超级(项目视图);
}
}
类别VHHeader扩展了RecyclerView.ViewHolder{
按钮;
公共标题(查看项目视图){
超级(项目视图);
}
}
}

简陋而简单的方法是,在检查所需条件(本例中为空字符串)后,使用
yourInflatedLayout.setVisibility(View.GONE)
隐藏第一项,或者使用
yourInflatedLayout.setLayoutParams(new ViewGroup.LayoutParams(0,0))
将其设置为零维–尽管这样做,你的观点仍然会被夸大,徒劳

检查可能只是这样一个简单的条件(从您的代码片段-假设您也添加了自己的方式来膨胀布局,因为在您的代码中,您只需将null传递给ViewHolder的构造函数):


根据我的说法,更好的方法是处理适配器逻辑中的问题。类似这样的内容(仅用于说明):


我将改用此库:。您可以用另一个适配器包装您的适配器,以提供所需的功能。Like
WrapAdapter WrapAdapter=新的WrapAdapter(适配器)
then
wrapAdapter.addHeader(视图)和wrapAdapter.removeHeader(适配器)`
    } 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)];
}