Android : Adding multiple fragments in same FrameLayout

on Wednesday, March 25, 2015


I have my container xml file as



<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

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

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

<FrameLayout
android:id="@+id/home_categories"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

</ScrollView>


I want to add more than two fragments in FrameLayout(id: home_categories). But when I try to add multiple fragments in the that FrameLayout, second fragment replaces the first one and even second fragment is not displayed fully (contains ListView).


TheCode:



public class TheHome extends Fragment{
private Category[] categories;
private SubcategoryList[] subcategoryList;
private boolean added = false;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.i("Attched","Yes");
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i("Activity","CreatedOfTheHome");
final FragmentManager manager = getFragmentManager() ;
final FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.search_products, new SearchProductsFragment(), "SEARCH_PRODUCTS");
transaction.add(R.id.slideshow, new SlideShowPageFragment(),"SLIDE_SHOW");
Log.i("TheHome","BeforeHandler");
Handler holderForCategories = new Handler()
{
@Override
public void handleMessage(Message msg) {
Log.i("TheHome","ReceviedMessage");
for(int j = 0 ; j < categories.length ; j ++) // a loop to add mutliple fragments
{
Context context = getActivity().getApplicationContext();
transaction.add(R.id.home_categories, new CategoriesAndSubcategories(context, categories[j], subcategoryList[j].subcategories),"CATEGORY"+j);

}transaction.commit();
super.handleMessage(msg);
}
};
getCatsAndSubcats(holderForCategories);
}
public void getCatsAndSubcats(final Handler handler)
{
Log.i("TheHome","GetCatsAndSubcats");
RequestQueue queue = VolleySingleton.getInstance(getActivity())
.getRequestQueue();
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.GET, Links.URL_CATLIST, null,
new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject json) {
Log.i("TheHome","ResponseReceived");
try {
JSONArray array = json.getJSONArray("categories");
JSONObject preamble = array.getJSONObject(0); //outermost object
int totalCategories = Integer.parseInt(preamble.getString("total"));
categories = new Category[totalCategories]; //total number of categories
subcategoryList = new SubcategoryList[totalCategories];
for(int i = 0 ; i < totalCategories ; i ++)
{
categories[i] = new Category() ;
JSONObject object = preamble.getJSONObject("cat"+(i+1));
categories[i].label = object.getString("category_name");
categories[i].background = object.getString("background_color");
JSONObject subcategory = object.getJSONObject("subcategories");
int totalSubcategories = Integer.parseInt(subcategory.getString("total"));
subcategoryList[i] = new SubcategoryList() ;
subcategoryList[i].subcategories = new Subcategory[totalSubcategories];
for(int j = 0 ; j < totalSubcategories ; j ++)
{
JSONObject subcat = subcategory.getJSONObject("sc"+(j+1));
subcategoryList[i].subcategories[j] = subcategoryList[i].new Subcategory() ;
subcategoryList[i].subcategories[j].label = subcat.getString("label");
}
}
Log.i("TheHome","GoingToSendMessage");
handler.sendEmptyMessage(0);

} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
Log.i("VolleyErrorFromCats",arg0.toString());
}
});
Log.i("TheHome","RequestSent");
queue.add(request);
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.i("OnCreateView","OfTheHome");
View view = inflater.inflate(R.layout.the_home, container, false) ;
return view;

}
class SubcategoryList
{
class Subcategory
{
String label ;
}
Subcategory[] subcategories;
}
class Category
{
String label;
String background;
}
}

0 comments:

Post a Comment