ViewPager nested in ViewPager

后端 未结 3 1031
离开以前
离开以前 2020-12-25 09:47

I\'m really newbie in android and I would appreciate any help for my course work.

I need to do:

1) two ViewPagers (not nested) in one Activity

2) two

3条回答
  •  [愿得一人]
    2020-12-25 10:38

    I think you should reconsider having a viewpager inside another viewpager. You will have a lot of problems with the touch events plus the user experience might be confusing/tricky, I suggest you to rethink that one.

    Now for question 1:

    Declare both viewpagers in the xml file of the activity (/layout/activity.xml), for example:

    
    
    
        
    
        
    
    
    

    Then inflate the xml on the onCreate method of your activity and wire both of the viewpagers:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
    
        mViewpager1 = (ViewPager) findViewById(R.id.viewpager1);
        mViewpager2 = (ViewPager) findViewById(R.id.viewpager2);
    }
    

    Don't forget to declare those two variables private ViewPager mViewpager1, mViewpager2;

    Then you can do whatever you want with mViewpager1 and mViewpager2. One more tip, i suggest you to use adapters to set the pages instead of adding them manually one by one to each viewpager, it will be much cleaner and better to operate with.

提交回复
热议问题