2017-04-04 61 views
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]); 
     } 
    } 
} 
+0

確定這方式工作在第一,但如果我把它的滾動蟲子:( – newToEverything

回答

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; 
    } 
}