Error using the RecyclerView: The specified child already has a parent
when inflating you shouldn't attach the view to its parent. you wrote:
View v = inflater.inflate(R.layout.my_text_view, parent, true);
which should be :
View v = inflater.inflate(R.layout.my_text_view, parent, false);
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp">
<TextView
android:id="@+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/darker_gray"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:textSize="22dp" />
</RelativeLayout>
RelativeLayout this is parent of your TextView with id item_title
.
Then when RecyclerView is trying to add TextView that has parent it throw exception.
Try this code:
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
Log.d(TAG, "onCreateViewHolder");
RelativeLayout v = (RelativeLayout)LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
// set the view's size, margins, paddings and layout parameters
View view = v.findViewById(R.id.text1);
v.removeView(view);
ViewHolder vh = new ViewHolder(view);
return vh;
}