我有一个项目需要有两个viewpager,每个都在活动的不同片段中.
这样的架构是这样的
问题是当寻呼机视图膨胀时,另一个不是.
例如,可以通过单击访问片段1,2和3.当我点击第一个片段时,它会完美地展开视图寻呼机及其两个片段,但是如果我点击片段3,则视图寻呼机不会使片段膨胀,而且标签也会变慢.再次打开应用程序进程,首先单击片段3,它可以正常工作,但点击后面的第一个,这个问题再次发生.
已经调试过程并没有什么不同.
我的活动
public class HomeActivity extends AppCompatActivity implements NavigationFragment.OnNavigationListener{ public NavigationFragment mNavigation; private String mTagFragmentShowing = ""; private int currentNavigation = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); ButterKnife.bind(this); mNavigation = (NavigationFragment) getFragmentManager().findFragmentById(R.id.manager_navigation); onNavigationSelected(currentNavigation); } @Override public void onNavigationSelected(int position) { if (mTagFragmentShowing.equals(String.valueOf(position))) return; currentNavigation = position; mNavigation.setNavigationSelected(position); FragmentManager fragmentManager = getSupportFragmentManager(); Fragment fragment = fragmentManager.findFragmentByTag(mTagFragmentShowing); // Hide last fragment if (fragment != null && fragment.isVisible()) { fragmentManager.beginTransaction().hide(fragment).commit(); } // Show new fragment mTagFragmentShowing = String.valueOf(position); fragment = fragmentManager.findFragmentByTag(mTagFragmentShowing); if (fragment != null && fragment.isHidden()) { fragmentManager.beginTransaction().show(fragment).commit(); } else if (fragment == null) { fragmentManager.beginTransaction().add(R.id.manager_content, getFragmentByPosition(position), mTagFragmentShowing) .setTransition(FragmentTransaction.TRANSIT_NONE).commit(); } } private Fragment getFragmentByPosition(int position) { switch (position) { case 0: return new DiscoverFragment(); case 1: return new UpcomingFragment(); case 2: return new FeaturesFragment(); case 3: return new VideosFragment(); case 4: return new MySongsFragment(); } return null; }
我的导航片段.它控制其他片段
public class NavigationFragment extends Fragment implements View.OnClickListener { public ViewHolder mHolder; public View mButtonSelected; public OnNavigationListener mCallback; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.component_navigation, container, false); ButterKnife.bind(this, view); initView(view); return view; } public void initView(View view) { mHolder = new ViewHolder(); mHolder.discover = view.findViewById(R.id.component_navigation_discover); mHolder.upcoming = view.findViewById(R.id.component_navigation_upcoming); mHolder.features = view.findViewById(R.id.component_navigation_features); mHolder.videos = view.findViewById(R.id.component_navigation_videos); mHolder.mymusic = view.findViewById(R.id.component_navigation_mymusic); mHolder.discover.setTag(0); mHolder.upcoming.setTag(1); mHolder.features.setTag(2); mHolder.videos.setTag(3); mHolder.mymusic.setTag(4); mHolder.discover.setOnClickListener(this); mHolder.upcoming.setOnClickListener(this); mHolder.features.setOnClickListener(this); mHolder.videos.setOnClickListener(this); mHolder.mymusic.setOnClickListener(this); mButtonSelected = mHolder.features; } public void setNavigationSelected(int position) { setNavigationButtonSelected(false); switch (position) { case 0: mButtonSelected = mHolder.discover; break; case 1: mButtonSelected = mHolder.upcoming; break; case 2: mButtonSelected = mHolder.features; break; case 3: mButtonSelected = mHolder.videos; break; case 4: mButtonSelected = mHolder.mymusic; break; } setNavigationButtonSelected(true); } public void setNavigationButtonSelected(boolean enabled) { mButtonSelected.setBackgroundColor(enabled ? getResources().getColor(R.color.color_red) : Color.TRANSPARENT); } @Override public void onClick(View view) { if (view != mButtonSelected) { mCallback.onNavigationSelected((Integer) view.getTag()); } } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallback = (OnNavigationListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnNavigationSelectedListener"); } } @Override public void onDetach() { super.onDetach(); mCallback = null; } static class ViewHolder { View discover; View upcoming; View features; View videos; View mymusic; } public interface OnNavigationListener { public void onNavigationSelected(int position); }
我的片段与视图寻呼机
public class MySongsFragment extends Fragment { private OnFragmentInteractionListener mListener; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getActivity().getSupportFragmentManager().beginTransaction().add(R.id.mysongs_fragment_container, MySongsViewPagerFragment.newInstance(0)).commit(); return inflater.inflate(R.layout.fragment_my_songs, container, false); } @Override public void onDetach() { super.onDetach(); mListener = null; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * * See the Android Training lesson Communicating with Other Fragments for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name public void onFragmentInteraction(Uri uri); }
我的观点寻呼机
public class MySongsViewPagerFragment extends Fragment { private static final int NUM_PAGES = 4; private String[] tab_names; private ListmTabNames; private PagerAdapter mPagerAdapter; public @Bind(R.id.pager) ViewPager mPager; public static MySongsViewPagerFragment newInstance(int index) { MySongsViewPagerFragment fragment = new MySongsViewPagerFragment(); Bundle args = new Bundle(); args.putInt("index", index); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = initView(inflater, container); return view; } @NonNull private View initView(LayoutInflater inflater, ViewGroup container) { View view = inflater.inflate(R.layout.fragment_view_pager_my_songs, container, false); ButterKnife.bind(this, view); tab_names = getResources().getStringArray(R.array.tabs_mysongs); mTabNames = Arrays.asList(tab_names); mPagerAdapter = new ScreenSlidePagerAdapter(getActivity().getSupportFragmentManager()); mPager.setAdapter(mPagerAdapter); mPager.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { mPager.onTouchEvent(event); return false; } }); TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs); tabLayout.setupWithViewPager(mPager); return view; } private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { public ScreenSlidePagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: return new PlaylistsFragment(); case 1: return new SongsFragment(); case 2: return new AlbumsFragment(); case 3: return new ArtistsFragment(); default: return new PlaylistsFragment(); } } @Override public int getCount() { return NUM_PAGES; } @Override public CharSequence getPageTitle(int position) { return mTabNames.get(position); } }
André Dantas.. 8
问题是viewpager中的活动上下文
在我的视图中寻呼机.我改变
mPagerAdapter = new ScreenSlidePagerAdapter(getActivity().getSupportFragmentManager()); mPager.setAdapter(mPagerAdapter);
至
mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager()); mPager.setAdapter(mPagerAdapter);
两个viewpager使用相同的上下文.因此,当我第一次膨胀时,另一个没有膨胀,因为解释已经存在一个膨胀的viewpager.
感谢mcwise帮助向我展示方法getChildFragmentManager().
问题是viewpager中的活动上下文
在我的视图中寻呼机.我改变
mPagerAdapter = new ScreenSlidePagerAdapter(getActivity().getSupportFragmentManager()); mPager.setAdapter(mPagerAdapter);
至
mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager()); mPager.setAdapter(mPagerAdapter);
两个viewpager使用相同的上下文.因此,当我第一次膨胀时,另一个没有膨胀,因为解释已经存在一个膨胀的viewpager.
感谢mcwise帮助向我展示方法getChildFragmentManager().