我有一个recyclerview,其中我想在其中显示多个视图,我已经为convertlerview和adpter实现了代码,但我不知道如何继续进行。请帮助
活动代码: -
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 :(得分:0)
您已经实施了正确的方法,但首先您必须知道如何在recyclerview中显示视图,例如按照以下链接
http://www.androidhive.info/2016/01/android-working-with-recycler-view/
答案 1 :(得分: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);
}
}