基本上我想在RecyclerView中添加一个TextView作为标题部分,但同时我也希望在字符串为null
的情况下删除此标题。在这种情况下,只显示常规的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);
}
}
}
答案 0 :(得分:0)
粗略而简单的方法就是用yourInflatedLayout.setVisibility(View.GONE)
隐藏第一个项目,或者在检查所需条件(在你的情况下是一个空字符串)后用yourInflatedLayout.setLayoutParams(new ViewGroup.LayoutParams(0,0))
将其设置为零维度 - 尽管这样做你的观点仍然会被夸大,徒劳无功。
检查只是一个简单的条件(来自您的代码片段 - 假设您还添加了自己的方式来扩充布局,因为在您的代码中您只需将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)];
}