【发布时间】: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
-
现在,我什至没有看到对话框
标签: android android-recyclerview dialog dialogfragment