Android Dev
Android Dev

Reputation: 3

ViewPager not appearing within a fragment layout

Hi i have been trying to inside a viewpager into a fragment however, i have followed many tutorials and come to no avail. I am trying to create a viewpager in the middle of another fragment layout here are my codes :

Adapter :

public class CardStackAdapter extends FragmentPagerAdapter {

private Context context;

public CardStackAdapter(Context context, FragmentManager fm) {
    super(fm);
    this.context =context;
}

@Override
public Fragment getItem(int position)
{
    return CardStackFragment.newInstance(position);
}
@Override
public int getCount() {
    return 5;
}
}

Fragment that contains the ViewPager including the transformer class :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_social_people,
            container, false);
    // stack cards
    ViewPager mPager;
    PagerAdapter mAdapter;

    mPager = (ViewPager)view.findViewById(R.id.stackcardvp);
  mAdapter = new CardStackAdapter(getActivity(),getChildFragmentManager());

    mPager.setPageTransformer(true,new CardStackTransformer());

    mPager.setOffscreenPageLimit(2);

     mPager.setAdapter(mAdapter);
     //   mPager.setAdapter(buildAdapter());

    return view;
}

Transformer :

     private class CardStackTransformer implements ViewPager.PageTransformer{


    @Override
    public void transformPage(View page, float position) {
        if(position >= 0 ){


            page.setScaleX(0.0f-0.02f * position);
            page.setScaleY(0.0f);


            page.setTranslationX(-page.getWidth()+position);
            page.setTranslationY(30 + position);
        }

    }
}// private class

And The fragment for the viewpager :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_card_stack,
            container, false);

    return view;
}

public static CardStackFragment newInstance(int selectedIdForIndex) {
    CardStackFragment fragment = new CardStackFragment();

    Bundle args = new Bundle();
    args.putInt(ARG_LAYOUT_ID, selectedIdForIndex);
    fragment.setArguments(args);

    return fragment;
}

XML layout for the fragment to contain the viewpager :

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="30dp">

        <FrameLayout
            android:id="@+id/fl"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            >

        <android.support.v4.view.ViewPager
            android:id="@+id/stackcardvp"
            android:layout_width="match_parent"
            android:layout_height="320dp"
            android:layout_centerInParent="true"
            android:visibility="visible" />

        </FrameLayout>

    </LinearLayout>

The problem is as i try the different tutorials of using GetChildFragment it still does not appear, i even tried creating a build adapter but with no results. Thank you in advance for the advice.

Upvotes: 0

Views: 104

Answers (1)

Sharath kumar
Sharath kumar

Reputation: 4132

CardStackFragment.newInstance(position) 

Remove this statement.It is recurring again

EDIT:

The problem is with this line.Comment this statment and your viewpager should work

mPager.setPageTransformer(true,new CardStackTransformer());

Upon seeing your whole code I found out that this statment is going wrong and putting view outside the screen.Hope this helps you.

page.setScaleX(0.0f-0.02f * position);

Upvotes: 1

Related Questions