对话框片段和RecycleView

时间:2019-12-15 13:41:47

标签: 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>


和主要片段editText侦听器:

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

我敢打赌DialogFragment类存在一些问题,但不知道到底是什么。 预先感谢!

1 个答案:

答案 0 :(得分:0)

在这种情况下,您应该像这样将所有代码放入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设置任何数据(患者),请参阅上面的代码以获取更多信息。