Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tabs are indicating false index when i implemented it on Fragment insteasd of Activity #309

Open
Vic003 opened this issue Jun 9, 2017 · 1 comment

Comments

@Vic003
Copy link

Vic003 commented Jun 9, 2017

hello there,
firstly thanks for this attractive sliding tab, I implemented your codes in my college project , the slides where working beautifully. But now when i swipe on screens it is showing diffrent details means:
tab at 0 index is showing index 1.

codes are below here:

public View onCreateView(LayoutInflater inflater, @nullable ViewGroup container, @nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_enquiry, container, false);
context = getActivity();

    tabs = (PagerSlidingTabStrip)view.findViewById(R.id.tabs);
    pager = (ViewPager) view.findViewById(R.id.pager);
  
    // create our manager instance after the content view is set
    mTintManager = new SystemBarTintManager(getActivity());
    // enable status bar tint
    mTintManager.setStatusBarTintEnabled(true);
    adapter = new MyPagerAdapter(getFragmentManager());
    pager.setAdapter(adapter);
    tabs.setViewPager(pager);
    final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
            .getDisplayMetrics());
    pager.setPageMargin(pageMargin);
    pager.setCurrentItem(1);
    changeColor(ContextCompat.getColor(getActivity(), R.color.tab_grey));

    tabs.setOnTabReselectedListener(new PagerSlidingTabStrip.OnTabReselectedListener() {
        @Override
        public void onTabReselected(int position) {
            Toast.makeText(getActivity(), "Tab reselected: " + position, Toast.LENGTH_SHORT).show();

/*
Here it is showing the correct information.

*/

        }
    });
    /*tabs.setOnPageChangeListener(new PagerSlidingTabStrip.OnPageChangeListener() {
        @Override
        public void onTabReselected(int position) {

        }
    });*/


    return view;

}

private void changeColor(int newColor) {
    tabs.setBackgroundColor(newColor);
    mTintManager.setTintColor(newColor);
    Drawable colorDrawable = new ColorDrawable(newColor);
    Drawable bottomDrawable = new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.dashboard_orange));
    LayerDrawable ld = new LayerDrawable(new Drawable[]{colorDrawable, bottomDrawable});
    if (oldBackground == null) {
        //((ActionBarActivity)getActivity()).getSupportActionBar().setBackgroundDrawable(ld);
    } else {
        TransitionDrawable td = new TransitionDrawable(new Drawable[]{oldBackground, ld});
        ((ActionBarActivity)getActivity()).getSupportActionBar().setBackgroundDrawable(td);
        td.startTransition(200);
    }
    oldBackground = ld;
    currentColor = newColor;
}

public class MyPagerAdapter extends FragmentPagerAdapter {

    private final String[] TITLES = { "All", "Coming", "May Be", "Ignored","Waiting"};

    MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public Fragment getItem(int position) {
        return FragmentEnquirySlidingTab.newInstance(position);
    }
}

public class FragmentEnquirySlidingTab extends Fragment {

private static final String ARG_POSITION = "position";


ListView listView;

private int position;

public static FragmentEnquirySlidingTab newInstance(int position) {
	FragmentEnquirySlidingTab f = new FragmentEnquirySlidingTab();
	Bundle b = new Bundle();
	b.putInt(ARG_POSITION, position);
	f.setArguments(b);
	return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	position = getArguments().getInt(ARG_POSITION);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_card,container,false);
		ViewCompat.setElevation(rootView, 50);

//here it is showing diffrent values from Tab

    Toast.makeText(getActivity(),"Slider Position: "+position),Toast.LENGTH_SHORT).show();

	

	return rootView;
}

}

please help me out

@Vic003
Copy link
Author

Vic003 commented Jun 9, 2017

I tried this and it is working perfectly.

pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@OverRide
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@OverRide
public void onPageSelected(int position) {
Toast.makeText(getActivity(),"Selected :" +String.valueOf(position),Toast.LENGTH_SHORT).show();
}
@OverRide
public void onPageScrollStateChanged(int state) {
}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant