我的XML如下-live_test_question_change_dialog_layout.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/live_test_change_question_option_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a"
android:background="@drawable/option_number_background"
android:textStyle="bold"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
/>
</RelativeLayout>
ShowDialog方法如下-
/*---------------------------------------------------*/
public void showDialog(){
final Dialog dialog = new Dialog(getApplicationContext(), R.style.FullHeightDialog);
dialog.setContentView(R.layout.live_test_question_change_dialog_layout);
dialog.setCancelable(false);
if (dialog.getWindow() != null){
dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
RecyclerView recyclerView = dialog.findViewById(R.id.live_test_question_change_dialog_rec_view_id);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
ArrayList<String> questionNumberList = new ArrayList<>();
for(int i = 1; i<= testModel.getTotalQuestions(); i++){
questionNumberList.add(String.valueOf(i));
}
CustomAdapter customAdapter = new CustomAdapter(questionNumberList);
recyclerView.setAdapter(customAdapter);
dialog.show();
}
/*--------------------------------------------------*/
但当我试图显示给对话时应用程序崩溃了-
android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView
这个代码有什么问题? 如何解决这个问题?
我认为您正在使用AndroidX
依赖项
您应该使用
<androidx.recyclerview.widget.RecyclerView
而不是
<android.support.v7.widget.RecyclerView
您必须创建一个新的“片段”,它从DialogFragment扩展而来
public class MyDialogWithRecyclerView extends DialogFragment
官方文档很好,我使用它在DialogFragment中创建了一个Firestore回收器视图。 对于常规的回收器视图也是一样的。
https://developer.android.com/reference/android/app/dialogfragment
如果你已经与RV和碎片你应该是好的,但你可以问我任何问题。
编辑对不起,不推荐使用该版本,以下是正确的文档https://developer.android.com/reference/android/support/v4/app/dialogfragment.html