Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change page on button click ViewPager Android

UPDATES: Logcat has been added to the question I actually want to change view ( from one fragment to other. ) on button Click i have tried many things but I am unable to do it. I have also wrote a CustomViewPager to stop changing pages on swiping the screen. it is working fine.

Here is my code:

MyFragment

 public CustomViewPager mViewPager;
 public Button next;
 public static int count=0;

 public static final MyFragment newInstance(String message,ArrayList<String> a)

 {

   MyFragment f = new MyFragment();

   Bundle bdl = new Bundle(1);

   bdl.putString(EXTRA_MESSAGE, message);
   bdl.putStringArrayList(EXTRA_ARRAY, a);

   f.setArguments(bdl);

   return f;

 }



 @Override

 public View onCreateView(LayoutInflater inflater, ViewGroup container,

   Bundle savedInstanceState) {

   String message = getArguments().getString(EXTRA_MESSAGE);
   View v = inflater.inflate(R.layout.myfragment_layout, container, false);

   spinner = (Spinner)v.findViewById(R.id.spinoptions);
   answer = (EditText)v.findViewById(R.id.answer);
   next = (Button)v.findViewById(R.id.next);

   next.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        mViewPager.setCurrentItem(1, true);//getItem(-1) for previous  

// it is not working

    }
});

   ArrayList<String> list = new ArrayList<String>();
    list.add("list 1");
    list.add("list 2");
    list.add("list 3");
    Builtprofile.q.get(0).getOptions();


        answer.setVisibility(View.INVISIBLE);
        spinner.setVisibility(View.VISIBLE);
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(Builtprofile.context,
        android.R.layout.simple_spinner_item,getArguments().getStringArrayList(EXTRA_ARRAY));
    dataAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 
       spinner.setAdapter(dataAdapter);



   TextView messageTextView = (TextView)v.findViewById(R.id.textView);


   messageTextView.setText(message);



   return v;

 }
 private int getItem() {
       return mViewPager.getCurrentItem();
}

Code for my PageAdapter

class MyPageAdapter extends FragmentPagerAdapter {

      private List<Fragment> fragments;


      public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {

        super(fm);

        this.fragments = fragments;

      }

      @Override 

      public Fragment getItem(int position) {

        return this.fragments.get(position);

      }


      @Override

      public int getCount() {

        return this.fragments.size();

      }

    }

Code for ActivityClass

  public List<Fragment> getFragments(){

      List<Fragment> fList = new ArrayList<Fragment>();

     for(i=0;i<q.size();i++)
     {
      fList.add(MyFragment.newInstance(q.get(i).getText(),q.get(i).getOptions()));

     }


      return fList;

    }

  public void fragmentopener()
  {
      List<Fragment> fragments = getFragments();

        pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);

        ViewPager pager = (ViewPager)findViewById(R.id.viewpager);

        pager.setAdapter(pageAdapter);
  }

Code for myCustomViewPager

public class CustomViewPager extends ViewPager {

    private boolean isPagingEnabled;


    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.isPagingEnabled = false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.isPagingEnabled) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.isPagingEnabled) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(boolean b) {
        this.isPagingEnabled = b;
    }
}

LogCat:

04-07 15:55:53.254: E/AndroidRuntime(17379): FATAL EXCEPTION: main
04-07 15:55:53.254: E/AndroidRuntime(17379): java.lang.NullPointerException
04-07 15:55:53.254: E/AndroidRuntime(17379):    at adapters.MyFragment$1.onClick(MyFragment.java:71)
04-07 15:55:53.254: E/AndroidRuntime(17379):    at android.view.View.performClick(View.java:3574)
04-07 15:55:53.254: E/AndroidRuntime(17379):    at android.view.View$PerformClick.run(View.java:14293)
04-07 15:55:53.254: E/AndroidRuntime(17379):    at android.os.Handler.handleCallback(Handler.java:605)
04-07 15:55:53.254: E/AndroidRuntime(17379):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 15:55:53.254: E/AndroidRuntime(17379):    at android.os.Looper.loop(Looper.java:137)
04-07 15:55:53.254: E/AndroidRuntime(17379):    at android.app.ActivityThread.main(ActivityThread.java:4448)
04-07 15:55:53.254: E/AndroidRuntime(17379):    at java.lang.reflect.Method.invokeNative(Native Method)
04-07 15:55:53.254: E/AndroidRuntime(17379):    at java.lang.reflect.Method.invoke(Method.java:511)
04-07 15:55:53.254: E/AndroidRuntime(17379):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
04-07 15:55:53.254: E/AndroidRuntime(17379):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
04-07 15:55:53.254: E/AndroidRuntime(17379):    at dalvik.system.NativeStart.main(Native Method)
like image 256
Hassaan Rabbani Avatar asked Apr 07 '14 10:04

Hassaan Rabbani


2 Answers

you get NPE on line 71 @ MyFragment, as you said in comment this line is:

 mViewPager.setCurrentItem(1, true);

so mViewPager is null, you need initialize that before using

use following code instead your code:

mViewPager.setCurrentItem(getItem(+1), true);

and getItem() is:

private int getItem(int i) {
       return mViewPager.getCurrentItem() + i;
}
like image 142
Shayan Pourvatan Avatar answered Nov 03 '22 17:11

Shayan Pourvatan


Not an Answer though, but read on

I might be late here, I found a very easy solution from an online resource:

In the fragment xml, where you are implementing the next / Previous button add this line to the button xml:

android:onClick="jumpToPage"

Place the cursor inbetween "jumpToPage" - press Alt + Enter:

enter image description here

Add the related function in your PagerActivity -- Not in the fragment

Now modify the function to this:

public void jumpToPage(View view) {

        viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
    }

Now buy me a nunchai :)

like image 39
Skynet Avatar answered Nov 03 '22 17:11

Skynet