Android FragmentPagerAdapter 检测用户是否访问了特定选项卡

Android FragmentPagerAdapter detect if user has visited a specific tab

我有多个使用 FragmentPagerAdapter 的选项卡,但 ViewPager 在我当前选项卡之前和之后创建了一个页面,即使我不在该选项卡上,也会调用 OnCreate() 方法。在特定选项卡上 运行 特定操作变得非常困难,而不是在访问该选项卡之前执行。无论如何,我可以检测用户是否从选项卡本身访问了特定选项卡?

  public class MainActivity extends ActionBarActivity implements ViewPager.OnPageChangeListener {

    public static final int TAB_PROFILE = 0;
    public static final int TAB_JOBS = 1;
    public static final int TAB_GPS = 2;
    public static final int TAB_COMMENTS = 3;
    public static final int TAB_OTHERS = 4;

    Toolbar toolbar;
    private ViewPager mPager;
    private SlidingTabLayout mTabs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        mPager.setOnPageChangeListener(this);

        mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
        mTabs.setCustomTabView(R.layout.custom_tab_item, R.id.tabText);
        mTabs.setDistributeEvenly(true);
        mTabs.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
        mTabs.setViewPager(mPager);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.action_search) {
            startActivity(new Intent(this, subActivity.class));
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Log.d("CURRENT_TAB", "" + position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class MyPagerAdapter extends FragmentPagerAdapter {

        //String[] tabsTitle = getResources().getStringArray(R.array.tabs);
        int tabIcons[] = {R.drawable.ic_tab_home,
                R.drawable.ic_tab_job,
                R.drawable.ic_tab_map,
                R.drawable.ic_tab_comments,
                R.drawable.ic_tab_others};

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            //tabsTitle = getResources().getStringArray(R.array.tabs);
        }

        @Override
        public Fragment getItem(int position) {

            Fragment fragment = null;
            Logs.logs("getItem called for " + position);

            switch (position) {
                case TAB_PROFILE:
                    fragment = FragmentProfile.newInstance("" + position, "true");
                    break;
                case TAB_JOBS:
                    fragment = FragmentJobs.newInstance("" + position, "true");
                    break;
                case TAB_GPS:
                    fragment = FragmentGPS.newInstance("" + position, "true");
                    break;
                case TAB_COMMENTS:
                    fragment = FragmentComments.newInstance("" + position, "true");
                    break;
                case TAB_OTHERS:
                    fragment = FragmentOthers.newInstance("" + position, "true");
                    break;
            }
            return fragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            Drawable drawable = getResources().getDrawable(tabIcons[position]);
            drawable.setBounds(0, 0, 36, 36);
            ImageSpan imageSpan = new ImageSpan(drawable);
            SpannableString spannableString = new SpannableString(" ");
            spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return spannableString;
        }

        @Override
        public int getCount() {
            return 5;
        }


    }
}

选项卡#1

public class FragmentProfile extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_profile, container,false);
        return view;
}
}

让你的主要片段实现 ViewPager.OnPageChangeListener 怎么样,然后你可以覆盖 onPageSelected

好的,找了好久,我终于知道你是为ViewPager调用setOnPageChangeListener(),而不是为SlidingTabLayout调用setOnPageChangeListener()

这里回答了这个问题,

onPageScrolled() not being called