Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass some data with viewpager.setCurrentItem(position)

I want to passes some value inside button click with viewpager tab fragment changing. I have two question.

  1. what is the best way to passes data?
  2. If I use static data what will be problem? such like:

    public static string abc=""
    
    case R.id.IVActionMore:   
    ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.tabs_viewpager);
    viewPager.setCurrentItem(3);
    //abc = "action";
    break;
    
like image 609
t.gakk Avatar asked Oct 30 '22 02:10

t.gakk


2 Answers

You can use shared-preference to set and get the data. using static is not a great idea. check the link

https://stackoverflow.com/a/23024962/4549220

like image 174
Tariqul Avatar answered Nov 15 '22 04:11

Tariqul


  1. Use addOnPageChangeListener

void addOnPageChangeListener (ViewPager.OnPageChangeListener listener) Add a listener that will be invoked whenever the page changes or is incrementally scrolled.

How ?

 viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float 
                     positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) 
    {

     if(position == 3)
     {
           // DO your work
     }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});

OnClick

 buttonOBJ.setOnClickListener(new OnClickListener()
   {
             @Override
             public void onClick(View v)
             {
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
             } 
   }); 
like image 33
IntelliJ Amiya Avatar answered Nov 15 '22 06:11

IntelliJ Amiya