TabHost 无法处理多个 Tab 开关
TabHost not working upon multiple Tab switches
我制作了一个 Master/Detail 视图,其中在 Detailview 中包含一个 TabHost。
我必须填充一个选项卡列表,每个选项卡都有一个列表视图作为其内容。
现在,当我更改要显示的项目时,在大约第三次更改之后,不再呈现选项卡内容(呈现指示器)。但是当我更改选项卡时,内容会被渲染。
当我更改要显示的项目时,将调用 DetailFragment 的 onCreate 方法。选项卡已生成并添加到 tabhost。但是在我切换项目显示几次之后,OrderGroupTabFragment 的 onCreateView 方法不再被调用,对我来说没有任何理由。
感谢您的帮助。
这是我的代码:
细节片段:
/**
* A fragment representing a single Order detail screen. This fragment is either
* contained in a {@link OrderListActivity} in two-pane mode (on tablets) or a
* {@link OrderDetailActivity} on handsets.
*/
public class OrderDetailFragment extends Fragment implements IASyncMethodCallbacks {
private static Logger _logger = Logger.getLogger(OrderDetailFragment.class.getSimpleName());
/**
* The fragment argument representing the item ID that this fragment
* represents.
*
*/
public static final String ARG_ORDER = "order";
private TrafficSafetyOrder _order;
private TrafficSafetyOrderService _trafficSafetyOrderService;
private boolean _detached;
private FragmentTabHost tabHost;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public OrderDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
_detached = false;
_trafficSafetyOrderService = new TrafficSafetyOrderService();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_order_detail,
container, false);
tabHost = (FragmentTabHost)rootView.findViewById(R.id.order_detail_tab_host);
tabHost.setup(getActivity(), ((FragmentActivity)getActivity()).getSupportFragmentManager() , android.R.id.tabcontent);
OrderListItem order = (OrderListItem)getArguments().getSerializable(ARG_ORDER);
try {
if(order != null)
_order = _trafficSafetyOrderService.getOrderForId(order.getId());
} catch (Exception e) {
_logger.log(Level.WARNING, e.getMessage(), e);
}
if (_order != null) {
OrderId orderId = _order.getOrderIdByOrderIdType(OrderIdTypes.GEB);
((TextView) rootView.findViewById(R.id.order_detail_header))
.setText("Auftrag: " + orderId.getName() + orderId.getId());
for (final Group formGroup : _order.getFormGroups()) {
System.out.println("Adding Tab " + formGroup.getDisplayText());
TabSpec spec = tabHost.newTabSpec(formGroup.getDisplayText());
System.out.println("Spec created " + formGroup.getDisplayText());
spec.setIndicator(formGroup.getDisplayText());
System.out.println("Indicator set " + formGroup.getDisplayText());
Bundle bundle = new Bundle();
System.out.println("Bundle created " + formGroup.getDisplayText());
bundle.putSerializable(OrderGroupTabFragment.ORDER_GROUP_LIST_ARG, formGroup.getItems());
System.out.println("Added items to bundle " + formGroup.getDisplayText());
tabHost.addTab(spec, OrderGroupTabFragment.class, bundle);
System.out.println("Tab added " + formGroup.getDisplayText());
}
}
return rootView;
}
@Override
public void onStart() {
super.onStart();
tabHost.setCurrentTab(0);
}
public TrafficSafetyOrder getOrder() {
return _order;
}
public void setOrder(TrafficSafetyOrder _order) {
this._order = _order;
}
@Override
public void onDestroyView() {
super.onDestroyView();
try {
_trafficSafetyOrderService.updateTrafficServiceOrder(_order);
} catch (Exception e) {
_logger.log(Level.WARNING, e.getMessage(), e);
}
}
@Override
public void onDetach() {
super.onDetach();
_detached = true;
}
@Override
public void onASyncMethodCompleted(Object receiver, Method method) {
if(_detached)// if fragment is already detached from Activity, do not execute callback
return;
Toast.makeText(getActivity(), receiver != null ? receiver.getClass().getSimpleName() : "NULL" + "." + method != null ? method.getName() : "NULL" + " executed succesfully", Toast.LENGTH_LONG).show();
}
}
OrderGroupTabFragment:
public class OrderGroupTabFragment extends android.support.v4.app.Fragment{
public static final String ORDER_GROUP_LIST_ARG = "ORDER_GROUP_ITEMS";
private List<GroupItem> groupItems;
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("Creating Order List View");
View view = inflater.inflate(R.layout.fragment_order_group_tab_list, null);
groupItems = (List<GroupItem>)getArguments().getSerializable(ORDER_GROUP_LIST_ARG);
listView = (ListView)view.findViewById(R.id.order_detail_tab_list_view);
listView.setAdapter(new GroupItemAdapter(getActivity(), groupItems));
System.out.println("Order List View Created");
return view;
}
}
GroupItemAdapter:
public class GroupItemAdapter extends ArrayAdapter<GroupItem> {
private Context _context;
public GroupItemAdapter(Context context, List<GroupItem> groupItems) {
super(context, R.layout.lvi_group_item , groupItems);
this._context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
LayoutInflater li = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.lvi_group_item,
parent, false);
holder = new ViewHolder();
holder.header = (TextView)convertView.findViewById(R.id.tab_header);
holder.isVerified = (CheckBox)convertView.findViewById(R.id.tab_verification_checkbox);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
GroupItem item = getItem(position);
holder.header = (TextView)convertView.findViewById(R.id.tab_header);
holder.isVerified = (CheckBox)convertView.findViewById(R.id.tab_verification_checkbox);
if(item != null)
holder.header.setText(item.getName());
return convertView;
}
private class ViewHolder {
TextView header;
CheckBox isVerified;
}
}
fragment_order_detail.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/order_detail_button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="@android:style/Widget.ActionBar">
<Button android:id="@+id/order_detail_button_undo"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="@android:style/Widget.ActionButton"
android:drawableTop="@drawable/ic_action_undo"
android:text="Undo"
android:layout_weight="1"/>
<Button android:id="@+id/order_detail_button_redo"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="@android:style/Widget.ActionButton"
android:drawableTop="@drawable/ic_action_redo"
android:text="Redo"
android:layout_weight="1"/>
<Button android:id="@+id/order_detail_button_submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="@android:style/Widget.ActionButton"
android:drawableTop="@drawable/ic_action_tick"
android:text="Submit"
android:layout_weight="1"/>
</LinearLayout>
<TextView
style="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_below="@id/order_detail_button_bar"
android:layout_alignParentStart="true"
android:id="@+id/order_detail_header"/>
<android.support.v4.app.FragmentTabHost android:id="@+id/order_detail_tab_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/order_detail_header">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@android:id/tabs"/>
</RelativeLayout>
</android.support.v4.app.FragmentTabHost>
fragment_order_group_tab_list.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/order_detail_tab_list_view"/>
</RelativeLayout>
lvi_group_item.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="@+id/tab_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"/>
<CheckBox android:id="@+id/tab_verification_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/tab_header"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_toEndOf="@id/tab_header"
android:text="@string/lvi_group_item_verification_succesfull"
/>
</RelativeLayout>
gr33tz 黑帮鱼
成功了。
我在 DetailFragment 的 OnCreateView 方法中将错误的 fragmentmanager 传递给了 tabhost.setup() 方法。
tabHost.setup(getActivity(), getChildFragmentManager() , android.R.id.tabcontent);
我制作了一个 Master/Detail 视图,其中在 Detailview 中包含一个 TabHost。 我必须填充一个选项卡列表,每个选项卡都有一个列表视图作为其内容。
现在,当我更改要显示的项目时,在大约第三次更改之后,不再呈现选项卡内容(呈现指示器)。但是当我更改选项卡时,内容会被渲染。 当我更改要显示的项目时,将调用 DetailFragment 的 onCreate 方法。选项卡已生成并添加到 tabhost。但是在我切换项目显示几次之后,OrderGroupTabFragment 的 onCreateView 方法不再被调用,对我来说没有任何理由。 感谢您的帮助。
这是我的代码:
细节片段:
/**
* A fragment representing a single Order detail screen. This fragment is either
* contained in a {@link OrderListActivity} in two-pane mode (on tablets) or a
* {@link OrderDetailActivity} on handsets.
*/
public class OrderDetailFragment extends Fragment implements IASyncMethodCallbacks {
private static Logger _logger = Logger.getLogger(OrderDetailFragment.class.getSimpleName());
/**
* The fragment argument representing the item ID that this fragment
* represents.
*
*/
public static final String ARG_ORDER = "order";
private TrafficSafetyOrder _order;
private TrafficSafetyOrderService _trafficSafetyOrderService;
private boolean _detached;
private FragmentTabHost tabHost;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public OrderDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
_detached = false;
_trafficSafetyOrderService = new TrafficSafetyOrderService();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_order_detail,
container, false);
tabHost = (FragmentTabHost)rootView.findViewById(R.id.order_detail_tab_host);
tabHost.setup(getActivity(), ((FragmentActivity)getActivity()).getSupportFragmentManager() , android.R.id.tabcontent);
OrderListItem order = (OrderListItem)getArguments().getSerializable(ARG_ORDER);
try {
if(order != null)
_order = _trafficSafetyOrderService.getOrderForId(order.getId());
} catch (Exception e) {
_logger.log(Level.WARNING, e.getMessage(), e);
}
if (_order != null) {
OrderId orderId = _order.getOrderIdByOrderIdType(OrderIdTypes.GEB);
((TextView) rootView.findViewById(R.id.order_detail_header))
.setText("Auftrag: " + orderId.getName() + orderId.getId());
for (final Group formGroup : _order.getFormGroups()) {
System.out.println("Adding Tab " + formGroup.getDisplayText());
TabSpec spec = tabHost.newTabSpec(formGroup.getDisplayText());
System.out.println("Spec created " + formGroup.getDisplayText());
spec.setIndicator(formGroup.getDisplayText());
System.out.println("Indicator set " + formGroup.getDisplayText());
Bundle bundle = new Bundle();
System.out.println("Bundle created " + formGroup.getDisplayText());
bundle.putSerializable(OrderGroupTabFragment.ORDER_GROUP_LIST_ARG, formGroup.getItems());
System.out.println("Added items to bundle " + formGroup.getDisplayText());
tabHost.addTab(spec, OrderGroupTabFragment.class, bundle);
System.out.println("Tab added " + formGroup.getDisplayText());
}
}
return rootView;
}
@Override
public void onStart() {
super.onStart();
tabHost.setCurrentTab(0);
}
public TrafficSafetyOrder getOrder() {
return _order;
}
public void setOrder(TrafficSafetyOrder _order) {
this._order = _order;
}
@Override
public void onDestroyView() {
super.onDestroyView();
try {
_trafficSafetyOrderService.updateTrafficServiceOrder(_order);
} catch (Exception e) {
_logger.log(Level.WARNING, e.getMessage(), e);
}
}
@Override
public void onDetach() {
super.onDetach();
_detached = true;
}
@Override
public void onASyncMethodCompleted(Object receiver, Method method) {
if(_detached)// if fragment is already detached from Activity, do not execute callback
return;
Toast.makeText(getActivity(), receiver != null ? receiver.getClass().getSimpleName() : "NULL" + "." + method != null ? method.getName() : "NULL" + " executed succesfully", Toast.LENGTH_LONG).show();
}
}
OrderGroupTabFragment:
public class OrderGroupTabFragment extends android.support.v4.app.Fragment{
public static final String ORDER_GROUP_LIST_ARG = "ORDER_GROUP_ITEMS";
private List<GroupItem> groupItems;
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("Creating Order List View");
View view = inflater.inflate(R.layout.fragment_order_group_tab_list, null);
groupItems = (List<GroupItem>)getArguments().getSerializable(ORDER_GROUP_LIST_ARG);
listView = (ListView)view.findViewById(R.id.order_detail_tab_list_view);
listView.setAdapter(new GroupItemAdapter(getActivity(), groupItems));
System.out.println("Order List View Created");
return view;
}
}
GroupItemAdapter:
public class GroupItemAdapter extends ArrayAdapter<GroupItem> {
private Context _context;
public GroupItemAdapter(Context context, List<GroupItem> groupItems) {
super(context, R.layout.lvi_group_item , groupItems);
this._context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
LayoutInflater li = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.lvi_group_item,
parent, false);
holder = new ViewHolder();
holder.header = (TextView)convertView.findViewById(R.id.tab_header);
holder.isVerified = (CheckBox)convertView.findViewById(R.id.tab_verification_checkbox);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
GroupItem item = getItem(position);
holder.header = (TextView)convertView.findViewById(R.id.tab_header);
holder.isVerified = (CheckBox)convertView.findViewById(R.id.tab_verification_checkbox);
if(item != null)
holder.header.setText(item.getName());
return convertView;
}
private class ViewHolder {
TextView header;
CheckBox isVerified;
}
}
fragment_order_detail.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/order_detail_button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="@android:style/Widget.ActionBar">
<Button android:id="@+id/order_detail_button_undo"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="@android:style/Widget.ActionButton"
android:drawableTop="@drawable/ic_action_undo"
android:text="Undo"
android:layout_weight="1"/>
<Button android:id="@+id/order_detail_button_redo"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="@android:style/Widget.ActionButton"
android:drawableTop="@drawable/ic_action_redo"
android:text="Redo"
android:layout_weight="1"/>
<Button android:id="@+id/order_detail_button_submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="@android:style/Widget.ActionButton"
android:drawableTop="@drawable/ic_action_tick"
android:text="Submit"
android:layout_weight="1"/>
</LinearLayout>
<TextView
style="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_below="@id/order_detail_button_bar"
android:layout_alignParentStart="true"
android:id="@+id/order_detail_header"/>
<android.support.v4.app.FragmentTabHost android:id="@+id/order_detail_tab_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/order_detail_header">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@android:id/tabs"/>
</RelativeLayout>
</android.support.v4.app.FragmentTabHost>
fragment_order_group_tab_list.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/order_detail_tab_list_view"/>
</RelativeLayout>
lvi_group_item.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="@+id/tab_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"/>
<CheckBox android:id="@+id/tab_verification_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/tab_header"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_toEndOf="@id/tab_header"
android:text="@string/lvi_group_item_verification_succesfull"
/>
</RelativeLayout>
gr33tz 黑帮鱼
成功了。
我在 DetailFragment 的 OnCreateView 方法中将错误的 fragmentmanager 传递给了 tabhost.setup() 方法。
tabHost.setup(getActivity(), getChildFragmentManager() , android.R.id.tabcontent);