[英]RecyclerView inside AlertDialog is not displaying items
我試圖在對話框內的 recyclerView 中顯示數據,數據是從 HTTP 請求中獲取的。
我驗證了適配器獲得了數據——我在適配器中打印了數組長度並且它很好,但由於某種原因它不會顯示它——我只能看到 recyclerView 應該所在的空白區域。
重要說明:該應用程序運行良好,沒有運行時錯誤或其他錯誤,只是 recyclerView 不存在。
final View dialogView = getLayoutInflater().inflate(R.layout.generate_question_dialog, null);
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setView(dialogView);
alertDialog.show();
Button generateQuestionBtn = alertDialog.findViewById(R.id.generateQuestionBtn);
EditText subjectEditText = alertDialog.findViewById(R.id.subjectEditText);
ProgressBar loading = alertDialog.findViewById(R.id.generateProgressBar);
Objects.requireNonNull(generateQuestionBtn).setOnClickListener(view2 -> {
if (!TextUtils.isEmpty(subjectEditText.getText().toString())){
loading.setVisibility(View.VISIBLE);
HttpUrl.Builder httpBuilder = HttpUrl.parse("ADDRESS").newBuilder();
OkHttpClient okhttpclient = new OkHttpClient();
okhttpclient.setReadTimeout(60, TimeUnit.SECONDS);
httpBuilder.addQueryParameter("subject",subjectEditText.getText().toString());
Request request = new Request.Builder().url(httpBuilder.build()).build();
okhttpclient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
System.out.println("FAILURE");
System.out.println(e.getMessage());
}
@Override
public void onResponse(Response response) throws IOException {
String[] questions = response.body().string().split(",");
loading.setVisibility(View.INVISIBLE);
QuestionsRecyclerViewAdapter adapter = new QuestionsRecyclerViewAdapter(getContext(),questions);
new Handler(Looper.getMainLooper()).post(new Runnable(){
@Override
public void run() {
System.out.println("HERE!");
RecyclerView questionsRecyclerView = alertDialog.findViewById(R.id.generatedQuestionsRecyclerView);
questionsRecyclerView.setAdapter(adapter);
questionsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
questionsRecyclerView.setVisibility(View.VISIBLE);
}
});
}
});
}
xmi文件:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="@string/enter_a_subject_and_we_ll_find_question_for_you"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/subjectEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="@string/enter_subject"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/generateQuestionBtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:text="@string/generate_question"
android:textColor="@color/white"
android:background="@drawable/button_bg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/subjectEditText" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:paddingStart="185dp"
android:layout_height="wrap_content"
android:id="@+id/generatedQuestionsRecyclerView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/generateQuestionBtn" />
<ProgressBar
android:id="@+id/generateProgressBar"
style="?android:attr/progressBarStyle"
android:layout_width="100dp"
android:layout_height="80dp"
android:visibility="invisible"
android:indeterminateTint="@color/blue_p"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/generatedQuestionsRecyclerView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/generateQuestionBtn" />
</androidx.constraintlayout.widget.ConstraintLayout>
我認為您的對話框內沒有足夠的空間來容納這些項目。 該對話框將以僅適合空 RecyclerView 的大小創建。 當您將項目添加到 RecyclerView 時,內容大小會發生變化,但對話框不會調整大小以適應新的內容大小。 您可以使用alertDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
增加對話框的大小。
如果這不能解決問題,則問題可能出在您的QuestionsRecyclerViewAdapter.class
或您顯示項目的方式上。
您在 RecyclerView 中添加了一個非常大的 paddingStart (185dp),這可能是您的視圖不可見的原因,請刪除此填充並查看是否有任何改變。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.