I have been following this guide on making a RecyclerView
, but I've run into a snag. The code it gives isn't very clear, but when building the custom Recyclerview.Adpater
, it has this method:
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
TextView v = (TextView) 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;
}
Now, I can't seem to figure out where the R.layout.my_text_view
came from. They didn't specify it in their layout file. So, when I tried to put that code into my Adapter, it told me that the symbol was not found. So, I added it as the id to my TextView
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_recycle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".recycle.RecycleActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".recycle.RecycleActivity"/>
<include
layout="@layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/my_text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
But I still can't use my_text_view
in R.layout.my_text_view
because it still can't find it. I also tried R.id.my_text_view
, which it can find, but it says that it needs a layout, not an id. so I tried using R.layout.activity_recycle
like this:
@Override
public RecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
// create a new view
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_recycle, parent, false);
// set the view's size, margins, paddings and layout parameters
return new ViewHolder(v);
}
But that didn't work, and I get the exception:
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
What am I missing? Where did this mysterious my_text_view
come from, and how do I use it?