android - How to make a swipe view navigation? -


i have problem made right in program swipping of tabs isnt working showing pages.

main activity.java

public class mainactivity extends fragmentactivity implements tablistener { viewpager vp;  actionbar actionbar; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      vp=(viewpager)findviewbyid(r.id.pager);     vp.setadapter(new myadapter(getsupportfragmentmanager()));     actionbar=getactionbar();     actionbar.setnavigationmode(actionbar.navigation_mode_tabs);     actionbar.tab tab1=actionbar.newtab();     tab1.settext("mp3");     tab1.settablistener(this);     actionbar.tab tab2=actionbar.newtab();           tab2.settext("mp4");     tab2.settablistener(this);     actionbar.addtab(tab1);     actionbar.addtab(tab2); } @override public void ontabreselected(tab arg0, fragmenttransaction arg1) { log.d("gul",   "ontabreselected"+"postion"+arg0.getposition()+"name"+arg0.gettext()); } @override public void ontabselected(tab arg0, fragmenttransaction arg1) {     log.d("gul", "ontabselected"+"postion"+arg0.getposition()+"name"+arg0.gettext());  } @override public void ontabunselected(tab arg0, fragmenttransaction arg1) {     log.d("gul", "ontabunselected"+"postion"+arg0.getposition()+"name"+arg0.gettext());  } }   class myadapter extends fragmentpageradapter{  public myadapter(fragmentmanager fm) {     super(fm);     // todo auto-generated constructor stub }  @override public fragment getitem(int arg0) {     // todo auto-generated method stub     fragment f=null;     if(arg0==0){         f=new mp3list();     }     else{         f=new mp4list();     }     return null; }  @override public int getcount() {     // todo auto-generated method stub     return 2; } 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"  /> 

there problem know couldn't find , when clicking on tabs still not working? , mp3list , mp4list 2 fragment classes.

since google introduce android support design library this:

add below dependencies gradle

compile 'com.android.support:appcompat-v7:22.2.0'  compile 'com.android.support:design:22.2.0' 

modify mainactivity shown below:

import android.os.bundle;   import android.support.design.widget.tablayout;   import android.support.v4.app.fragment;   import android.support.v4.app.fragmentmanager;   import android.support.v4.app.fragmentpageradapter;   import android.support.v4.view.viewpager;   import android.support.v7.app.appcompatactivity;   import android.support.v7.widget.toolbar;   import android.view.menu;   import android.view.menuitem;  import java.util.arraylist;   import java.util.list;  public class mainactivity extends appcompatactivity {  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);     setsupportactionbar(toolbar);      viewpager viewpager = (viewpager) findviewbyid(r.id.viewpager);     if (viewpager != null) {         setupviewpager(viewpager);     }      tablayout tablayout = (tablayout) findviewbyid(r.id.tabs);     tablayout.setupwithviewpager(viewpager); }  private void setupviewpager(viewpager viewpager) {     // create adapter     adapter adapter = new adapter(getsupportfragmentmanager());      // add fragments adapter     adapter.addfragment(new mp3list() , "mp3");     adapter.addfragment(new mp4list() , "mp4");      // set adapter viewpager     viewpager.setadapter(adapter); }   // adapter class class adapter extends fragmentpageradapter {     private final list<fragment> mfragments = new arraylist<>();     private final list<string> mfragmenttitles = new arraylist<>();      public adapter(fragmentmanager fm) {         super(fm);     }      public void addfragment(fragment fragment, string title) {         mfragments.add(fragment);         mfragmenttitles.add(title);     }      @override     public fragment getitem(int position) {         return mfragments.get(position);     }      @override     public int getcount() {         return mfragments.size();     }      @override     public charsequence getpagetitle(int position) {         return mfragmenttitles.get(position);     } } } 

and change activity_main.xml layout this:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">  <android.support.v7.widget.toolbar     android:id="@+id/toolbar"     android:layout_width="match_parent"     android:layout_height="?attr/actionbarsize"     android:background="?attr/colorprimary"     app:popuptheme="@style/themeoverlay.appcompat.light" />  <android.support.design.widget.tablayout     android:id="@+id/tabs"     android:background="?attr/colorprimary"     android:layout_width="match_parent"     android:layout_height="wrap_content"     app:tabmode="scrollable" />     <!-- tabmode can scrollable or fixedß -->  <android.support.v4.view.viewpager     android:id="@+id/viewpager"     android:layout_width="match_parent"     android:layout_height="match_parent" />  </linearlayout>   

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -