У меня есть домашний фрагмент, который состоит из: заголовка, кнопки, списка. Когда я нажимаю на список, я показываю второй фрагмент, в котором есть все детали выбранного элемента. Заголовок и список фрагмента дома исчезают, но кнопка остается там. Я обнаружил здесь , что я мог бы установить для 2-го фрагмента clickable = true, чтобы избежать триггеров кликов исходного фрагмента, но это не сработало, и я считаю такой способ работы ненадежным.
Я не знаю, в чем заключается хитрость, но вот мой home_fragment.xml
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment"
android:id="@+id/fragment_container"
>
<ListView
android:id="@+id/list_apero"
... />
<TextView
android:id="@+id/text_home"
... />
<Button
android:id="@+id/btn_add_apero"
app:layout_constraintTop_toBottomOf="@+id/text_home" />
</androidx.constraintlayout.widget.ConstraintLayout>
И затем с моим java кодом я отображаю новый фрагмент:
public View onCreateView(@NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
//------------------------------------- Add a event ---------------------------------
Button btn_add_apero = (Button) root.findViewById(R.id.btn_add_apero);
btn_add_apero.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final View inflater = getLayoutInflater().inflate(R.layout.layout_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(inflater);
builder.setTitle("Super un nouvel apéro !");
final EditText name_apero = (EditText)inflater.findViewById(R.id.edit_apero);
final EditText date_apero = (EditText)inflater.findViewById(R.id.edit_date);
// Add action buttons
builder.setPositiveButton(R.string.text_add, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
LaperoDatabase db = Room.databaseBuilder(root.getContext(),
LaperoDatabase.class, "lapero_db").allowMainThreadQueries().build();
AperoDao dbApero = db.getAperoDao();
Apero new_apero = new Apero(name_apero.getText().toString(), date_apero.getText().toString());
dbApero.insert(new_apero);
AperoIncomingAdapter a = (AperoIncomingAdapter) ((ListView) root.findViewById(R.id.list_apero)).getAdapter();
a.addApero(new_apero);
}
});
builder.setNegativeButton(R.string.text_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.show();
}
});
//------------------------------------- the incoming list -----------------
ArrayList<Apero> incomingApero = new ArrayList<Apero>();
initList(incomingApero, getContext());
AperoIncomingAdapter adapterIncomingApero = new AperoIncomingAdapter(this.getContext(), R.layout.apero_coming_cell_layout, incomingApero);
final ListView listIncomingApero = (ListView) root.findViewById(R.id.list_apero);
listIncomingApero.setAdapter(adapterIncomingApero);
listIncomingApero.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
Apero selectedItem = (Apero) adapter.getItemAtPosition(position);
Fragment newFragment = new AperoDetailFragment(selectedItem);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return root;
}
Проблема должна быть там, я думаю, потому что я не вижу другого способа, которым кнопка была бы в AperoDetailFragment