在回收站视图中添加多个标题?

时间:2015-02-25 21:37:49

标签: android android-recyclerview recycler-adapter

所以我已经使用this method在我的recyclerview中实现了两个标题,但是我这样做的方式我认为是正确的。对于我的两个标题,我已经膨胀了两个单独的布局,这实际上是不必要的,因为我可以使用我的recyclerview中的所有项目的布局,也膨胀布局是昂贵的。我将如何改变文本

这是我的适配器代码:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_HEADER2 = 11;
private static final int TYPE_ITEM = 1;
List<Information> data = Collections.emptyList();
LayoutInflater inflater;

public RecyclerViewAdapter(RecyclerViewTesting recyclerViewTesting, List<Information> data) {
    this.data = data;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_HEADER) {
        return new VHHeader(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_custom_row_header_one, parent, false));
    }
    if (viewType == TYPE_HEADER2) {
        return new VHHeader(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_custom_row_header_two, parent, false));
    }

    return new VHItem(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_custom_row, parent, false));
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof VHItem) {
        Information item = getItem(position);
        ((VHItem) holder).title.setText(item.title);
    } else if (holder instanceof VHHeader) {
    }
}

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

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

    if (isPositionHeader2(position)){
        return TYPE_HEADER2;
    }

    return TYPE_ITEM;
}

private boolean isPositionHeader2(int position) { return position ==  11;
}

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

private Information getItem(int position) {
    return data.get(position - 1);
}

class VHItem extends RecyclerView.ViewHolder {
    TextView title;
    ImageView icon;

    public VHItem(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.listText);
        icon = (ImageView) itemView.findViewById(R.id.listIcon);
    }
}

class VHHeader extends RecyclerView.ViewHolder {
    TextView title;
    ImageView icon;

    public VHHeader(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.listText);
    }
}
}

非常感谢任何帮助!

0 个答案:

没有答案