Android implementation Viewpager+fragment Swipe left and right

Source: Internet
Author: User

Recent news found that the news page is able to swipe left and right. So I was curious, before I did Viewpager show pictures, how to load the page? studied a bit. The discovery is joins the fragment, the nonsense does not say much, the secret mystery time has arrived.
Used Viewpager little friends know that Viewpager load data. A data source is required, here we define 4 fragment.

To facilitate the use of other colleagues, I added a base class to the 4 fragment. Detailed code such as the following:

   Public Abstract  class basefragment extends Fragment {     PublicContext Mcontext;//Context    PrivateView MView;@Override     Public void onactivitycreated(Bundle savedinstancestate) {Super. onactivitycreated (Savedinstancestate);    InitData (savedinstancestate); }@Override     Public void onCreate(Bundle savedinstancestate) {mcontext = Getactivity ();Super. OnCreate (Savedinstancestate); }@Override     PublicViewOncreateview(Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {MView = Initview (Inflater);returnMView; }/** * Initialize interface * /     Public AbstractViewInitview(Layoutinflater inflater);/** * Initialize data * /     Public Abstract void InitData(Bundle savedinstancestate);}

Inside I added 2 methods, one is the initialization interface. An initialization data so that other fragment inherit the class to implement its two methods, let's take a look at the code in one of the fragment:

public   class  weifragment  extends  basefragment  {  @Override  public  View initview  (Layoutinflater inflater) {//join page layout  view view        = Inflater.inflate (r.layout.fragment_weixin, null );    return  view; }  @Override  public  void  initdata  (Bundle savedinstancestate) {}}  

So other colleagues will know what to do with the data. On the main page. We just have to do two things. One is to give the data to Viewpager, and the other thing is to enable the following tab to control the page changes. The following is the code that passes the data:

     //加入数据     listnew ArrayList<BaseFragment>();     list.add(new WeChatFragment());     list.add(new FriendFragment());     list.add(new AddressFragment());     list.add(new SettingFragment());

Then we need to bind an adapter to Viewpager. A dedicated fragmentpageradapter is provided for the adapter,google of fragment. Detailed code such as the following:

 Public  class fragmentadapter extends fragmentpageradapter {    PrivateList<basefragment> list; Public Fragmentadapter(Fragmentmanager FM, list<basefragment> List) {Super(FM); This. list = list; }//Get Data    @Override     PublicFragmentGetItem(intPosition) {returnList.get (position); }@Override     Public int GetCount() {returnList.size (); }}

Finally we have to bind the adapter to the Viewpager;

   //绑定适配器   newlist);   mViewPager.setAdapter(adapter);

The second thing is to handle the tab change page changes. Here we set the tab number, according to the number to handle the tab display page problem, here is not affixed to the code, detailed see demo.
For example, the following:

Project Source Code
If in doubt, can give me a message. A lot of communication. Thank you!

Android implementation Viewpager+fragment Swipe left and right

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.