RecyclerView 适配器 代码
时间: 2023-09-08 19:13:23 浏览: 90
可以在 onCreate() 方法中创建 RecyclerView 对象,然后通过 setAdapter() 方法设置 RecyclerView 的适配器,以下是一个简单的 RecyclerView 适配器代码示例:
```
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> mData;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.text);
}
}
public MyAdapter(List<String> data) {
mData = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mData.get(position));
}
@Override
public int getItemCount() {
return mData.size();
}
}
```
阅读全文