how to use a ViewPager in a fragment

前端 未结 4 1641
甜味超标
甜味超标 2021-01-14 15:29

I download a project about ViewPager and CirclePageIndicator. It works well on my tablet and its code is :

Test1Activity.java:

TestFragmentAdapter mA         


        
相关标签:
4条回答
  • 2021-01-14 15:46

    Check the fragment extends class it should be imported from android.support.v4.app.Fragment Not android.app.Fragment

    0 讨论(0)
  • 2021-01-14 16:01

    Put this code in your MainActivity. The below code will load the Fragment in your Layout.

    Step 1

    Fragment fragment = new PageSlider();
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.content, fragment).commit();
    

    Next we need to implement the ViewPager

    Step 2

    public class PageSlider extends Fragment {
    public PageSlider() {
    // Required empty public constructor
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
        return inflater.inflate(R.layout.page_slider, container, false);
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
      super.onViewCreated(view, savedInstanceState);
      PagerSlidingTabStrip tab = (PagerSlidingTabStrip) view
    .findViewById(com.akshay.protocol10.asplayer.R.id.tabs);
    
     ViewPager pager=(ViewPager)view  .findViewById(com.akshay.protocol10.asplayer.R.id.view_pager);
    MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
    pager.setAdapter(adapter);
    tab.setViewPager(pager);
    }
    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
    }
    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
    }
    @Override
    public void onDetach() {
    super.onDetach();
    }
    public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    public void onFragmentInteraction(Uri uri);
    }
    }
    

    Step 3: Create the Adapter

        public class MyPagerAdapter extends FragmentPagerAdapter {
            private final String[] TITLES = { "Album", "Artist", "Songs", "Genre" };
            public MyPagerAdapter(FragmentManager childFragmentManager) {
          // TODO Auto-generated constructor stub
        super(childFragmentManager);
        }
        @Override
        public CharSequence getPageTitle(int position) {
        // TODO Auto-generated method stub
        return TITLES[position];
        }
        @Override
        public Fragment getItem(int index) {
        // TODO Auto-generated method stub
           Fragment fragment = null;
           if (index == 0) {
           fragment = new Albums();
           } else if (index == 1) {
           fragment = new ArtistFragment();
           } else if (index == 2) {
            fragment = new TracksFragment();
           } else if (index == 3) {
           fragment = new GenreFragment();
          }
         return fragment;
        }
    
        @Override
        public int getCount() {
        // TODO Auto-generated method stub
        return TITLES.length;
        }
       }
    
    0 讨论(0)
  • 2021-01-14 16:02

    Have you try to replace extends Fragment by extends android.support.v4.app.Fragment ?

    When you do this, getFragmentManager() refers to android.support.v4.app.FragmentManager.

    0 讨论(0)
  • 2021-01-14 16:08

    Android does not support fragments inside of fragments. Either switch your ViewPager to use a PagerAdapter that does not use fragments, or do not put the ViewPager in a fragment.

    EDIT:

    Now, Android supports fragments inside fragments by using Fragment.getChildFragmentManager() . Have a look at this answer.

    0 讨论(0)
提交回复
热议问题