Selecting tabs not working - android

currently i have tabs in my activity, i can swipe between the tabs easily.
But here's a bug:
I Can't select a tab!
Here i'm on the new tab.
If i click on upcomming i will get this
I Will need to swype in order to see the content of upcoming!
Why is this happening?
Here's is my codes:
public class Taby extends SherlockFragmentActivity {
FragmentTransaction transaction;
static ViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
Fragment tabOneFragment = new TabOne();
Fragment tabTwoFragment = new TabTwo();
PagerAdapter mPagerAdapter = new PagerAdapter(
getSupportFragmentManager());
mPagerAdapter.addFragment(tabOneFragment);
mPagerAdapter.addFragment(tabTwoFragment);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setOffscreenPageLimit(2);
mViewPager.setCurrentItem(0);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
getSupportActionBar().setSelectedNavigationItem(
position);
}
});
ActionBar ab = getSupportActionBar();
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
com.actionbarsherlock.app.ActionBar.Tab tab1 = ab
.newTab()
.setText("New")
.setTabListener(
new TabListener<TabOne>(this, "tabone", TabOne.class));
com.actionbarsherlock.app.ActionBar.Tab tab2 = ab
.newTab()
.setText("Upcoming")
.setTabListener(
new TabListener<TabTwo>(this, "tabtwo", TabTwo.class));
ab.addTab(tab1);
ab.addTab(tab2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.like, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
case R.id.facebooklike:
String facebookScheme = "fb://profile/" + "192346130896055";
Intent facebookIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(facebookScheme));
startActivity(facebookIntent);
break;
}
return true;
}
#SuppressLint("NewApi")
public static class TabListener<T extends Fragment> implements
ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(Tab arg0,
android.app.FragmentTransaction arg1) {
}
#SuppressLint("NewApi")
public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
mViewPager.setCurrentItem(arg0.getPosition());
}
public void onTabUnselected(Tab arg0,
android.app.FragmentTransaction arg1) {
}
#Override
public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabUnselected(
com.actionbarsherlock.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(
com.actionbarsherlock.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
public class PagerAdapter extends FragmentPagerAdapter {
private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
public PagerAdapter(FragmentManager manager) {
super(manager);
}
public void addFragment(Fragment fragment) {
mFragments.add(fragment);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
}
}

mViewPager.setCurrentItem(position); inside the onPageSelected() like that:
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
getSupportActionBar().setSelectedNavigationItem(
position);
mViewPager.setCurrentItem(position);
}
});

Related

How to disable/enable the ActionBar.TabListener on some condition

I have implemented ActionBar.TabListener with viewpager. Now I want to disable tab click for some condition
I have stopped swipe using TouchListner but want to do same for Tab Click.
Here is my code.
MainActivity.java
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
ViewPager viewPager;
String check ="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdpter(fragmentManager));
viewPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
if ("".equals(check)) {
return true;
}
return false;
}
});
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Tab 1");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("Tab 2");
tab2.setTabListener(this);
ActionBar.Tab tab3 = actionBar.newTab();
tab3.setText("Tab 3");
tab3.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
viewPager.setCurrentItem(2, false);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
MyAdpter.java
class MyAdpter extends FragmentPagerAdapter{
public MyAdpter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
if(position==0){
fragment = new FragmentA();
}
if(position==1){
fragment = new FragmentB();
}
if(position==2){
fragment = new FragmentC();
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
}
FragmentA.java
public class FragmentA extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a , container,false);
}
}
FragmentB/FragmentC are similar to FragmentA
activity_main.xml
<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" >
</android.support.v4.view.ViewPager>
ScreenShot
you can do something like this..
in OnCheckedChanged listener for checkbox, you can specify which tabs are enable..
just by doing
// to enable
tabLayout.getChildAt(position).setEnabled(true);
// or to disable
tabLayout.getChildAt(position).setClickable(false);.
use this property to enable or disable clicks on tab.

SherlockFragmentActivity tabs onBackPressed()

