android - 对话框 fragment 和 RecyclerView

标签 android android-recyclerview dialog dialogfragment

我正在尝试在 EditText Click 上制作 RecycleView DialogFragment,但我遇到了一些问题:我无法实现 Recycler。 我有这样的东西:

enter image description here

DialogFragment 类:

public class PatientDialogFragment extends DialogFragment {


    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.dialog_fragment, null));
        builder.setTitle("Choose Patient");
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        return builder.create();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.dialog_fragment, container, false);

        final RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recyclerViewPatientsDialog);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setHasFixedSize(true);
        final PatientsAdapter adapter = new PatientsAdapter();
        recyclerView.setAdapter(adapter);


        return root;
    }
}



对话框 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewPatientsDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:listitem="@layout/person_item"
        android:layout_marginTop="50dp"
        />

</LinearLayout>


和主要 fragment editText 监听器:

        editTextPatient.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final PatientDialogFragment patientDialogFragment= new PatientDialogFragment();
                patientDialogFragment.show(getActivity().getSupportFragmentManager(), "myTag");
            }
        });

我敢打赌 DialogFragment 类存在一些问题,但不知 Prop 体是什么。 提前致谢!

最佳答案

在这种情况下,您应该将所有代码放在 onCreateDialog 方法中,如下所示。

public class PatientDialogFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Context context = requireActivity();
        LayoutInflater inflater = LayoutInflater.from(context);
        View dialogView = inflater.inflate(R.layout.dialog_fragment, null);

        RecyclerView recyclerView = dialogView.findViewById(R.id.recyclerViewPatientsDialog);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setHasFixedSize(true);
        PatientsAdapter adapter = new PatientsAdapter();

        // Create patient list here to pass into PatientsAdapter
        // This demo I just create some random patients, replace on your own version.
        List<Patient> patientList = new ArrayList<>();
        patientList.add(new Patient("Alice", "Liddel", System.currentTimeMillis(), "note"));
        patientList.add(new Patient("Bob", "Martin", System.currentTimeMillis(), "note2"));
        patientList.add(new Patient("Clark", "Ken", System.currentTimeMillis(), "note3"));
        adapter.setPatients(patientList);

        recyclerView.setAdapter(adapter);

        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setView(dialogView)
                .setTitle("Choose Patient")
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        return builder.create();
    }
}

更新:您的对话框仍然是空的,因为您还没有为您的 PatientsAdapter 设置任何数据(患者),请参阅我上面的代码以获取更多信息。

关于android - 对话框 fragment 和 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59344458/

相关文章:

java - 在 android 应用程序中使用我自己的 SQLiteDatabase

java - 通过android应用程序获取执行命令的输出

android - 在零位置插入 RecyclerView 项目 - 始终保持滚动到顶部

java - 从 Firebase 检索一些数据并将其放在 RecyclerView 上

android - 显示不褪色背景的对话框

c++ - 模态对话框在调用接受、完成或关闭后挂起

android - 如何在 Android 中创建自定义 PopupMenu

java - Android:反转 RecyclerView 位置

python - 使用 GTK 窗口并等待响应

android - 创建覆盖 ImageView 动画谷歌地图