astuetz / PagerSlidingTabStrip

An interactive indicator to navigate between the different pages of a ViewPager
139 stars 44 forks source link

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

Open Vic003 opened 7 years ago

Vic003 commented 7 years ago

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 commented 7 years ago

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