Android:How to create different view in ViewPager?

前端 未结 2 1462
难免孤独
难免孤独 2021-01-03 11:41

I have create Main_Activity with ViewPager with Android Studio that has a code like this

public class MainActivity extends Activity {
    SectionsPagerAdap         


        
相关标签:
2条回答
  • 2021-01-03 12:21

    A very simple implementation of your question in my other post.

    How to use swipe gesture in a view pager's page with android?

    In that post I have mention how you can customize every layout. In each layout you can add your own buttons or textviews or whatever you want to add. For example, in that post, Layout to put inside the ViewPager in MainLayout, instead of using that image view and all other things, you can add your own desired items.

    I am sure that will solve you problem and also you can add as many layouts as you can and just make that change in the adapter like this:

    private class MyPagerAdapter extends PagerAdapter {
    

    Here can be your page count.. You can increase you page count depending on your layouts.

    public int getCount() {
                return 3;
            }
    
            public Object instantiateItem(ViewGroup container, int position) {
                LayoutInflater inflater = (LayoutInflater) container.getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
                // Using different layouts in the view pager instead of images.
    
                int resId = -1;
    
                       //Getting my layout's in my adapter. Three layouts defined.
    

    Here you can add your layouts you have designed.

      switch (position) {
                case 0:
                    resId = R.layout.tutorial1;
                    break;
                case 1:
                    resId = R.layout.tutorial2;
                    break;
                case 2:
                    resId = R.layout.tutorial3;
                    break;
    
                }
    
                View view = inflater.inflate(resId, container, false);
                ((ViewPager) container).addView(view, 0);
                return view;
            }
    
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
    
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
    
        }
    
    }
    

    Hope I answered your question .. :)

    0 讨论(0)
  • 2021-01-03 12:29

    In the following method implement like this in getItem() :

    @Override
    public Fragment getItem(int position) {           
    
        Fragment fragment = null;
    
        switch(position) {
            case 0 :
               fragment = new MatchFragment();
            break;
            case 1 :
               fragment = new Fragment1();
            break;
            case 2 :
               fragment = new MFragment();
            break;
        }
    
        return fragment;
    }
    
    0 讨论(0)
提交回复
热议问题