Reputation: 3887
I'm trying to follow the example here:
https://developer.android.com/training/material/lists-cards.html#RecyclerView
And there's a part which isn't very clear to me.
@Override
public BlogPostAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
In this part, they are inflating a layout with "my_text_view". I'm having trouble recreating this layout. If I create a single xml file "my_text_view.xml" with a just a TextView, it won't compile. If I surround my TextView with "LinearLayout" I will fail to cast it to TextView for the example to work. Create just a just a TextView with an ID and trying to inflate it will fail since that method takes a Layout as a parameter not a view.
How could I make this my_text_view layout for this example to work?
Upvotes: 0
Views: 263
Reputation: 1331
It should compile using just a TextView inside the layout file. Just make sure to add the xmlns:
<?xml version="1.0" encoding="utf-8"?>
<Textview xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
If you would want to wrap it inside a parent layout, when setting the viewHolder, you need to obtain the textview you're interested in from the view you inflate. You can obtain it by calling
view.findViewById(R.id.text_view);
Upvotes: 1