Android RelativeLayout不能强制转换为LayoutFlator中的TextView
我一直在遵循制作Android RelativeLayout不能强制转换为LayoutFlator中的TextView,android,android-layout,android-recyclerview,classcastexception,layout-inflater,Android,Android Layout,Android Recyclerview,Classcastexception,Layout Inflater,我一直在遵循制作回收视图的指南,但我遇到了一个障碍。它给出的代码不是很清楚,但在构建定制的Recyclerview.Adpater时,它有以下方法: @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view TextView
回收视图的指南,但我遇到了一个障碍。它给出的代码不是很清楚,但在构建定制的Recyclerview.Adpater
时,它有以下方法:
@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;
}
现在,我似乎不知道R.layout.my_text\u view
是从哪里来的。他们没有在布局文件中指定它。因此,当我试图将代码放入适配器时,它告诉我找不到符号。因此,我将其作为id添加到我的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>
但这不起作用,我得到一个例外:
java.lang.ClassCastException:android.widget.RelativeLayout不能强制转换为android.widget.TextView
我错过了什么?这个神秘的我的文本视图
是从哪里来的,我该如何使用它?你所犯的错误是你试图膨胀相对论视图中的文本视图。相反,首先对整个布局进行充气,并尝试找出布局中可用的子视图
例如,假设您的布局是item\u recycle\u layout.xml
,首先将其充气并将其放入视图
或直接放入RelativeLayout
,因为布局中的父布局与idactivity\u recycle
相对
例如,项目布局为:
<RelativeLayout
.....
..... >
<TextView
android:id="@+id/my_text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
基本上,当使用RecyclerView.Adapter的onCreateViewHolder时,它会将viewholder返回到列表项的视图。
ViewHolder用于在RecyclerView中为单个列表项(或行)充气
您应该为要添加到RecyclerView的列表项创建布局。
例如
R.layout.my_text_视图应仅包含此文本视图:
<TextView
android:id="@+id/my_text_view_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
现在,您可以使用与上面相同的其余代码。那么我的布局应该是什么样的呢?否决票,请说明否决票的原因!这更有意义。但是,现在我看到了错误“指定的子项已经有父项。您必须首先对子项的父项调用removeView()。”您是否创建了一个名为item\u recycle\u layout
的单独xml文件,或者您想给出的任何名称?如回答中所述,是的。它看起来很像你在答案里写的。只有TextView
在RelativeLayout
.R.layout.activity\u中的TextView不接受布局。它将采用布局项id.R.layout.my_text_视图类似于一个布局或卡片,它将在回收器视图上膨胀,与列表项相同检查此答案并遵循该教程
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
RelativeLayout v = (RelativeLayout) LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_recycle_layout, parent, false);
TextView txtView = (TextView) v.findViewById(R.id.my_text_view);
....
....
ViewHolder vh = new ViewHolder(v);
return vh;
}
<TextView
android:id="@+id/my_text_view_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>