2017-05-04 139 views
0

我有一個recyclerview,我想在其中顯示多個視圖,我已經爲recyclerview和adpter實現了代碼,但是我不知道如何繼續下一步.PLS幫助如何在Android的回收視圖中添加多個視圖

代碼活動: - 爲適配器

private RecyclerView.LayoutManager layoutManager; 
private RecyclerView mList; 
private Context mContext; 
private CMyAdapter adapter; 
private String[] list; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mContext = MainActivity.this; 
    init(); 
} 

private void init() { 
    mList = (RecyclerView) findViewById(R.id.dealList); 
    layoutManager = new LinearLayoutManager(mContext); 
    mList.setLayoutManager(layoutManager); 
    mList.setHasFixedSize(true); 
    // set divider between items of list 
    mList.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL)); 
    list = getResources().getStringArray(R.array.list); 
    adapter = new CMyAdapter(mContext, list); 
    mList.setAdapter(adapter); 

} 

代碼: -

private Context mContext; 
private String[] list; 

public CMyAdapter(Context context, String[] list) { 
    this.mContext = context; 
    this.list = list; 
} 

@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view1, parent, false); 

    return new ViewHolder0(itemView); 
} 

@Override 
public int getItemViewType(int position) { 
    // Just as an example, return 0 or 2 depending on position 
    // Note that unlike in ListView adapters, types don't have to be contiguous 
    return position % 2 * 2; 
} 

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 

} 

@Override 
public int getItemCount() { 
    return list.length; 
} 

public class ViewHolder0 extends RecyclerView.ViewHolder { 
    public ViewHolder0(View itemView) { 
     super(itemView); 
    } 
} 

class ViewHolder2 extends RecyclerView.ViewHolder { 

    public ViewHolder2(View itemView) { 
     super(itemView); 

    } 
} 

}

回答

0

聲明不同ViewHolders如下圖所示

public class ItemViewHolder extends RecyclerView.ViewHolder { 
    public TextView mTitle; 
    private LinearLayout mItemView; 

    public ItemViewHolder(View itemView) { 
     super(itemView); 
     mTitle = (TextView) itemView.findViewById(R.id.webview_text); 
     mItemView = (LinearLayout) itemView.findViewById(R.id.header_container); 


    } 
} 

public static class ViewHolderHeader extends RecyclerView.ViewHolder { 
    public View header; 
    public TextView headerTitle; 

    public ViewHolderHeader(View itemView) { 
     super(itemView); 
     header = (View) itemView.findViewById(R.id.list_header_container); 
     headerTitle = (TextView) itemView.findViewById(R.id.webview_text); 
    } 
} 

public static class FooterViewHolder extends RecyclerView.ViewHolder { 
    public TextView mTitle; 
    public LinearLayout mItemView; 

    public FooterViewHolder(View itemView) { 
     super(itemView); 
     mTitle = (TextView) itemView.findViewById(R.id.webview_text); 
     mItemView = (LinearLayout) itemView.findViewById(R.id.header_container); 
    } 
} 

並使用它們就像下面使用的那樣

@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    if (viewType == TYPE_HEADER) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.dt_list_header_layout, parent, false); 
     return new ViewHolderHeader(v); 
    } else if (viewType == TYPE_ITEM) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_search_list, parent, false); 
     return new ItemViewHolder(v); 
    } else if (viewType == TYPE_FOOTER) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_item_dt, parent, false); 
     return new FooterViewHolder(v); 
    } 
    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 ItemViewHolder) { 
     X.Result result = getItem(position); 
     ((ItemViewHolder) holder).mTitle.setText(result.getName()); 
     ((ItemViewHolder) holder).mTitle.setTypeface(Util.getTondoRegularTypeFace(mContext)); 
     ((ItemViewHolder) holder).mItemView.setOnClickListener(this); 
     ((ItemViewHolder) holder).mItemView.setTag(position); 
    } else if (holder instanceof ViewHolderHeader) { 
     ((ViewHolderHeader) holder).headerTitle.setText(mTitle); 
     ((ViewHolderHeader) holder).headerTitle.setTypeface(Util.getTondoBoldTypeFace(mContext)); 
     ((ViewHolderHeader) holder).headerTitle.setTextSize(18); 
    } else if (holder instanceof FooterViewHolder) { 
     ((FooterViewHolder) holder).mTitle.setText("Issue not listed above"); 
     ((FooterViewHolder) holder).mTitle.setTypeface(Util.getTondoRegularTypeFace(mContext)); 
     ((FooterViewHolder) holder).mItemView.setTag(position); 
    } 

} 
+0

和在getItemViewType() – Sandeep

+0

並在getItemCount() – Sandeep

+0

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

相關問題