【问题标题】:Dialog Fragment and RecycleViewDialog Fragment 和 RecyclerView
【发布时间】:2019-12-15 13:41:47
【问题描述】:

我正在尝试在 EditText Click 上创建一个 RecycleView DialogFragment,但我有一些问题:我无法实现 Recycler。 我有类似的东西:

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 类存在一些问题,但不知道究竟是什么。 提前致谢!

【问题讨论】:

  • 你为什么要夸大你的视图两次?在 onCreateDialog 中移除你的 inflate 并在 onCreateView 中移动代码。在 onCreateView 内部膨胀并返回您的视图,覆盖 onViewCreated 并在其中设置您的视图(如 onClickListeners、文本、适配器等)
  • 并将你的 recyclerView 设置为 match_parent
  • 嗯,我做了这样的东西,但还是不行:pastebin.com/raw/439K6XZw
  • 像这样:pastebin.com/E7Ga4Swj
  • 现在,我什至没有看到对话框

标签: android android-recyclerview dialog dialogfragment


【解决方案1】:

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

【讨论】:

  • 我有一个异常:java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。我应该在哪里调用 removeView()?
  • 好的,我已经在返回之前放了:((ViewGroup)recyclerView.getParent()).removeView(recyclerView);我没有异常,但对话框中也没有 RecyclerView。
  • @domvnski 你能在收到异常时发布堆栈跟踪吗?
  • 我不知道,但我现在无法得到这个异常。这是我所有的项目:ufile.io/gn25u76q你能看一下吗?
  • @domvnski 很高兴听到这个消息,顺便说一句,如果您的问题已经解决,那么您可以将至少一个答案标记为已接受。
猜你喜欢
  • 1970-01-01
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多