我在按钮中单击一个片段(联系人),然后单击“更改选项卡布局选项卡”,然后附加一个详细信息片段(“联系方式”)。 >
public class TabPager extends FragmentStatePagerAdapter {
private final SparseArray<WeakReference<Fragment>> instantiatedFragments = new SparseArray<>();
int tabCount;
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
private String[] tabTitles = new String[]{"tab1", "tab2", "tab3", "tab4"};
private Fragment primary;
public CallTabPager(FragmentManager fm, int tabCount) {
super(fm);
//Initializing tab count
this.tabCount = tabCount;
}
//Overriding method getItem
@Override
public Fragment getItem(int position) {
//Returning the current tabs
switch (position) {
case 0:
Fragment1 tab1 = new Fragment1 ();
return tab1;
case 1:
Fragment2 tab2 = new Fragment2();
return tab2;
case 2:
Fragment3 tab3 = new Fragment3 ();
return tab3;
case 3:
Fragmen4 tab4= new Fragmen4 ();
return tab4;
default:
return null;
}
}
@Override
public int getCount() {
return tabCount;
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
final Fragment fragment = (Fragment) super.instantiateItem(container, position);
instantiatedFragments.put(position, new WeakReference<>(fragment));
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
instantiatedFragments.remove(position);
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
@Nullable
public Fragment getFragment(final int position) {
final WeakReference<Fragment> wr = instantiatedFragments.get(position);
if (wr != null) {
return wr.get();
} else {
return null;
}
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
primary = (Fragment) object;
}
public Fragment getPrimaryFragment() {
return primary;
}
public void setUserVisibleHint(boolean isVisible) {
if (primary != null) {
primary.setUserVisibleHint(isVisible);
}
}
}
在Fragment类中,我使用此Tabpager将标签栏设置为viewpager
公共类CallDataFragment扩展了Fragment实现TabLayout.OnTabSelectedListener,OnBackPressListener {
CallTabPager adapter;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View vw = inflater.inflate(R.layout.activity_contacts_main, container, false);
tabLayout = vw.findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("tab1"));
tabLayout.addTab(tabLayout.newTab().setText("tab2"));
tabLayout.addTab(tabLayout.newTab().setText("tab3"));
tabLayout.addTab(tabLayout.newTab().setText("tab4"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
viewPager = (ViewPager) vw.findViewById(R.id.pager);
adapter = new CallTabPager(getChildFragmentManager(), tabLayout.getTabCount());
viewPager.setOffscreenPageLimit(4);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
return vw;
}
@SuppressLint("LongLogTag")
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SharedPreferences pref = getActivity().getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("key_name", "alls"); // Storing string
editor.apply();
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
@Override
public boolean onBackPressed() {
OnBackPressListener currentFragment = (OnBackPressListener) adapter.getRegisteredFragment(viewPager.getCurrentItem());
if (currentFragment != null) {
return currentFragment.onBackPressed();
}
return false;
}
}
答案 0 :(得分:0)
我要在可见的新片段中创建一个静态实例,并创建一种方法来将新的可见片段替换为较旧的片段。
public class FragmentVisible extends Fragment{
public static FragmentVisible instance;
public static synchronized FragmentVisible getInstance() {
return instance;
}
public class Tab2 extends Fragment implements TabLayout.OnTabSelectedListener, OnBackPressListener {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_visible, container, false);
instance = this;
return view;
}
public void ChangeUI() {
OlderFragemnt OlderFragemnt = new OlderFragemnt ();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.root_view, OlderFragemnt);
fragmentTransaction.commit();
}
}
当选项卡更改时,我检查碎片可见时的片段可见性,即调用方法ChangeUI()
public class TabFragment extends Fragment{
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setUserVisibleHint(true);
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser)
{
Log.d("MyFragment", "Fragment is visible.");
FragmentVisible.getInstance().ChangeUI();
}
else
{
Log.d("MyFragment", "Fragment is not visible.");
}
}
}
感谢您的帮助。