SherlockFragmentActivity with 3 tabs How can i add onBackPressed() in all tabs? It is possible? this code is default abs Viewpage totorial from http://www.androidbegin.com/tutorial/android-actionbarsherlock-viewpager-tabs-tutorial/. Any help?
MainActivity.jav
public class MainActivity extends SherlockFragmentActivity {
private FragmentTab2 F2;
ActionBar mActionBar;
ViewPager mPager;
Tab tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
mActionBar.setSelectedNavigationItem(position);
}
};
mPager.setOnPageChangeListener(ViewPagerListener);
ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
mPager.setAdapter(viewpageradapter);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
};
tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
mActionBar.addTab(tab);
tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
mActionBar.addTab(tab);
tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
mActionBar.addTab(tab);
}
#Override
public void onBackPressed()
{
super.onBackPressed();
F2.onBackPressed();
}
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
case 2:
FragmentTab3 fragmenttab3 = new FragmentTab3();
return fragmenttab3;
}
return null;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
}
FragmentTab2.java
public class FragmentTab2 extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmenttab2, container, false);
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
public boolean onBackPressed() {
Toast.makeText(getActivity(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
return false;
}
}
Error
Exception dispatching finished signal.
Exception in MessageQueue callback: handleReceiveCallback
java.lang.NullPointerException
at com.androidbegin.absviewpagertutorial.MainActivity.onBackPressed(MainActivity.java:87)
at android.app.Activity.onKeyUp(Activity.java:2193)
at android.view.KeyEvent.dispatch(KeyEvent.java:2664)
at android.app.Activity.dispatchKeyEvent(Activity.java:2423)
at com.actionbarsherlock.app.SherlockFragmentActivity.dispatchKeyEvent(SherlockFragmentActivity.java:121)
Use onPageSelected to check if user is on FragmentTab2 then initialize F2 as:
public void onPageSelected(int position) {
super.onPageSelected(position);
mActionBar.setSelectedNavigationItem(position);
if(position==1)
F2 = (FragmentTab2)viewpageradapter.getItem(position);
else
F2 =null;
}
and also put null check in onBackPressed of Activity :
#Override
public void onBackPressed()
{
super.onBackPressed();
if(F2 !=null)
F2.onBackPressed();
}

About Fragment cyclelife and viewpager

My App is a fragment have viewpager to change tab with fragmentA and fragmentB.
After I change another fragment by drawlayout.
I back to the first fragment.
The fragmentA and fragmentB doesn't link with the viewpager.
I think it's about Fragment cyclelife questions.
But I can't figure out the answer.
Please help me.Thanks.
(My English is not very well.Please use simple word.Thanks)
public class Fragment_Viewpager extends Fragment implements TabListener{
private View v;
ViewPager viewPager;
ActionBar actionBar;
ActionBar.Tab tab1;
ActionBar.Tab tab2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v= inflater.inflate(R.layout.fragment_viewpager, container, false);
viewPager=(ViewPager)v.findViewById(R.id.pager);
Log.w("Step1","inviewPager");
viewPager.setAdapter(new MyAdapter(getFragmentManager()));
Log.w("Step2","inviewPager");
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO 自動產生的方法 Stub
actionBar.setSelectedNavigationItem(arg0);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO 自動產生的方法 Stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO 自動產生的方法 Stub
}
});
Log.w("Step7","inviewPager");
Log.w("Step8","inviewPager");
return v;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO 自動產生的方法 Stub
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
Log.w("in","Fragment-2");
}
#Override
public Fragment getItem(int arg0) {
// TODO 自動產生的方法 Stub
Log.w("in","Fragment-1");
Fragment fragment = null;
Log.w("in","Fragment000");
if(arg0==0)
{
Log.w("in","FragmentA");
fragment = new FragmentA();
}
if(arg0==1)
{
Log.w("in","FragmentB");
fragment = new FragmentB();
}
return fragment;
}
#Override
public int getCount() {
// TODO 自動產生的方法 Stub
return 2;
}
}
#Override
public void onStop() {
super.onPause();
Log.w("in","inStop");
}
#Override
public void onPause() {
super.onPause();
getActivity().getActionBar().removeAllTabs();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Log.w("in","onPause");
onDetach();
}
#Override
public void onDestroy() {
super.onDestroy();
Log.w("in","onDestroy");
}
#Override
public void onResume() {
super.onResume();
Log.w("in","onResume");
}
#Override
public void onStart() {
Log.w("Step3","inviewPager");
actionBar = getActivity().getActionBar();
Log.w("Step4","inviewPager");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Log.w("Step5","inviewPager");
tab1=actionBar.newTab();
tab2=actionBar.newTab();
Log.w("Step6","inviewPager");
tab1.setText("Tab 1");
tab1.setTabListener(this);
tab2.setText("Tab 2");
tab2.setTabListener(this);
actionBar.addTab(tab1);
Log.w("in","onStart2");
actionBar.addTab(tab2);
super.onStart();
}
}

How to add child fragment without ActionBar and swipe to class fragment

