我正在尝试在 AlertDialog 中使用回收器视图。
我希望回收器视图占据 AlertDialog 宽度的 100%。
我使用这个xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="@+id/TV_DownloadablesDialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/file_downloads"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/RV_DownloadablesDialogRecycler"
android:layout_width="0dp"
android:layout_height="250dp"
android:layout_marginTop="12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/TV_DownloadablesDialogTitle"
tools:listitem="@layout/downloadables_dialog_item_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
但在运行时它实际上看起来像这样。
请注意
TextView
如何响应约束布局,但 RecyclerView
则不然。
这是我显示 AlertDialog 的方式。
private void showDownloadablesDialog(LinkedList<Downloadable> downloadableList) {
View downloadablesDialogView = getLayoutInflater().inflate(R.layout.downloadables_dialog, null, false);
RecyclerView downloadablesRecycler = downloadablesDialogView.findViewById(R.id.RV_DownloadablesDialogRecycler);
DownloadablesDialogAdapter adapter = new DownloadablesDialogAdapter(this, downloadableList);
downloadablesRecycler.setAdapter(adapter);
downloadablesRecycler.setLayoutManager(new LinearLayoutManager(this));
new AlertDialog.Builder(this)
.setView(downloadablesDialogView)
.setPositiveButton(R.string.ok, (dialogView, which) -> {
})
.create()
.show();
}
前面的代码在 Activity 类中执行。
我尝试将
android:layout_width
更改为 match_parent
但得到了相同的结果。
但是当我将其设置为固定宽度时,它可以工作,但我不想使用固定宽度,因为它不会使应用程序在不同的屏幕尺寸上做出响应。
编辑: 我注意到,当我单击“删除”按钮时,它会以某种方式自行修复。
这是回收器适配器中
onBindViewHolder()
内删除按钮的逻辑
holder.BFileRemove.setOnClickListener((button) -> {
Intent intent = new Intent(context, NetworkService.class);
intent.setAction(NetworkService.ACTION_MODIFY_DOWNLOADABLE);
intent.putExtra(NetworkService.EXTRA_MODIFY_TYPE, NetworkService.VALUE_MODIFY_DELETE);
intent.putEffmpeg -ss 30 -t 3 -i input.mp4 \
-vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" \
-loop 0 output.gifxtra(NetworkService.EXTRA_MODIFY_DELETE_UUID, downloadables.get(holder.downloadableIndex).uuid);
downloadables.remove(holder.downloadableIndex);
notifyItemRemoved(holder.downloadableIndex);
context.startService(intent);
});
看来问题可能出在初始布局计算上。即使 RecyclerView 的宽度设置为 0dp 并带有约束,允许其在 AlertDialog 内扩展,布局过程可能无法正确考虑这些约束。
使用下面的代码自动调整回收器视图的宽度:
private void showDownloadablesDialog(LinkedList<Downloadable> downloadableList) {
View downloadablesDialogView = getLayoutInflater().inflate(R.layout.downloadables_dialog, null, false);
RecyclerView downloadablesRecycler = downloadablesDialogView.findViewById(R.id.RV_DownloadablesDialogRecycler);
DownloadablesDialogAdapter adapter = new DownloadablesDialogAdapter(this, downloadableList);
downloadablesRecycler.setAdapter(adapter);
downloadablesRecycler.setLayoutManager(new LinearLayoutManager(this));
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setView(downloadablesDialogView)
.setPositiveButton(R.string.ok, (dialogView, which) -> {})
.create();
alertDialog.setOnShowListener(dialogInterface -> {
Window window = alertDialog.getWindow();
if (window != null) {
View decorView = window.getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
ViewGroup.LayoutParams layoutParams = downloadablesRecycler.getLayoutParams();
layoutParams.width = decorView.getWidth();
downloadablesRecycler.setLayoutParams(layoutParams);
}
});
}
});
alertDialog.show();
}