1

好吧,我有一个三片段页面滑动应用程序。视图中填充有片段 1、片段 2 和片段 3。当我启动一个应用程序时,它会加载 fragment1(左)。我希望片段 2 成为第一个加载的片段,因此其他片段可以左右关闭。简单地说,当应用程序启动时,我希望它位于中心,而不是左侧。

主要活动:

public class MainActivity extends FragmentActivity {
    private FeedItem feed;

    SectionsPagerAdapter mSectionsPagerAdapter;
     ViewPager mViewPager;

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

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = new Fragment();

        switch (position) {
            case 0:
                return fragment = new Fragment1();
            case 1:
                return fragment = new Fragment2();
            case 2:
                return fragment = new Fragment3();
            default:
                break;

        }
        return fragment;
    }


    @Override
    public int getCount() {

        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.first).toUpperCase(l);
        case 1:
            return getString(R.string.second).toUpperCase(l);
        case 2:
            return getString(R.string.third).toUpperCase(l);
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

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

        return rootView;
    }
}

虚拟.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@android:color/tab_indicator_text"
    tools:context=".MainActivity$DummySectionFragment" >

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

这是我将contex更改为Fragment2的activity_main:

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment2" >

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />

   </android.support.v4.view.ViewPager>
4

1 回答 1

0

这可以通过使用来实现

mViewPager.setCurrentItem(1)

强制平滑滚动:

mViewPager.setCurrentItem(1,true);

强制立即滚动:

mViewpager.setCurrentItem(1, false);

来源:Android Reference set CurrentItem & Source code for viewpager

于 2013-11-01T15:53:49.720 回答