我发现很难解决这个问题。
我的应用有一个MainActivity,其中我有一个包含TabLayout的TabFragment。 我只想在用户点击第二个标签时在MainActivity的工具栏中显示一个副标题,并在我点击它时隐藏它。
我已经设法通过侧面NavigationDrawer获取它(我也可以从那里选择一个标签,一切正常)。 仍然,我无法理解如何在选项卡上设置setOnClickListener(在片段中),然后只有当我在第二个选项卡中时才要求容器Activity显示字幕。
我在 MainActivity 中使用了这两种方法,用于显示/隐藏字幕。
// Update the petrol stations' total count.
public void updatePetrolStationsCount(Toolbar toolbar) {
PetrolLab petrolLab = PetrolLab.get(getApplicationContext());
int petrolStationsSize = petrolLab.getPetrolStations().size();
String totalCount = getResources().getQuantityString(R.plurals.total_count_format, petrolStationsSize, petrolStationsSize);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setSubtitle(totalCount);
}
// Hide the petrol stations' total count.
public void hidePetrolStationsCount(Toolbar toolbar) {
String totalCount = null;
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setSubtitle(totalCount);
}
在我的 TabFragment 中,我正在使用TabPager的内部类。
class TabPagerAdapter extends FragmentPagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
// Return fragment with respect to position.
@Override
public Fragment getItem(int position) {
Fragment fragment;
switch (position) {
case MAP_POSITION:
fragment = MapFragment.newInstance(MAP_TYPE);
return fragment;
case PETROL_POSITION:
fragment = PetrolStationListFragment.newInstance(position);
return fragment;
case FAV_POSITION:
return new FavouritesFragment();
}
return null;
}
@Override
public int getCount() {
return intItems;
}
}
有关如何告诉MainActivity用户在TabFragment中选择第二个标签的任何提示?就像在每个选项卡上放置一个Listener一样。 感谢。
答案 0 :(得分:1)
您可以在MainActivity上注册BroadCastReceive,在选择第二个选项卡时向BroadCast发送消息 像这样:
protected void onCreate(Bundle savedInstanceState) {
//to register BroadCastReceive
manager = LocalBroadcastManager.getInstance(this);
myReceive = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.action.changetab");//fill you want to write
manager.registerReceiver(myReceive,filter);
}
class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
int xy= intent.getIntExtra("xy",0);
if(xy==1){
//change tab
}
}
}
片段中的
manager = LocalBroadcastManager.getInstance(this);
Intent intent= new Intent("com.action.changetab");//same as you filter
intent.putExtra("xy",1);
manager.sendBroadcast(intent);
您最好先从谷歌学习BroadCast,它会让您对它有更深入的了解
答案 1 :(得分:0)
好的,我设法通过Cgx解决了这个问题。
我已经设置了我的MainActivity,就像他说的那样,然后,在 TabFragment 中设置:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
int flag;
@Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == PETROL_POSITION) {
flag = 1;
sendBroadcast(flag);
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
if (tab.getPosition() == PETROL_POSITION) {
flag = 0;
sendBroadcast(flag);
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
// Send a broadcast to MainActivity about the tab we are inside to.
public void sendBroadcast(int flag) {
mLocalBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
Intent intent = new Intent(ACTION_TAB_ID);
intent.putExtra(EXTRA_TAB_ID, flag);
mLocalBroadcastManager.sendBroadcast(intent);
}