对话框片段和 RecyclerView
Dialog Fragment and RecycleView
我正在尝试在 EditText Click 上制作 RecycleView DialogFragment,但我遇到了一些问题:我无法实施 Recycler。
我有这样的东西:
DialogFragment class:
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 class 存在一些问题,但不知道具体是什么。
提前致谢!
在这种情况下,您应该将所有代码放在 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
设置任何数据(患者),请参阅我上面的代码以获取更多信息。
我正在尝试在 EditText Click 上制作 RecycleView DialogFragment,但我遇到了一些问题:我无法实施 Recycler。 我有这样的东西:
DialogFragment class:
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 class 存在一些问题,但不知道具体是什么。 提前致谢!
在这种情况下,您应该将所有代码放在 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
设置任何数据(患者),请参阅我上面的代码以获取更多信息。