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);
Toolbar toolbar = (Toolbar) findViewById(R.id.appbar);
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black);
ab.setDisplayHomeAsUpEnabled(true);
ctlLayout = (CollapsingToolbarLayout)findViewById(R.id.ctlLayout);
ctlLayout.setTitle(getString(R.string.categories));
Fragment fragmentSubcategorias = new SubcategoriasFragment2();
FragmentManager fragmentManagerMain = getSupportFragmentManager();
fragmentManagerMain.beginTransaction()
.add(R.id.container, fragmentSubcategorias).commit();
}
(...)
@Override
public void onSubcategorySelected(int idSubcategory) {
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() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_coordinator2, container, false);
int idCategoria=22;
this.mSubcategoriasDAO = new SubcategoriasDAO(view.getContext());
mListaSubcategorias = mSubcategoriasDAO.getSubcategoriasDeCategoria(idCategoria);
mRecycler = (RecyclerView) view.findViewById(R.id.catalogueRV);
mRecycler.setHasFixedSize(true);
mLManager = new LinearLayoutManager(view.getContext());
mRecycler.setLayoutManager(mLManager);
mAdapter = new SubcategoriasAdapter(mListaSubcategorias);
mRecycler.setAdapter(mAdapter);
clickHandle();
return view;
}
public void clickHandle(){
final GestureDetector subcategoriasGestureDetector = new GestureDetector(getActivity(),
new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
mRecycler.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@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);
int idSubcategoria = mListaSubcategorias.get(position).getScId();
mListener.onSubcategorySelected(idSubcategoria);
getActivity().setTitle(mListaSubcategorias.get(position).getScNombre());
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
}
@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() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle args = getArguments();
int idSubcategoriaArg=0;
if (args != null && args.containsKey("idSubcategoria")) {
idSubcategoriaArg = args.getInt("idSubcategoria");
Log.e("ARGS ProductosFragment", args.toString());
}
View view = inflater.inflate(R.layout.activity_coordinator2, container, false);
this.mProductosDAO = new ProductosDAO(getActivity());
mListaProductos= mProductosDAO.getProductosDeSubcategoria(idSubcategoriaArg);
mRecycler = (RecyclerView) view.findViewById(R.id.catalogueRV);
mRecycler.setHasFixedSize(true);
mLManager = new LinearLayoutManager(getActivity());
mRecycler.setLayoutManager(mLManager);
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");
}
}