how to disable viewpager adapter on touching specific views? - android

I have a viewpager which switches between tabs when swiping left/right.In my second tab, i have some custom views which have listeners for pinching and dragging but when i try to pinch or drag, the viewpager starts to swipe the page.
A solution comes to my mind is to disable swiping when touching those specific views and only swipe when touching outside those views.Is this possible?
Updated:
#Asok kindly provided the solution. But then updated the code which wouldnt work in my case so i post the previous piece of code which worked for me:
public class CustomViewPager extends ViewPager {
private boolean swipeable = true;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
// Call this method in your motion events when you want to disable or enable
// It should work as desired.
public void setSwipeable(boolean swipeable) {
this.swipeable = swipeable;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
return (this.swipeable) ? super.onInterceptTouchEvent(arg0) : false;
}
Lets suppose i have a draggable view and i need to disable swipping when dragging start and re enable when dragging finished so in TouchEvent of my so called view:
#Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
//disable swiping when the button is touched
((ActivityOriginal) getActivity()).setSwipeable(false);
//the rest of the code...
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
//re enable swipping when the touch is stopped
//the rest of the code...
((ActivityOriginal) getActivity()).setSwipeable(true);
break;
}
return true;
}

This first thing that comes to mind for me is to have a custom ViewPager in which, when your touch listeners get notified of a specific event you could set the swipeable boolean in ViewPager to false and set it back to true whichever way best fits your application.
public class CustomViewPager extends ViewPager {
private boolean swipeable = true;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
// Call this method in your motion events when you want to disable or enable
// It should work as desired.
public void setSwipeable(boolean swipeable) {
this.swipeable = swipeable;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
return (this.swipeable) ? super.onInterceptTouchEvent(arg0) : false;
}
}
Make sure to change your layout file to show:
<com.your.package.CustomViewPager .. />
Instead of:
<android.support.v4.view.ViewPager .. />
Edit 2
Here is my setup (Working with the above CustomViewPager):
CustomViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the CustomViewPager with the sections adapter.
mViewPager = (CustomViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
public void swipeOn(View v) {
mViewPager.setSwipeable(true);
}
public void swipeOff(View v) {
mViewPager.setSwipeable(false);
}
The above shown onCreate is in my MainActivity class which extends FragmentActivity and implements ActionBar.TabListener

I am using requestDisallowInterceptTouchEvent(true) int the onTouchEvent listener of the view that also has drag events.
#Override
public boolean onTouchEvent(MotionEvent event) {
ViewParent parent = getParent();
// or get a reference to the ViewPager and cast it to ViewParent
parent.requestDisallowInterceptTouchEvent(true);
// let this view deal with the event or
return super.onTouchEvent(event);
}

I am using like this
public void setSwipeable(boolean swipeable)
{
this.swipeable = swipeable;
}
#Override
public void scrollTo(int x, int y){
if (swipeable){
super.scrollTo(x, y);
}
}

If you know the positions on which you don't want to intercept touch events in the view pager you can do something like I did.
#Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if(arg0.getY()>getHeight()/2)return false;
return super.onInterceptTouchEvent(arg0);
}

Related

How to disable ViewPager from swiping and enable manual swipe using button click

