我有使用tabhost实现的父片段,并且有两个子片段A和B片段,并且我想在子片段对用户可见时调用方法,我使用了setUserVisibleHint这是一个Override方法。但是在儿童片刻A或B期间从未打电话给用户。
以下是使用过的代码:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
Log.d("setUserVisibleHint***called",""+isVisibleToUser);
if(isVisibleToUser){
calltoMethod();
}
}
父片段代码是:
public class ParentFragment extends Fragment{
private TabHost planTabHost;
LinearLayout tabMainLayout;
private static View plansView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
plansView = inflater.inflate(R.layout.plansfragment, container, false);
planTabHost = (TabHost)plansView. findViewById(android.R.id.tabhost);
planTabHost.setup();
planTabHost.addTab(planTabHost.newTabSpec("tab1").setIndicator("A").setContent(R.id.Afragment));
planTabHost.addTab(planTabHost.newTabSpec("tab2").setIndicator("B").setContent(R.id.Bfragment));
planTabHost.setCurrentTab(1);
}
}
答案 0 :(得分:0)
您可以使用TabHost.OnTabChangeListener
了解当前所选标签位置。
planTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String arg0) {
Log.i("Selected Tab", "index::" + planTabHost.getCurrentTab());
}
});