![](/img/trans.png)
[英]java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
[英]java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
我有一個 RecyclerView,里面有一個 LinearLayout。 LinearLayout 由 9 個按鈕組成。 在進行一些更改之前,我可以單擊按鈕並且它沒有崩潰。 但是在我在下面添加此代碼后,它給出了錯誤並且沒有顯示它的來源:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能轉換為 android.support.v7.widget.RecyclerView$LayoutParams
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;
在 onClickListener 中添加了這個:
tempTag = (int) v.getTag();
mAdapter.grayButton(tempTag / 9, tempTag % 9);
zahl1 = Integer.parseInt(mAdapter.getText(tempTag).toString());
在 RecyclerView.Adapter 中添加了這個方法:
public void grayButton(int row, int element){
recycleViewDatas.get(row).setGray(element);
notifyItemChanged(row);
}
在 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);
這是我的 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));
}
}
}
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" />
...更多按鈕
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>
我不明白為什么更新后會出現錯誤。 而且該應用程序只會在我的 M9 上崩潰。 在 HTC One V 上,它不會崩潰並且一切都按預期工作。 你知道問題嗎? 你需要更多的代碼嗎?
如果您需要 kotlin 版本:
with(view.layoutParams){
width = ViewGroup.LayoutParams.WRAP_CONTENT
height = ViewGroup.LayoutParams.MATCH_PARENT
}
你的問題是在onBindViewHolder
:
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams
是為(幾乎)所有布局實現的。 當您在視圖上設置LayoutParams
時,您需要使用視圖的父級類型來設置它。 因此,如果您有一個LinearLayout
並且您正在嘗試添加一個 Button,則需要在Button
上設置LinearLayout.LayoutParams
。 這種情況下的問題是您不知道父類型。 我的意思是它可以在不同的 Android 版本上有所不同。
安全的賭注是使用公共類( ViewGroup
?)中的LayoutParams
,因為您只設置了寬度和高度。
我有同樣的問題,你可以使用這個
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.