我的容器xml文件为
<?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>
我想在FrameLayout中添加两个以上的片段(id:home_categories)。但是当我尝试在FrameLayout中添加多个片段时,第二个片段会替换第一个片段,甚至第二个片段也不会完全显示(包含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;
}
}
即使我为我添加的每个片段动态创建FrameLayout,也无法显示第一个片段。显示第二个片段(列表视图),其中包含11个子节点,但它只显示一个。
下面,
Handler holderForCategories = new Handler()
{
@Override
public void handleMessage(Message msg) {
Log.i("TheHome","ReceviedMessage");
for(int j = 0 ; j < categories.length ; j ++)
{
FrameLayout frameLayout = new FrameLayout(getActivity());
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
frameLayout.setLayoutParams(params);
frameLayout.setId(j);
linearLayout.addView(frameLayout);
Context context = getActivity().getApplicationContext();
transaction.add(frameLayout.getId(), new CategoriesAndSubcategories(context, categories[j], subcategoryList[j].subcategories),"CATEGORY"+j);
}transaction.commit();
super.handleMessage(msg);
}
};
答案 0 :(得分:2)
请尝试替换
transaction.add(R.id.search_products, new SearchProductsFragment(), "SEARCH_PRODUCTS");
transaction.add(R.id.slideshow, new SlideShowPageFragment(),"SLIDE_SHOW");
带
transaction.replace(R.id.search_products, new SearchProductsFragment());
transaction.replace(R.id.slideshow, new SlideShowPageFragment());
让我知道它是否仍无效。