1
我想動態地誇大Cardview中的第二個XML,但它會拋出空指針異常。當我試圖做硬代碼(通過直接包含在卡片視圖佈局中) 。是否有其他方法可以動態擴充新的XML?如何在Cardview動態內充實新佈局
CardView佈局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/room_detail_cards"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/room_detail_Linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/module_type_5"/>
<!--<include layout="@layout/module_type_3"/>-->
<!--<It Works well when i hard code the layout here/>-->
</LinearLayout>
</android.support.v7.widget.CardView>
RecyclerAdapter.java(onCreateViewHolder)
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
layout= (LinearLayout) parent.findViewById(R.id.room_detail_Linear_layout);
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.module_type_3, layout, false);
layout.addView(itemView); //NULL Pointer Exception occurs here
View itemView1 = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.room_detail_card_view, null, false);
return new ViewHolder(itemView1);
}
第一佈局XML(module_type_5)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:hint="module"
android:id="@+id/module_5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/add"
android:id="@+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/add"
android:id="@+id/switch_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/add"
android:id="@+id/switch_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/add"
android:id="@+id/switch_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/add"
android:id="@+id/switch_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
第二XML佈局(module_type_3)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:hint="module"
android:id="@+id/module_5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/add"
android:id="@+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/add"
android:id="@+id/switch_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/add"
android:id="@+id/switch_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
首先,我嘗試了onBindViewHolder到舉行圖像,但它不適合我,這就是爲什麼我試圖膨脹wh cardView裏面的ole佈局。 –
'onBindViewHolder'將會工作。如果你不使用它,那麼'RecyclerView'將無法正常工作。 BTW'onCreateViewHolder'只被調用一次,所以數據不會被更新。 –
onBindViewHolder可以根據我的需要正常工作。 –