I have the home fragment who is composed of : a title, a button, a list. When I click on the list I display a 2nd fragment where you have all the detail of the clicked element. The title and the list of the home fragment disappear but the button stay there. I found out here that I could set on the 2nd fragment clickable = true to avoid the click triggers of the home fragment but that didn't work and I find that way of working kind of not reliable.
I don't know what is the trick but here is my 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>
And then with my java code I display the new fragment :
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;
}
The issue should be there I think because I don't see any other way that the button would be in the AperoDetailFragment