我有一個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);
}
}
}
和在getItemViewType() – Sandeep
並在getItemCount() – Sandeep
@Override public int getItemViewType(int position){if(isPositionHeader(position)){ return TYPE_HEADER; } return TYPE_ITEM; } – Sunny