如果我的片段有(TabLayout,ViewPager),则startSupportActionMode()返回null

时间:2016-11-03 10:19:00

标签: android android-fragments android-viewpager android-actionmode

startSupportActionMode()失败的原因是我的一个片段由PagerAdapter(TabLayout,ViewPager)创建并使用带有RecyclerView的简单片段工作正常 这里有一些细节:

public class MainActivity extends AppCompatActivity {
...
@Override
protected void onCreate(...) {
...
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    Fragment fragment = null;

    if (id == R.id.tab_fragment) {
        fragment = new TabFragment();
    } else if (id == R.id.simple_fragment) {
        fragment = new SimpleFragment();
    }
...
    if (fragment != null) {
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.containerView, fragment).commit();
    }
    mDrawer.closeDrawer(GravityCompat.START);
    return false;
}

我使用:

public class TabFragment extends Fragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View tab =  inflater.inflate(R.layout.well_tab_layout, null);
    tabLayout = (TabLayout) tab.findViewById(R.id.tabs);
    viewPager = (ViewPager) tab.findViewById(R.id.viewpager);
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
    tabLayout.setupWithViewPager(viewPager);

    return tab;
    }
    class MyAdapter extends FragmentPagerAdapter {
    ...
    }
}

在我需要动作模式的片段中我使用:

public class OneOfFragment extends Fragment implements ActionMode.Callback {
...
if (mActionMode == null)
    mActionMode = ((AppCompatActivity) getActivity()).startSupportActionMode(this);

我的活动主题有NoActionBar。

0 个答案:

没有答案