首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android片段,案例2先加载

Android片段,案例2先加载
EN

Stack Overflow用户
提问于 2013-11-01 11:21:51
回答 1查看 486关注 0票数 1

嗯,我有一个三页的滑动应用程序。视图由fragment1、fragment2和fragment3填充。当我启动一个应用程序时,它会加载fragment1 (左)。我希望片段2是加载的第一个片段,所以其他片段被左移和右移。简单地说,当应用程序启动时,我希望它在中间,而不是在左边。

MainActivity:

代码语言:javascript
复制
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;
    }
}

Dummy.xml

代码语言:javascript
复制
<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>

这是activity_main,在这里,我将contex更改为Fragment2:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-01 15:53:49

这可以通过使用

代码语言:javascript
复制
mViewPager.setCurrentItem(1)

强迫一个平滑的卷轴:

代码语言:javascript
复制
mViewPager.setCurrentItem(1,true);

强制立即滚动:

代码语言:javascript
复制
mViewpager.setCurrentItem(1, false);

资料来源:安卓参考集CurrentItem & 查看器的源代码

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19725772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档