我有一个适用于RecyclerView的以下适配器:
public class GreenAdapter extends RecyclerView.Adapter<GreenAdapter.NumberViewHolder> {
private final String TAG = GreenAdapter.class.getSimpleName();
private int mNumberItems;
public GreenAdapter(int numberOfItems) {
mNumberItems = numberOfItems;
}
@Override
public NumberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.number_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
NumberViewHolder viewHolder = new NumberViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(NumberViewHolder holder, int position) {
Log.d(TAG, "#" + position);
holder.bind(position);
}
@Override
public int getItemCount() {
return mNumberItems;
}
class NumberViewHolder extends RecyclerView.ViewHolder {
TextView listItemNumberView;
public NumberViewHolder(View itemView) {
super(itemView);
listItemNumberView = (TextView) itemView.findViewById(R.id.TV1);
}
void bind(int listIndex) {
String[] messages = getResources().getStringArray(R.array.messageArray);
listItemNumberView.setText(messages[listIndex]);
}
}
}
我想要做的是让它的第一项更大。我的方法类似于下面的代码,但我想有更好的方法,对吧?
public class GreenAdapter extends RecyclerView.Adapter<GreenAdapter.NumberViewHolder> {
boolean isFirst=true;
private final String TAG = GreenAdapter.class.getSimpleName();
private int mNumberItems;
public GreenAdapter(int numberOfItems) {
mNumberItems = numberOfItems;
}
@Override
public NumberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.number_list_item;
if(isFirst) {
isFirst=false;
layoutIdForListItem = R.layout.first;
}
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
NumberViewHolder viewHolder = new NumberViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(NumberViewHolder holder, int position) {
Log.d(TAG, "#" + position);
holder.bind(position);
}
@Override
public int getItemCount() {
return mNumberItems;
}
class NumberViewHolder extends RecyclerView.ViewHolder {
TextView listItemNumberView;
public NumberViewHolder(View itemView) {
super(itemView);
listItemNumberView = (TextView) itemView.findViewById(R.id.TV1);
}
void bind(int listIndex) {
String[] messages = getResources().getStringArray(R.array.messageArray);
listItemNumberView.setText(messages[listIndex]);
}
}
}
答案 0 :(得分:0)
您应该覆盖getItemViewType
方法。
样品
public class GreenAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_LARGE = 1;
private static final int VIEW_TYPE_REGULAR = 2;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_LARGE) {
// supply large layout
}
if (viewType == VIEW_TYPE_REGULAR) {
// supply regular layout
}
return null;
}
@Override
public int getItemViewType(int position) {
return position == 0 ? VIEW_TYPE_LARGE : VIEW_TYPE_REGULAR;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
}