I am unable to understand properly what these functions are used for in my FragmentActivity class.
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int pos) {
// on changing the page
// make respected tab selected
position = pos;
actionBar.setSelectedNavigationItem(pos);
new Getquestions().execute();
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
I am using a view pager for page sliding, and what I want to do is, if a condition is false, for example I have three edit texts in fragment one, and if a user wants to slide to fragment two without filling up the texts then the must not slide.
I tried to find any such example but I am unable to find it. Can anyone help? Understanding these functions will help me implement my thing I guess
I have got an answer from another forum
public class CustomViewPager extends ViewPager {
private boolean isPagingEnabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.isPagingEnabled = true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (this.isPagingEnabled) {
return super.onTouchEvent(event);
}
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.isPagingEnabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean b) {
this.isPagingEnabled = b;
}
}
Now when you want to disable swiping just setPagingEnable = false;
and in the layout file replace any <com.android.support.V4.ViewPager> tags with <com.yourpackage.CustomViewPager> tags.
Documented advantage of fragments, is that they can be added or removed to the activity on a run-time.
What you want is have a single fragment initially, and once the requirements are fulfilled - add a next fragment to your activity.
Related
So I am trying to have a screen with three fragments in different tabs, and on one of them have them switch to be another fragment, but I am unable to figure out how I could do this. I have looked at other similar questions, however, I cannot seem to get them working. So if someone could help me exactly figure it out that would be great.
My tabsadapter is this
public class Tabsadapter extends FragmentStatePagerAdapter {
private int TOTAL_TABS = 3;
public Tabsadapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new fragment1();
case 1:
return new fragment2();
case 2:
return new fragment3();
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return TOTAL_TABS;
}
}
and my main is this:
public class main extends ActionBarActivity implements android.support.v7.app.ActionBar.TabListener {
private ViewPager tabsviewPager;
private ActionBar mActionBar;
private Tabsadapter mTabsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabsviewPager = (ViewPager) findViewById(R.id.tabspager);
mTabsAdapter = new Tabsadapter(getSupportFragmentManager());
tabsviewPager.setAdapter(mTabsAdapter);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab1 = getSupportActionBar().newTab().setText("Month").setTabListener(this);
Tab tab2 = getSupportActionBar().newTab().setText("Week").setTabListener(this);
Tab tab3 = getSupportActionBar().newTab().setText("Day").setTabListener(this);
getSupportActionBar().addTab(tab2);
getSupportActionBar().addTab(tab2);
getSupportActionBar().addTab(tab3);
tabsviewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
getSupportActionBar().setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
}
#Override
public void onTabSelected(Tab selectedtab, FragmentTransaction arg1) {
tabsviewPager.setCurrentItem(selectedtab.getPosition()); //update tab position on tap
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
}
}
Any help anyone can provide would be greatly appreciated. Thanks!
On Android Studio, you can create automatically the code for this :
Right click on a folder of your project, choose New -> Tabbed Activity.
Choose the navigation style. (ActionBarTabs if I understood you well)
Change other fields if needed
Click finish
You now have a brand new Tabbed Activity to use :)
You can read the generated code if you want to understand how it works.
Please ask me if you need any help.
i have two custom button Add and Remove, And when i click on Add tab Work good, after when i remove tab its go ok, but when i remove last tab then i get following error.
java.lang.NullPointerException
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3699)
at android.view.ViewGroup.removeViewAt(ViewGroup.java:3663)
at app.burhanimumineenbrowser.HomeActivity$3.onClick(HomeActivity.java:184)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4963)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
When i remove first tab its del , second one also delete but after third tab i cant remove it, error say me null pointer.
Here is Add Button Code.
btNewtab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
COUNT_TAB+=1;
// viewPager.setAdapter(mAdapter);
actionBar.addTab(actionBar.newTab().setText("New Tabs")
.setTabListener(HomeActivity.this));
mAdapter.notifyDataSetChanged();
}
});
Remove button Code.
btCloseTab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(COUNT_TAB>0) {
if(TAB_CURRENT>=0 && PAGE_CURRENT>=0) {
System.out.println("CLOSE PAGE AND TAB : "+TAB_CURRENT+" AND "+PAGE_CURRENT);
COUNT_TAB -=1;
// int ichektab=TAB_CURRENT-1;
int ip=viewPager.getCurrentItem();
viewPager.removeViewAt(ip);
actionBar.removeTabAt(TAB_CURRENT);
//actionBar.removeTab(iTab);
mAdapter.notifyDataSetChanged();
// viewPager.destroyDrawingCache();
/* if(ichektab>0){
// viewPager.setCurrentItem(ichektab);
viewPager.setCurrentItem(ichektab);
//actionBar.removeTabAt(TAB_CURRENT);
}*/
}
}
}
});
My Adapter
public class TabsPageAdapter extends FragmentStatePagerAdapter {
private long baseId = 0;
public TabsPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return new MyWebBrowser();
}
#Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
#Override
public int getCount() {
return COUNT_TAB;
}
}
} please kindly suggest me about this error. Thanks
Use this code in remove button:
before removing tab u making COUNT_TAB to reduce so it troughs NullPointerException
also in addtab button increment the count tab in last
btCloseTab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(COUNT_TAB>0) {
if(TAB_CURRENT>=0 && PAGE_CURRENT>=0) {
System.out.println("CLOSE PAGE AND TAB : "+TAB_CURRENT+" AND "+PAGE_CURRENT);
// int ichektab=TAB_CURRENT-1;
int ip=viewPager.getCurrentItem();
viewPager.removeViewAt(ip);
actionBar.removeTabAt(TAB_CURRENT);
//actionBar.removeTab(iTab);
mAdapter.notifyDataSetChanged();
COUNT_TAB -=1;
// viewPager.destroyDrawingCache();
/* if(ichektab>0){
// viewPager.setCurrentItem(ichektab);
viewPager.setCurrentItem(ichektab);
//actionBar.removeTabAt(TAB_CURRENT);
}*/
}
}
}
});
View Pager page change listener
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
#Override
public void onPageScrolled(int i, float v, int i2) {
NUM_TABS=i+1;
btTabCount.setText(""+NUM_TABS);
}
#Override
public void onPageSelected(int i) {
getActionBar().setSelectedNavigationItem(i);
PAGE_CURRENT=i;
System.out.println("CURRENT PAGE DISCRIPTION : "+PAGE_CURRENT);
mAdapter.notifyDataSetChanged();
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
Tab listener methods
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mAdapter.notifyDataSetChanged();
viewPager.setCurrentItem(tab.getPosition());
TAB_CURRENT=tab.getPosition();
iTab=tab;
System.out.println("CURRENT TAB DISCRIPTION : "+TAB_CURRENT);
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
//viewPager.setCurrentItem(tab.getPosition());
}
Finally i solve error.
Th reason is viewpager pager screen limit. By default there viewpager have Two page limit, but i write first time i used this code
viewPager.setOffscreenPageLimit(COUNT_TAB);
And i solved it
viewPager.setOffscreenPageLimit(500);
and if use COUNT_TAB then add button code is
btNewtab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// COUNT_TAB+=1;
// viewPager.setAdapter(mAdapter);
actionBar.addTab(actionBar.newTab().setText("New Tabs")
.setTabListener(HomeActivity.this));
COUNT_TAB+=1;
mAdapter.notifyDataSetChanged();
viewPager.setOffscreenPageLimit(COUNT_TAB);
}
});
Put pager limit after notifydatasetchanged.
Hope you guys understand.
In my app, I am using FragmentStatePagerAdapter with a ViewPager.
All is working well except that when I am coming from background and the memory was cleaned.
In that case, some of the fragments have disappeared. The disappeared fragments are not visible but do take the hole screen. I can't see anything.
What could cause this?
Thanks!
Possible solution, given the poor problem definition:
The Fragment is being redisplayed after the Activity was recreated. Therefore, the widgets in the view are linked to the previous Activity, and you are not correctly re-inflating the view and re-wiring the widgets (redoing the findByID() commands) in onCreateView().
Any time the Activity is recreated, all Fragments must redo these things in onCreateView().
In onCreate try setting setRetainInstance(true); in the Fragments.
I'm using this code... test this on another file (TestViewPager.java maybe) to see if it work and maybe adapt to your code.
For me... it works like magic :P but... I'm newbie on Android, so maybe you need another code solution (sorry I speak spanish :P)
public class Home_ViewPager extends ActionBarActivity
implements ActionBar.TabListener {
funcionPagerAdapter miPagerAdapter;
ViewPager miViewPager;
public void onCreate(Bundle EstadoInstanciaSalvada){
super.onCreate(EstadoInstanciaSalvada);
setContentView(R.layout.home_viewpager);
miPagerAdapter = new funcionPagerAdapter(getSupportFragmentManager());
miViewPager = (ViewPager) findViewById(R.id.vpContenedor);
miViewPager.setAdapter(miPagerAdapter);
final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Set a Tab when a Fragment is selected
miViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.addTab(actionBar.newTab().setText(R.string.tab1_title).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.tab2_title).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.tab3_title).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.tab4_title).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.tab5_title).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.tab6_title).setTabListener(this));
miViewPager.setCurrentItem(1);
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
}
#Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
miViewPager.setCurrentItem(arg0.getPosition());
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
}
public static class funcionPagerAdapter extends FragmentPagerAdapter{
public funcionPagerAdapter(FragmentManager fm){
super(fm);
}
#Override
public Fragment getItem(int itemCapturado) {
switch(itemCapturado){
case 0: return new fragment_1();
case 1: return new fragment_2();
case 2: return new fragment_3();
case 3: return new fragment_4();
case 4: return new fragment_5();
case 5: return new fragment_6();
default:
Fragment miFragmento = new fragment_1();
return miFragmento;
}
}
#Override
public int getCount() {
return 6;
}
}
public static class fragment_1 extends ListFragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] arrayDeValores = new String[] { "yourItem1", "yourItem2", "yourItem3", "yourItem4", "yourItem5", "yourItem6"};
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, arrayDeValores));
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something with the data
}
}
//Others Fragments Here...
}
I'm using a viewpager to swipe between fragments and would like the back button to navigate to the previously viewed fragment rather than ending the activity. Sorry if this is a duplicate of this question however I didn't find the answer very helpful. Obviously onBackPressed needs to be overridden, but I don't know how to get and display the correct fragment. I assume that I should use the fragmentmanager's backstack, but getSupportFragmentManager().getBackStackEntryCount() always returns 0. Do I need to manually add fragments to the backstack using FragmentTransaction.addToBackStack()? If so, where would I add this in my adapter?
Here is the code for my activity,
public class PagerActivity extends FragmentActivity {
ArrayList<Sale> sales;
MyAdapter mAdapter;
ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent it = getIntent();
this.sales = (ArrayList<Sale>) it.getExtras().get("sales");
int position = it.getExtras().getInt("position");
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.fragment_pager);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(position);
}
public class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public int getCount() {
return sales.size();
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
#Override
public Fragment getItem(int position) {
SalesThumbFragment frag = new SalesThumbFragment();
return frag.newInstance(sales.get(position));
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sales_controller, menu);
return true;
}
#Override
public void onBackPressed() {
if(getSupportFragmentManager().getBackStackEntryCount() != 0) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
}
In new design support library, i use this
I have same issue and i follow this step
In the main activity where there are 3 fragment in viewpager i create stack
and push and pop data.
//private Stack<Integer> stackkk; ==> As i get edit suggestion
private Stack<Integer> stackkk = new Stack<>(); // Edited
private ViewPager mPager;
private int tabPosition = 0;
mTabLayout.setupWithViewPager(mPager);
mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
tabPosition = tab.getPosition();
mPager.setCurrentItem(tab.getPosition());
if (stackkk.empty())
stackkk.push(0);
if (stackkk.contains(tabPosition)) {
stackkk.remove(stackkk.indexOf(tabPosition));
stackkk.push(tabPosition);
} else {
stackkk.push(tabPosition);
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
tabPositionUnselected = tab.getPosition();
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
and in the onBackPressed in activity,
#Override
public void onBackPressed() {
if (stackkk.size() > 1) {
stackkk.pop();
mPager.setCurrentItem(stackkk.lastElement());
} else {
}
}
Use this code in Fragment Activity class. Don't forget to add return true;
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
mViewPager.setCurrentItem(viewPageSelected - 1);
return true;
}
return super.onKeyDown(keyCode, event);
}
I had a similar problem, this is how I solved it. I think you can adapt the code to your problem, if I understood what you problem is. I had a ViewPager with 6 fragments and wanted to keep track of the page history and to be able to use the back button to navigate backwards in the history. I create a java.util.Stack<Integer> object, add fragment numbers to it (except when you use the back button, see below), and override onBackPressed() to make it pop the last viewed fragment instead of using the back stack, when my history stack is not empty.
You want to avoid pushing elements on the Stack when you press the back button, otherwise you will get stuck between two fragments if you keep using the back button, instead of eventually exiting.
My code:
MyAdapter mAdapter;
ViewPager mPager;
Stack<Integer> pageHistory;
int currentPage;
boolean saveToHistory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.container);
mPager.setAdapter(mAdapter);
mPager.setOffscreenPageLimit(5);
pageHistory = new Stack<Integer>();
mPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
if(saveToHistory)
pageHistory.push(Integer.valueOf(currentPage));
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
saveToHistory = true;
}
#Override
public void onBackPressed() {
if(pageHistory.empty())
super.onBackPressed();
else {
saveToHistory = false;
mPager.setCurrentItem(pageHistory.pop().intValue());
saveToHistory = true;
}
};
If you use a field to keep track of the index of the previous page using mPager.getCurrentItem() after each time the user navigates to a new fragment, then in the onBackPressed() method, you should be able to call mPager.setCurrentItem(previousPage)
Or, if the user can only page in order, then you don't need a field at all, and you could just do mPager.setCurrentItem(mPager.getCurrentItem()-1)
I've made custom ViewPager and implement stack functionality in it.
public class CustomViewPager extends ViewPager {
private Stack<Integer> stack = new Stack<>();
#Override
public void setCurrentItem(int item, boolean smoothScroll) {
stack.push(getCurrentItem());
super.setCurrentItem(item, smoothScroll);
}
public int popFromBackStack(boolean smoothScroll) {
if (stack.size()>0) {
super.setCurrentItem(stack.pop(), smoothScroll);
return getCurrentItem();
} else return -1;
}
I'm developing an application that uses the new Tabs system (ActionBar.addTab()), I will have 3 tabs.
During a determined process I would like to lock (disable) two of them (not remove, only do not accept clicks and not change the 'selector'.
I was able to do not change the content (by implementing the TabListener and not changing the fragment, etc) but the selector changes.
Is there anyway to only disable/enable the tabs? without have to remove and add again?
Thanks in advance!
BR,
Danilo
Sorry, there is no "enable" or "disable" with tabs in the action bar. As you note, you can remove and re-add them -- that is the closest you can get AFAIK.
I got into the exact same issue. Finally was able to solve it. As said already, there is no way to disable/enable a tab. There is only one way out - and that is to go back to the previous tab using
getActionBar().setSelectedNavigationItem(tab);
Note however, that this should NOT be done from within the
onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction)
method, because then it gets stuck in recursive loop. So within the onTabSelected() - send a messsage to the Activity Handler to do the setSelectedNavigation().
One catch here is (you will know when you implement): what "tab" value to pass in the setSelectedNavigation. For this, we have to keep track of whether it is a user pressed tab switch or forced switch to the last tab via the code.
For this:
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
if (forcedTabSwitch)
forcedTabSwitch = false;
else
mTabToBeSwitchedTo = tab.getPosition();
}
and in the Handler,
switch (msg.what) {
case SWITCH_TO_TAB :
forcedTabSwitch = true;
break;
}
I also faced same problem.My application has three tabs and each tab has buttons to move onto next and previous tab.
I don't find any method for enabling or disabling tab.
I used DataSetter class to hold previously selected tab position and boolean value to determine tab selection.
If boolean flag is true then user has pressed next/previous button. Otherwise user is trying to select tab by pressing
them which we don't allow.
I used handler for tab selection.Inside fragment when user clicks next button I broadcast that request.
Broadcast request has integer parameter which is position of next or previous tab that we want to set.
Code inside fragment is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.first_tab_fragment, container, false);
next = (Button)rootView.findViewById(R.id.nextbutton);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("TAB_CLICKED");
Bundle bundle = new Bundle();
bundle.putInt(MainActivity.POSITION,1);
intent.putExtras(bundle);
getActivity().sendBroadcast(intent);
}
});
return rootView;
}
I have used CustomViewPager instead of ViewPager to prevent tab swiping.
CustomViewPager class is :
public class CustomViewPager extends ViewPager {
private boolean enabled;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
}
Inside oncreate method of MainActivity.java add this two lines:
viewPager = (CustomViewPager) findViewById(R.id.pager);
viewPager.setPagingEnabled(false);
Below is remaining code from MainActivity.java:
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("MainActivity","onTabSelected");
if(setter.getFlag() == true)
{
viewPager.setCurrentItem(setter.getposition());
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.d("MainActivity","onTabUnselected");
//handler.sendEmptyMessage(0);
Message message = new Message();
message.arg1 = setter.getposition();
handler.sendMessage(message);
}
Handler handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
int arg1 = msg.arg1;
Log.d("arg1",arg1+"");
//viewPager.setCurrentItem(arg1);
mActionBar.setSelectedNavigationItem(arg1);
Log.d("handler","Inside handler");
}
};
BroadcastReceiver receiver = new BroadcastReceiver(){
public void onReceive(Context context, android.content.Intent intent) {
String action = intent.getAction();
Bundle bundle = intent.getExtras();
int position = bundle.getInt(MainActivity.POSITION);
Log.d("Action",action);
Log.d("position",position+"");
setter.setposition(position);
setter.setflag(true);
Message message = new Message();
message.arg1 = position;
handler.sendMessage(message);
};
};
}
Finally DataSetter.java :
public class DataSetter
{
int position;
boolean flag;
void setposition(int value)
{
position = value;
}
void setflag(boolean value)
{
flag = value;
}
int getposition()
{
return position;
}
boolean getFlag()
{
return flag;
}
}
More you can find on: http://suhas1989.wordpress.com/2014/10/13/enable-or-disable-actionbar-tab-in-android/