Layout coordinator replaces fragments

I have a problem replacing fragments in a CoordinatorLayout container.

I have a class with the coordinator Layout AppBarLayout and CollapsingToolbarLayout.

When the action begins, the first snippet is added to show some categories using RecyclerView. If any category is selected, this fragment is replaced by the product fragment, which is a list of Products, also using RecyclerView.

The problem is when I select a category, the second fragment looks empty, the list does not appear as it should.

If I use ListView instead of CoordinatorLayout, everything works fine.

And if I use add in the second snippet, both lists are displayed overlapping.

Any help with this problem?

Here is the XML for the Activity:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/container">


<android.support.design.widget.AppBarLayout
    android:id="@+id/appbarLayout"
    android:layout_height="192dp"
    android:layout_width="match_parent">
    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/ctlLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        app:layout_collapseMode="parallax">
        <android.support.v7.widget.Toolbar
            android:id="@+id/appbar"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            app:layout_scrollFlags="scroll|enterAlways"
            app:layout_collapseMode="pin"/>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

XML , RView:

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/catalogueRV"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

:

(....) 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_coordinator);

    //App bar
    Toolbar toolbar = (Toolbar) findViewById(R.id.appbar);
    setSupportActionBar(toolbar);

    final ActionBar ab = getSupportActionBar();
    //CAMBIAR POR WHITE PARA QUE SEAN IGUALES...
    ab.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black);
    ab.setDisplayHomeAsUpEnabled(true);

    //CollapsingToolbarLayout
    ctlLayout = (CollapsingToolbarLayout)findViewById(R.id.ctlLayout);
    ctlLayout.setTitle(getString(R.string.categories));


    //Rellena la Actividad con SubcategoriasFragment
    Fragment fragmentSubcategorias = new SubcategoriasFragment2();
    FragmentManager fragmentManagerMain = getSupportFragmentManager();
    fragmentManagerMain.beginTransaction()
            .add(R.id.container, fragmentSubcategorias).commit();
}

(...)

@Override
public void onSubcategorySelected(int idSubcategory) {
    //Argumentos para pasar al ProductosFragment
    final Bundle bundle = new Bundle();
    bundle.putInt("idSubcategoria", idSubcategory);

    Fragment fragmentProductos = ProductosFragment2.newInstance(bundle);
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .add(R.id.container,
                 fragmentProductos).addToBackStack(null).commit();
}

1:

(....)
public static SubcategoriasFragment2 newInstance(Bundle arguments){
    SubcategoriasFragment2 f = new SubcategoriasFragment2();
    if(arguments != null){
        f.setArguments(arguments);
    }
    return f;
}

public SubcategoriasFragment2() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.activity_coordinator2, container, false);

    int idCategoria=22;
    //INICIO EL DAO
    this.mSubcategoriasDAO = new SubcategoriasDAO(view.getContext());
    //Rellena la lista de Subcategorias
    mListaSubcategorias = mSubcategoriasDAO.getSubcategoriasDeCategoria(idCategoria);

    mRecycler = (RecyclerView) view.findViewById(R.id.catalogueRV);
    mRecycler.setHasFixedSize(true);
    // Usar un administrador para LinearLayout
    mLManager = new LinearLayoutManager(view.getContext());
    mRecycler.setLayoutManager(mLManager);

    //Inicio el Adapter de Subcategorias
    mAdapter = new SubcategoriasAdapter(mListaSubcategorias);
    mRecycler.setAdapter(mAdapter);

    //Maneja los eventos de click
    clickHandle();

    return view;
}

public void clickHandle(){
    /**
     * Manejo de Clicks.
     * GestureDetector.
     * Devuelve true si hay un click sencillo.
     */
    final GestureDetector subcategoriasGestureDetector = new GestureDetector(getActivity(),
            new GestureDetector.SimpleOnGestureListener(){
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }
            });

    /**
     * Define addOnItemTL del RV al que se le pasa el objeto OnItemTouchListener
     * Cuando se crea un objeto OnItemTocuhListener, se sobrescriben dos métodos:
     * onInterceptTouchEvent() le dice el tipo de Gesto que ha detectado
     */
    mRecycler.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        /**
         * onInterceptTouchEvent() detecta el evento
         * @param recyclerView
         * @param motionEvent
         * @return
         */
        @Override
        public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {

            View child = recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());

            if(child!=null && subcategoriasGestureDetector.onTouchEvent(motionEvent)){
                int position = mRecycler.getChildAdapterPosition(child);
                //Obtiene el id de la Subcategoría
                int idSubcategoria = mListaSubcategorias.get(position).getScId();

                mListener.onSubcategorySelected(idSubcategoria);

                //Actualiza nombre de la ventana
                getActivity().setTitle(mListaSubcategorias.get(position).getScNombre());

                return true;
            }
            return false;
        }
        @Override
        public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
            /**
             * ON TOUCH EVENT
             */
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    });

}



@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.e("Subcategorias2Fragment", "onAttach()");
    try {
        mListener = (OnSubcategorySelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnSubcategorySelectedListener");
    }
}

2:

 (...)
 public static ProductosFragment2 newInstance(Bundle arguments){
    ProductosFragment2 f = new ProductosFragment2();
    if(arguments != null){
        f.setArguments(arguments);
    }
    return f;
}

public ProductosFragment2() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    //Obtiene los argumentos del Fragment
    Bundle args = getArguments();
    int idSubcategoriaArg=0;
    if (args  != null && args.containsKey("idSubcategoria")) {
        idSubcategoriaArg = args.getInt("idSubcategoria");
        Log.e("ARGS ProductosFragment", args.toString());
    }

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.activity_coordinator2, container, false);

    //INICIO EL DAO
    this.mProductosDAO = new ProductosDAO(getActivity());
    //Rellena la lista de Producto
    mListaProductos= mProductosDAO.getProductosDeSubcategoria(idSubcategoriaArg);

    mRecycler = (RecyclerView) view.findViewById(R.id.catalogueRV);
    mRecycler.setHasFixedSize(true);
    // Usar un administrador para LinearLayout
    mLManager = new LinearLayoutManager(getActivity());
    mRecycler.setLayoutManager(mLManager);

    //Inicio el Adapter de Producto
    mAdapter = new ProductosAdapter(mListaProductos);
    mRecycler.setAdapter(mAdapter);


    Log.e("ProductosFragment", ""+mListaProductos.size());


    return view;
}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.e("Productos2Fragment", "onAttach()");
    try {
        mListener = (OnSubcategorySelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnSubcategorySelectedListener");
    }
}
+4
1

, , .

:

CoordinatorLayout , , ListView FrameLayout ..

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<!--The main content view-->
<FrameLayout
        android:id="@+id/main_content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

Layout , Activity onCreate .

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initView();
    setListener();
    setupMainFragment();
}

private void setupMainFragment() {
    MainFragment mainFragment = new MainFragment();
    getSupportFragmentManager().beginTransaction().replace(R.id.main_content_layout, mainFragment).commit();
}

, CoordinatorLayout , .

, .

+6

All Articles