I created a view pager that controls four fragments. But I want to disabled swiping by finger. I want to lock swipe and use button click to navigate to the next fragment or previous fragment. But all my trial codes gotten from research proved wrong. Please how do I disable swiping completely and introduce a button to enable me to navigate to the
This is SimpleFragmentPageAdapter
public class SimpleFragmentPageAdapter extends FragmentPagerAdapter {
private String[] tabtitle=new String[]{"Page1","Page2","Page3","Page4",};
Context context;
private int pagecount=4;
public SimpleFragmentPageAdapter(FragmentManager fm, Context context) {
super(fm);
this.context=context;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: //Fragment # 0 - This will show DevelopersFragment
return Page1Fragment.newInstance(0, "page # 0");
case 1: //Fragment # 0 - This will show Page1Fragment
return Page2Fragment.newInstance(1, "Page # 1");
case 2: //Fragment # 0 - This will show Page2Fragment
return Page3Fragment.newInstance(2, "Page # 2");
case 3: //Fragment # 0 - This will show Page3Fragment
return Page4Fragment.newInstance(3, "Page # 3");
default:
return null;
}
}
#Override
public int getCount() {
return pagecount;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return "woliwo" + position;
}
}
This is Main1Activity.java
public class Main1Activity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
Button Tips3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the start page passed to us or default to first page
Intent mIntent = getIntent();
int startPage = mIntent.getIntExtra("startPage", 0);
setContentView(R.layout.activity_main1);
toolbar = findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new SimpleFragmentPageAdapter(getSupportFragmentManager(), this));
// set the current page to the start page
viewPager.setCurrentItem(startPage);
//Attach the page change listener inside the activity
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
// This method will be invoked when a new page becomes selected
#Override
public void onPageSelected(int position) {
Toast.makeText(Main1Activity.this, "bool:" + position, Toast.LENGTH_SHORT).show();
}
// This method will be invoked when the current page is scrolled
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetpixels) {
}
// Called when the scroll state changes:
//SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
#Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
}
}
This is what I tried that gave 1% motion event disability
final View touchView = findViewById(R.id.view_pager);
touchView.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
return true;
}
});
This is the ViewPager class Main1Activity.java This below is how I did it and it show nothing please correct me
public class Main1Activity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
Button Tips3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
toolbar = findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
CustomViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.tool_item_menu1, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item0:
Toast.makeText(this, "Home Selected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Main1Activity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
You can use ViewPager2 as mentioned in the comment above by https://stackoverflow.com/users/7725103/rajasekhar, but if you really need to use a viewpager: then you will have to create a custom viewpager like this:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import androidx.viewpager.widget.ViewPager;
public class CustomViewPager extends ViewPager {
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
}
Usage
<com.yourpackagename.CustomViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/tab_layout"/>
Finally :
In the class you are using the viewpager:
CustomViewPager viewPager=findViewById(R.id.view_pager);
viewPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
This Solution is for those using ViewPager2
Set you Layout thus:
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/tab_layout"/>
And then set the class thus: No matter the way you code just placing this viewPager2.setUserInputEnabled(false); disables swiping effects
ViewPager2 viewPager2 = findViewById(R.id.viewPager);
viewPager2.setAdapter(new SimpleFragmentPagerAdapter(this));
viewPager2.setUserInputEnabled(false);
Goodluck!

Setting onClickListener on a ViewPager in Android

I have a ViewPager and I'm setting OnClickListener to it.
But the ViewPager is not detecting the on click. Whats wrong here ?
Code:
viewPager = (ViewPager) findViewById(R.id.pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getBaseContext(), "view Pager Clicked", Toast.LENGTH_SHORT).show();
}
});
You can use setOnClickListener inside the adapter in instantiateItem method.
it works for me.
I understand you want to identify touch events on your ViewPager, so that you can trigger an animation that hides your ActionBar. One method requires you create your own subclass of ViewPager and override onInterceptTouchEvent:
public class ABetterViewPager extends ViewPager {
public ABetterViewPager(Context context) {
super(context);
}
public ABetterViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//Enter the code here to communicate a touch event
return super.onInterceptTouchEvent(ev);
}
}
You will probably need to tailor the above to your specifications. For instance, you may only want to trigger the event once someone starts to move their finger on the screen, in which case you would use:
if (ev.getAction() == MotionEvent.ACTION_MOVE) {
//User is moving their finger over the screen
}
In respect to communicating the touch event from your ViewPager to wherever it needs to be to fire your ActionBar hiding animation (which I assume is the Activity hosting the ViewPager) then you can use an interface/EventBus.
Also, don't forget to replace the ViewPager in your xml layout file with the full name of your custom class (i.e. com.example.mywidgets.ABetterViewPager, or whatever)
ViewPager is not made for onClick. It has OnPageChangelistener as below:
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// this will execute when page will be selected
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

ViewPager for Screen Slides without swipe

I want to use a Screen slide but without swipe. I see this example: http://developer.android.com/training/animation/screen-slide.html , only i want slide pages with the top menu and not with the swipe.
How I can do it?
Thanks.
This is how I have implemented it. In this example I have a ViewPager with 3 items in the adapter and it swipes to the next item every 3 seconds. After it reaches the third item it goes back to the first position.
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
int page = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity22);
viewPager = (ViewPager) findViewById(R.id.pager);
}
private void testing() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (page == 3) {
page = 0;
}
viewPager.setCurrentItem(page++);
testing();
}
}, 3000); // 3 seconds
}
}
Disable Swipe
viewPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
You have to use "Swipe Views with Tabs". Here's how:
http://developer.android.com/training/implementing-navigation/lateral.html

Using backstack and back button in viewpager

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;
}

Enable/Disable Android ActionBar.Tab

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/

Categories

Resources