android – 在滑动抽屉项之间滑动后,TabLayout和ViewPager -Tabs不起作用

前端之家收集整理的这篇文章主要介绍了android – 在滑动抽屉项之间滑动后,TabLayout和ViewPager -Tabs不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
TabLayout和ViewPager – 在滑动抽屉项目之间滑动后,单击选项卡不起作用.

我在有滑动抽屉和Tablayout的应用程序上工作.
我参考了这个教程For Sliding Drawer
For TabLayout.

内滑动抽屉

> TaskList – > TabLayout(标签(今天,每月,每年))
>设置
>订单

我在Sliding Drawer和Tablayout之间有问题.
第一次viewpager加载正确.但是当我移动到下一个抽屉项目(设置片段)之后,返回到TaskList选项卡片段viewpager无法加载任何片段,当滑动该寻呼机然后加载几个片段.

这里有Java代码

public class TaskListFragment extends Fragment {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

public TaskListFragment() {
    // required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_my_page,container,false);

    viewPager = (ViewPager)rootView.findViewById(R.id.viewpager);
    tabLayout = (TabLayout)rootView.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @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) {

        }
    });

    // Inflate the layout for this fragment
    return rootView;
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
    adapter.addFragment(new Mypage_TodayFragment(),"TODAY");
    adapter.addFragment(new Mypage_TodayFragment(),"WEEKLY");
    adapter.addFragment(new Mypage_TodayFragment(),"MONTHLY");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment,String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}

这里.XML代码

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="262dp"
        android:id="@+id/view"
        android:layout_weight="0.16">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"
            app:tabBackground="@drawable/mytabselector"
            app:tabIndicatorColor="@color/Orange" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

       </android.support.design.widget.CoordinatorLayout>

提前致谢!!

解决方法

试试这个而不是

ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());

ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());

这是因为由于在一个片段中有一个viewpager,这个viewpager应该用来进行片段事务的片段管理器应该是一个子片段管理器

原文链接:https://www.f2er.com/android/316083.html

猜你在找的Android相关文章