我制作了一个示例项目,其中包含带滑动标签的导航抽屉。我使用了android studio中的ViewPager,TabLayout和NavigationDrawer模板。
问题是有时片段视图是可见的,但是一旦我选择了不同的抽屉项目,视图对应于该片段变得不可见,当我向后滑动标签时,它变得可见。 我使用了com.android.support:design:22.2.0'支持相同的库
这里是详细的代码:
swipe_tab.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</LinearLayout>
fragment_page.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
ViewPagerFragment.java
public class ViewPagerFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.swipe_tabs,container,false);
ViewPager viewPager = (ViewPager)view. findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getFragmentManager(),getActivity()));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
return view;
}
public static ViewPagerFragment newInstance(int sectionNumber) {
ViewPagerFragment fragment = new ViewPagerFragment();
/*Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);*/
return fragment;
}
}
PageFragment.java
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) view;
textView.setText("Fragment #" + mPage);
return view;
}
}
SampleFragmentPagerAdapter.java
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 10;
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" ,"Tab4","Tab5","Tab6","Tab7" ,"Tab8","Tab9","Tab10"};
private Context context;
public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
MainActivity.java
**At last I made the following changes in the Navigation Drawer's template provided in the android studio**
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
switch (position) {
case 0:
fragmentManager.beginTransaction()
.replace(R.id.container, ViewPagerFragment.newInstance(position + 1))
.commit();
break;
default: fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
}
答案 0 :(得分:6)
使用
viewPager.setAdapter(new SampleFragmentPagerAdapter(getChildFragmentManager(),getActivity()));
而不是
viewPager.setAdapter(new SampleFragmentPagerAdapter(getFragmentManager(),getActivity()));