Go from one fragment to another when selecting an item from a RecyclerView

4

I'm working with Tabs Fragments. Within the first fragment I receive Data from Firebase and show it with a recyclerview, I use the ItemClickSupport class for the onItemClicked method.

This is my code:

ItemClickSupport.addTo(recycler).setOnItemClickListener(new ItemClickSupport.OnItemClickListener(){
    @Override
    public void onItemClicked(RecyclerView recyclerView, int position, View v){

        Fragment fragment = new NegocioFragment();
        Bundle args = new Bundle();
        args.putString("data", "This data has sent to FragmentTwo");
        fragment.setArguments(args);
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.activity_main, fragment);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.addToBackStack(null);
        transaction.commit();
        Toast.makeText(getActivity().getApplicationContext(), "Diste click en el número: "+recycler.getChildAdapterPosition(v), Toast.LENGTH_SHORT).show();
    }
});

The problem here is that, when I click, the new fragment loads over the already created one, but I want to go from one fragment to another.

This is my activity_main.xml '

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

In my tab_fragment_1.xml I only have the recyclerview

<android.support.v7.widget.RecyclerView
    android:id="@+id/lstCategorias"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

and this is my business_fragment.xml which will receive data in a recyclerview

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Negocios"
    android:id="@+id/lblnegocio"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/lstNegocios"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

and in my BusinessFragment class I only have the onCreateView for the time being

public class NegocioFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.negocio_fragment, container, false);
}
    
asked by Daniel Jaimes 18.01.2017 в 06:14
source

2 answers

1

If you want to replace, instead of the view pager in the ActivityMain, put a FrameLayout where you are loading your Fragments. Therefore create another layout for the viewPager that you will load in said FrameLayout, then load the other fragment in that hole. Let's say that FrameLayout is a hole where you will remove and put fragments.

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<FrameLayout
android:id="@+id/contenedorDeFragmentos"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>

In this way, the onClick code would be the following. Change R.id.activity_main to r.id.Fragment Container

Fragment fragment = new NegocioFragment();
Bundle args = new Bundle();
args.putString("data", "This data has sent to FragmentTwo");
fragment.setArguments(args);
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.contenedorDeFragmentos, fragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(null);
transaction.commit();

I hope it helps you better I have not explained myself well.

    
answered by 11.05.2017 в 20:29
0

The replace method is typically used in FrameLayout when you replace one fragment with another. In your case you should use add :

    Fragment fragment = new NegocioFragment();
    Bundle args = new Bundle();
    args.putString("data", "This data has sent to FragmentTwo");
    fragment.setArguments(args);
    FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
    transaction.add(R.id.activity_main, fragment);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.addToBackStack(null);
    transaction.commit();

If that does not solve the problem, share the xml of your layout please.

    
answered by 18.01.2017 в 06:52