我正在尝试关注Creating Lists and Cards.
的Android官方文档我发现here有必要在TextView中使用ViewHolder,但是当我这样做时,我遇到以下错误:java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
我的适配器
public class CardsViewAdapter extends RecyclerView.Adapter<CardsViewAdapter.ViewHolder> {
private Game[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(TextView v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.textViewName);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public CardsViewAdapter(Game[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public CardsViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_resume_game, parent, false);
// set the view's size, margins, paddings and layout parameters
//...
ViewHolder vh = new ViewHolder((TextView) 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].getId_game());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}
我的自定义视图XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:tag="cards main container">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="@color/blue_50"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
答案 0 :(得分:2)
你有两个混在一起。假设在
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_resume_game, parent, false);
R.layout.cards_resume_game
为LinearLayout
,您应该:
ViewHolder vh = new ViewHolder(v);
让ViewHolder构造函数接受一个视图:
public ViewHolder(View v) {
mTextView = (TextView) v.findViewById(R.id.some_text_view_id);
}
其中some_text_view_id是所述布局中的TextView,将由findViewById()
获取答案 1 :(得分:0)
在您的适配器中,您将TextView转换为ViewHolder,这是不必要的,并且您还以冗余方式调用ViewHolder。试试这个:
return new ViewHolder(v)
代替ViewHolder vh = new ViewHolder((TextView)
您不需要此强制转换的原因是因为您已经在ViewHolder构造函数下投射mView
,或者您已经定义了它应该期望的视图。