在Android中为ViewPager设置默认页面


67

我正在使用以下代码,MAX为2页。默认情况下,位置为0,并在右侧添加一个新页面。我给两个布局文件充气。

应用启动时如何显示page1并在左侧添加新页面?谢谢。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <android.support.v4.view.ViewPager
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:id="@+id/pagerView" />


</LinearLayout>

Java代码

public class MyPagerActivity extends Activity {
    private Context context;    
    private int pageNumber;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = this;

        ViewPager pagerView = (ViewPager)findViewById(R.id.pagerView);
        pagerView.setAdapter(new AwesomePagerAdapter());    

    }


    private class AwesomePagerAdapter extends PagerAdapter{

        @Override
        public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((View)view);            
        }

        @Override
        public void finishUpdate(View arg0) {
            //setPageTitles(getPageNumber());
        }

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

        @Override
        public Object instantiateItem(View collection, int position) {
            /*  TextView tv = new TextView(MyPagerActivity.this);
        tv.setText("Bonjour PAUG " + position);
        tv.setTextColor(Color.WHITE);
        tv.setTextSize(30);

        ((ViewPager) collection).addView(tv,0);

        return tv;*/

            View view = getViewToShow(position);
            ((ViewPager) collection).addView(view,0);
            return view;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==((View)object);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

    }


    private View getViewToShow(int position){
        View view = null;
        View layout; 
        LayoutInflater mInflater =  (LayoutInflater)
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        switch(position){

        case 0: 
            layout  = mInflater.inflate(R.layout.elements, null);
            view = layout;
            break;
        case 1: view = 
            layout  = mInflater.inflate(R.layout.elements2, null);
        view = layout;

        break;
        }
        return view;
    }

}

Answers:



54

可接受的解决方案很好,但是在设置适配器后调用此方法很重要。

    viewPager.setAdapter(adapterViewPager);
    viewPager.setCurrentItem(1);

7

默认情况下,您必须将当前值设置为1,以仅显示一个默认视图。如果未设置setCurrentItem为1,则看不到任何内容。

viewPager.setAdapter(adapterViewPager);
viewPager.setCurrentItem(1)

0

此解决方案提供了设置默认页面的功能,而不会受到Pager适配器类外部的干扰。

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View currentPage = null;
    switch(position){
        case 0:
            currentPage = LayoutInflater.from(context).inflate(R.layout.page0, null)    
            break;
        case 1:
            currentPage = LayoutInflater.from(context).inflate(R.layout.page1, null)    
            ///////////// This page will be default ////////////////////
            ((ViewPager)container).setCurrentItem(position);
            ////////////////////////////////////////////////////////////
            break;
        case 2:
            currentPage = LayoutInflater.from(context).inflate(R.layout.page2, null)    
            break;
    return currentPage;
}

0

您仅需要在片段的首次初始化期间设置页面。稍后,它将从“保存的实例状态”中还原用户选择的位置。因此,这看起来更好一些:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    pager.adapter = AwesomePagerAdapter()
    if (savedInstanceState == null) {
        pager.currentItem = 1
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.