【问题标题】:Changing tabs without swiping无需滑动即可更改标签
【发布时间】:2017-02-01 13:30:37
【问题描述】:

我想在标签之间切换而不向左或向右滑动。使用下面的类,这是 Android Studio 活动之一,我正在尝试通过使用外部事件从一个选项卡移动到另一个选项卡。

我该怎么做?我应该实现/覆盖哪些方法?

public class Tabbed extends Activity {

    private SectionsPagerAdapter mSectionsPagerAdapter;

    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabbed);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }

    @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_tabbed, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
    * A placeholder fragment containing a simple view.
    */
    public static class PlaceholderFragment extends Fragment {
    /**
    * The fragment argument representing the section number for this
    * fragment.
    */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
    * Returns a new instance of this fragment for the given section
    * number.
    */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tabbed, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }

    /**
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
    * one of the sections/tabs/pages.
    */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
        // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                return "SECTION 1";
                case 1:
                return "SECTION 2";
                case 2:
                return "SECTION 3";
            }
            return null;
        }
    }
}

也许我可以这样做:

@Override public void changeTab(int where) {
    mViewPager.setCurrentItem(mViewPager.getCurrentItem()+where)‌​; 
}

其中changeTab()方法是mainActivity中定义的一个接口。 我的 mainActivity 中也有:

intent=new Intent(this, Tabbed.class);
startActivity(intent);

如何从 mainActivity 调用 changeTab() 方法?

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以为此使用ViewPager 对象的setCurrentItem() 函数。

    例子:

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setCurrentItem(2); // set number of desired tab
    

    编辑:对于您的第二个问题,StackOverflow 上已经提出了很多问题。您不能只调用另一个活动的方法。您可以向意图添加额外数据以强制活动启动功能。但这不适用于您,因为您想在创建意图后调用该函数。我认为您将不得不查看听众的问题以及活动之间的沟通。 Here 是一个与您的问题密切相关的问题,它可以让您上路。

    【讨论】:

    • 好的,谢谢。对于第二个问题,我用 BroadcastReceiver 的 onReceive() 方法解决了
    • 干得好。很高兴它对你有用。祝你好运!
    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多