首先我想解释一下我的布局。它是“选项卡中的选项卡列表”,这意味着列表视图由选项卡控制,而选项卡列表由另一个选项卡控制。希望你能明白我的意思...
我的两个标签小部件:
(底部标签:主控制面板)
public class BottomTabWidget extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tab);
FragmentTabHost tabHost = (FragmentTabHost)findViewById(R.id.bottom_tab_host);
tabHost.setup(this, getSupportFragmentManager(), R.id.bottom_tab_content);
View exploreView = LayoutInflater.from(this).inflate(R.layout.explore, null);
View browseView = LayoutInflater.from(this).inflate(R.layout.browse, null);
View profileView = LayoutInflater.from(this).inflate(R.layout.profile, null);
View cartView = LayoutInflater.from(this).inflate(R.layout.cart, null);
tabHost.addTab(tabHost.newTabSpec("explore").setIndicator(exploreView), ListTabWidget.class, null);
tabHost.addTab(tabHost.newTabSpec("browse").setIndicator(browseView), SearchList.class, null);
tabHost.addTab(tabHost.newTabSpec("profile").setIndicator(profileView), SearchList.class, null);
tabHost.addTab(tabHost.newTabSpec("cart").setIndicator(cartView), SearchList.class, null);
}
}
(列表选项卡:仅控制列表视图)
public class ListTabWidget extends Fragment{
//for extending Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedIntanceState){
//View view = inflater.inflate(R.layout.list_tab, container);
//FragmentTabHost tabHost = (FragmentTabHost)view.findViewById(R.id.list_tab_host);
FragmentTabHost tabHost = new FragmentTabHost(getActivity());
tabHost.setup(getActivity(), getChildFragmentManager(), R.id.listtabcontent);
View tabView = setTab(inflater, " New Items ");
tabHost.addTab(tabHost.newTabSpec("all_post").setIndicator(tabView), ListPost.class, null);
return tabHost;
}
private View setTab(LayoutInflater inflater, String text){
View tabView = inflater.inflate(R.layout.list_tab_text, null);
TextView textView = (TextView)tabView.findViewById(R.id.textView);
textView.setText(text);
return tabView;
}
}
课堂上的代码ListPost
(太长了,所以我删掉了关键部分)
listView = (ListView)this.getView().findViewById(android.R.id.list);
list = new CustomAdapter(this.getActivity() ,android.R.layout.simple_selectable_list_item, productArray);
listView.setAdapter(list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Product item = (Product) parent.getItemAtPosition(position);
Bundle args = new Bundle();
args.putString("pid", item.getData(WPTemplateDB.PRODUCT_ID)+"");
//switchFragment(args);
new FragmentProcess().switchFragment(getActivity(), R.id.bottom_tab_content, new PostDetail(), args);
}
});
FragmentProcess
班级
public class FragmentProcess {
public void switchFragment(FragmentActivity activity, int element, Fragment replaceFragment, Bundle args){
replaceFragment.setArguments(args);
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.replace(element, replaceFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
当我单击列表视图元素时发生错误(显示为标题),这意味着替换片段。我做错了什么??