I have Fragment MainActivity class with ActionBar and swipe which work well.
Acording to action user the class FragmentA,FragmentB,FragmentC show in the scren.
How can I add child fragment without ActionBar and without swipe from FragmentA class
public class FragmentA extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a,container,false);
}
}
so from FragmentA I will have
-- FragmentAA
-- FragmentAB
Many Thanks For any help.
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ViewPager viewPager=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.pager);
final ActionBar actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
addTabs(actionBar);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
}
#Override
public void onPageSelected(int i) {
actionBar.setSelectedNavigationItem(i);
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
}
private void addTabs(ActionBar actionBar)
{
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("Tab 1");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText("Tab 2");
tab2.setTabListener(this);
ActionBar.Tab tab3=actionBar.newTab();
tab3.setText("Tab 3");
tab3.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
class MyAdapter extends FragmentStatePagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment=null;
if(i==0)
{
fragment=new FragmentA();
}
if(i==1)
{
fragment=new FragmentB();
}
if(i==2)
{
fragment=new FragmentC();
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
}

How to get which fragment has been selected

I am creating a tab application, using fragments.
I have successfully created tabs, but now I need to create an object when a specific fragment is selected. e.g. When I select SettingsTab I need to call a function that is defined in Test class. and similarly destroy
// SettingsTab
package com.example.sensorspositioningn;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SettingsTab extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.settingstab, container, false);
}
}
Here is the code in mainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appContext = getApplicationContext();
//ActionBar
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab Tab1= actionbar.newTab().setText("Tab1");
ActionBar.Tab Tab2= actionbar.newTab().setText("Tab2");
ActionBar.Tab Tab3= actionbar.newTab().setText("Tab3");
ActionBar.Tab SettingsTab = actionbar.newTab().setText("Settings");
Fragment Tab1F= new Tab1();
Fragment Tab2F= new Tab2();
Fragment Tab3F= new Tab3();
Fragment mSettingsFragment = new SettingsTab();
Tab1.setTabListener(new MyTabsListener(Tab1F));
Tab1.setTabListener(new MyTabsListener(Tab2F));
Tab1.setTabListener(new MyTabsListener(Tab3F));
SettingsTab.setTabListener(new MyTabsListener(mSettingsFragment));
actionbar.addTab(Tab1);
actionbar.addTab(Tab2);
actionbar.addTab(Tab3);
actionbar.addTab(SettingsTab);
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
In your ActionBar.TabListener you can do this - (I'm assuming that you need the object to be in the Activity)
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
if(fragment instanceof SettingsTab) {
// Create your object, call your function
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
if(fragment instanceof SettingsTab) {
// Destroy your object
}
}
}
Try this
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
if(fragment instanceof SettingsTab){
doSettingsMethod();
}
if(fragment instanceof Tab1){
doOtherMethod1();
}
if(fragment instanceof Tab2){
doOtherMethod2();
}
if(fragment instanceof Tab3){
doOtherMethod3();
}
}
Also you may change TabListener class in this way. As for me it's a better design.
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
private final Runnable onSelect;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
this.onSelect = null;
}
public MyTabsListener(Fragment fragment, Runnable onSelect) {
this.fragment = fragment;
this.onSelect = onSelect;
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
if(onSelect != null){
fragment.getActivity().runOnUiThread(onSelect);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
The only official source I found to this was on the developer site:
http://developer.android.com/training/implementing-navigation/lateral.html
They simply put arguments for each created fragment, and read the arguments when the fragments get selected.
By default if you select a tab it will be highlighted. If you want to select Explicitly means use the given commented code under onTabSelected(TabLayout.Tab tab) with your specified tab index position. This code will explains about change fragment on tab selected position using viewpager.
public class GalleryFragment extends Fragment implements TabLayout.OnTabSelectedListener
{
private ViewPager viewPager;public ViewPagerAdapter adapter;private TabLayout tabLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_gallery, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new PaymentCardFragment(), "PAYMENT CARDS");
adapter.addFragment(new LoyaltyCardFragment(), "LOYALTY CARDS");
viewPager.setAdapter(adapter);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(this);
}
#Override
public void onTabSelected(TabLayout.Tab tab) {
//This will be called 2nd when you select a tab or swipe using viewpager
final int position = tab.getPosition();
Log.i("card", "Tablayout pos: " + position);
//TabLayout.Tab tabdata=tabLayout.getTabAt(position);
//tabdata.select();
tabLayout.post(new Runnable() {
#Override
public void run() {
if (position == 0) {
PaymentCardFragment paymentCardFragment = getPaymentCardFragment();
if (paymentCardFragment != null) {
VerticalViewpager vp = paymentCardFragment.mypager;
if(vp!=null)
{
//vp.setCurrentItem(position,true);
vp.setCurrentItem(vp.getAdapter().getCount()-1,true);
}
}
}
if (position == 1) {
LoyaltyCardFragment loyaltyCardFragment = getLoyaltyCardFragment();
if (loyaltyCardFragment != null) {
VerticalViewpager vp = loyaltyCardFragment.mypager;
if(vp!=null)
{
vp.setCurrentItem(position);
}
}
}
}
});
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
//This will be called 1st when you select a tab or swipe using viewpager
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
//This will be called only when you select the already selected tab(Ex: selecting 3rd tab again and again)
}
private PaymentCardFragment getLoyaltyCardFragment() {
Fragment f = adapter.mFragmentList.get(viewPager.getCurrentItem());
if(f instanceof PaymentCardFragment)
{
return (PaymentCardFragment) f;
}
return null;
}
private LoyaltyCardFragment getPaymentCardFragment() {
Fragment f = adapter.mFragmentList.get(viewPager.getCurrentItem());
if(f instanceof LoyaltyCardFragment)
{
return (LoyaltyCardFragment) f;
}
return null;
}
class ViewPagerAdapter extends FragmentPagerAdapter {
public List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
}
}

Categories

Resources