deko
deko

Reputation: 495

How to display only Fragment Layout

I have a list of beers, which is my main activity MainActivity. Whenever I click in one of them, I want to display a new screen showing the beer info.

This is how my list looks and the code that is executed to create my fragment.

enter image description here

@Override
    public void onClick(View view) {

        // Data to send to fragment
        Beer beer = displayedList.get(getAdapterPosition());
        Bundle bundle = new Bundle();
        bundle.putParcelable("Beer", beer);
        bundle.putParcelableArrayList("BeerList", (ArrayList<? extends Parcelable>) beerList);

        // Begin the transaction
        FragmentTransaction ft = context.getSupportFragmentManager().beginTransaction();
        // Replace the contents of the container with the new fragment
        BeerInfoFragment beerInfoFragment = new BeerInfoFragment();
        beerInfoFragment.setArguments(bundle);
        ft.replace(R.id.frame_layout, beerInfoFragment);
        // or ft.add(R.id.your_placeholder, new FooFragment());
        // Complete the changes added above
        ft.commit();
    }
}

However, instead of displaying the beer's info on a new screen, I got both layouts at the same time:

enter image description here

QUESTION: How can I make to just display the Fragment Layout instead of both?

This is how my MainActivity layout looks like:

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:scrollbars="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<FrameLayout
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

Upvotes: 0

Views: 533

Answers (1)

Muddassir Iqbal
Muddassir Iqbal

Reputation: 162

You need to have two fragments.

  1. BeerListFragment: this fragment will have the recyclerview to display the list of beers you have.
  2. BeerInfoFragment: This fragment will display the detail of the beer item clicked / selected.

Now, on Launch of your MainActivity add the BeerListFragment to the container. Capture the click of the item and after processing it open the BeerInfoFragment as you have mentioned in the question.

Upvotes: 1

Related Questions