最佳答案
当我们创建RecyclerView.Adapter
时,我们必须指定将与适配器绑定的ViewHolder
。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset;
public MyAdapter(String[] myDataset) {mDataset = myDataset;}
public static class ViewHolder extends RecyclerView.ViewHolder {public TextView mTextView;public ViewHolder(TextView v) {super(v);mTextView = v;}}
@Overridepublic MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.some_layout, parent, false);
//findViewById...
ViewHolder vh = new ViewHolder(v);return vh;}
@Overridepublic void onBindViewHolder(ViewHolder holder, int position) {holder.mTextView.setText(mDataset[position]);}
@Overridepublic int getItemCount() {return mDataset.length;}}
是否可以创建具有多种视图类型的RecyclerView
?