0
我有以下的適配器,供RecyclerView:讓RecyclerView的某些項目使用不同的FrameLayouts?
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]);
}
}
}
確定這方式工作在第一,但如果我把它的滾動蟲子:( – newToEverything