Using FragmentStatePagerAdapter JazzyViewPager transitions wont work

532 Views Asked by At

The problem is that the transitions wont work but all over the app is working fine. It's just the transition effects that aren't working. I've already tried all of them but no luck. There are no errors in the codes too I don't know what I've done wrong so please help. This also my first app so please be kind. :) I didn't include the instantiateitem on pageradapter because if I do there will be a nullpointerexception. :/

activity_main xml

<com.jfeinstein.jazzyviewpager.JazzyViewPager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app1="http://schemas.android.com/apk/res/com.eight.yamjay" android:id="@+id/jazzy_pager" app1:style="cubeout" android:layout_width="match_parent" android:layout_height="match_parent" />

Main Activity:

public class MainActivity extends FragmentActivity {

//private static FragmentAdapter mAdapter;
private JazzyViewPager mJazzy;


  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setupJazziness(TransitionEffect.CubeIn);

}

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Toggle Fade");
    String[] effects = this.getResources().getStringArray(R.array.jazzy_effects);
    for (String effect : effects)
        menu.add(effect);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getTitle().toString().equals("Toggle Fade")) {
        mJazzy.setFadeEnabled(!mJazzy.getFadeEnabled());
    } else {
        TransitionEffect effect = TransitionEffect.valueOf(item.getTitle().toString());
        setupJazziness(effect);
    }
    return true;
}

  private void setupJazziness(TransitionEffect effect) {
      mJazzy = (JazzyViewPager) findViewById(R.id.jazzy_pager);
      mJazzy.setTransitionEffect(effect);
      mJazzy.setAdapter(new FragmentAdapter(getSupportFragmentManager()));
      mJazzy.setPageMargin(30);
  }   

}

FragmentStatePagerAdapter:

public class FragmentAdapter extends FragmentStatePagerAdapter{

public FragmentAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}   

@Override
public Fragment getItem(int position) 
{
    // TODO Auto-generated method stub
    Fragment fragment = new Fragment1();
    switch(position){
    case 0:
        fragment = new Fragment1();
        break;
    case 1:
        fragment = new Fragment2();
        break;
    case 2:
        fragment = new Fragment3();
        break;
    }
    return fragment;
}


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 3;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    if(object != null){
        return ((Fragment)object).getView() == view;
    }else{
        return false;
    }
}

}

Fragment1:

public class Fragment1 extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment1_layout, null);
    return v;


}

}

2

There are 2 best solutions below

0
On

Instead of calling the ScreenSlidePagerAdapter class you just have to call MainAdapter class and override its methods. Its not a huge change. If you still want i can give you the entire code and how the MainAdapter class is being called. I was also using ScreenSlidePagerAdapter but then i changed to JazzyViewPager and now i am populating the view pager using the MainAdapter class. Instead of passing the object as shown in the example you can also pass the rootview according to the position of the slider.

0
On

You have to use instantiateItem cause its author said that. You can use this code sample and you are okey to go.

   @Override
public Object instantiateItem(ViewGroup container, final int position) {
    Object obj = super.instantiateItem(container, position);
    mJazzy.setObjectForPosition(obj, position);
    return obj;
}

Please check the link.