[英]java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
I have a RecyclerView with a LinearLayout inside.我有一个 RecyclerView,里面有一个 LinearLayout。 The LinearLayout consists out of 9 buttons.
LinearLayout 由 9 个按钮组成。 Before some changes I could click the buttons and it didn't crashed.
在进行一些更改之前,我可以单击按钮并且它没有崩溃。 But after I added this code below it gives the error and does not show where it comes from:
但是在我在下面添加此代码后,它给出了错误并且没有显示它的来源:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.v7.widget.RecyclerView$LayoutParams
The imports in Adapter: Adapter 中的导入:
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout.LayoutParams;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
Added this in the onClickListener:在 onClickListener 中添加了这个:
tempTag = (int) v.getTag();
mAdapter.grayButton(tempTag / 9, tempTag % 9);
zahl1 = Integer.parseInt(mAdapter.getText(tempTag).toString());
Added this method in the RecyclerView.Adapter:在 RecyclerView.Adapter 中添加了这个方法:
public void grayButton(int row, int element){
recycleViewDatas.get(row).setGray(element);
notifyItemChanged(row);
}
Added this code in the onBindViewHolder:在 onBindViewHolder 中添加了此代码:
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
viewHolder.ll.setLayoutParams(lp);
lp.height = viewHolder.ll.getLayoutParams().height - (int) (viewHolder.buttons.get(j).getLayoutParams().height - recycleViewDatas.get(i).getSize()) + 30;
viewHolder.ll.setLayoutParams(lp);
if(recycleViewDatas.get(i).getGray() == j)
viewHolder.buttons.get(j).setTextColor(Color.GRAY);
else if(recycleViewDatas.get(i).getGray() == -1)
viewHolder.buttons.get(j).setTextColor(Color.BLACK);
This is my ViewHolder:这是我的 ViewHolder:
public static class ViewHolder extends RecyclerView.ViewHolder {
LinearLayout ll;
private ArrayList<Button> buttons = new ArrayList<>();
public ViewHolder(View v, Context context) {
super(v);
ll = (LinearLayout) v.findViewById(R.id.llRecycleView);
for (int i = 0; i < ll.getChildCount(); i++) {
buttons.add((Button) ll.getChildAt(i));
}
}
}
The layout file of the recycleviewdata: recycleviewdata 的布局文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="9"
android:background="@drawable/zeile"
android:id="@+id/llRecycleView">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/black"
android:background="@android:color/transparent"
android:textSize="23sp"
android:clickable="false"
android:layout_gravity="center"
android:gravity="center" />
...more buttons ...更多按钮
The layout of the MainActivity: MainActivity的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:background="@drawable/top">
...some buttons
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/></LinearLayout>
I do not understand why the error comes after the update.我不明白为什么更新后会出现错误。 And the app only crashes on my M9.
而且该应用程序只会在我的 M9 上崩溃。 On the HTC One V it does not crash and all works as expected.
在 HTC One V 上,它不会崩溃并且一切都按预期工作。 You know the problem?
你知道问题吗? Do you need any more code?
你需要更多的代码吗?
if you need the kotlin version :如果您需要 kotlin 版本:
with(view.layoutParams){
width = ViewGroup.LayoutParams.WRAP_CONTENT
height = ViewGroup.LayoutParams.MATCH_PARENT
}
Your problem is in onBindViewHolder
:你的问题是在
onBindViewHolder
:
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams
is implemented for (almost) all the layouts. LayoutParams
是为(几乎)所有布局实现的。 When you set LayoutParams
on a view, you need to set it with a type of the view's parents.当您在视图上设置
LayoutParams
时,您需要使用视图的父级类型来设置它。 So if you have a LinearLayout
and you are trying to add a Button, you need to set LinearLayout.LayoutParams
on the Button
.因此,如果您有一个
LinearLayout
并且您正在尝试添加一个 Button,则需要在Button
上设置LinearLayout.LayoutParams
。 The problem in this case is that you don't know the parent type.这种情况下的问题是您不知道父类型。 And by that I mean it can different on different Android versions.
我的意思是它可以在不同的 Android 版本上有所不同。
The safe bet is to use the LayoutParams
from a common class ( ViewGroup
?) since you only set width and height.安全的赌注是使用公共类(
ViewGroup
?)中的LayoutParams
,因为您只设置了宽度和高度。
i had same problem, you can use this我有同样的问题,你可以使用这个
ViewGroup.LayoutParams layoutParams =v.itemView.getLayoutParams();
layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.height= 200;
v.itemView.setLayoutParams(layoutParams);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.