我正在研究设计我的应用程序以使用 Android L 并设计它以使用新的 Material Design。
我正在查看https://developer.android.com/preview/material/ui-widgets.html上的 RecyclerView 示例,但该示例无法编译。
下面是我的活动课
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myDataset = new String[2];
myDataset[0] = "Hello";
myDataset[1] = "World";
setContentView(R.layout.activity_recylcer);
mRecyclerView = (RecyclerView)findViewById(R.id.recylcerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}
下面是我的 MyAdapter 类
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset;
// Provide a reference to the type of views that you are using
// (custom viewholder)
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position]);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}
我遇到的问题是,当我创建 ViewHolder 的实例时,该示例说要传入v
一个 View 但 Android Studio 说它需要一个 TextView。
我假设我做错了,因为我想认为 Google 的 Android 开发站点的示例会有正确的代码,但我一辈子都看不到我可能做错了什么。