use page.getTag() in viewpager2 PageTransformer

548 views Asked by At

I have a ViewPager2 with 3 pages and i need to transform specifically the number 2.

I am using Fragments but in the ViewPager2's PageTransformer, we override the transformPage(View page, int position) method.

So what i have tried is to to something like :

@Override  
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       Log.d(TAG, "onCreateView: creating the view.");
       // Inflate the layout for this fragment
       View view = inflater.inflate(R.layout.fragment_edit, container, false);
       view.setTag(2);
       //...
       return view;

and in the pageTransformer :

 @Override
 public void transformPage(@NonNull View page, float position) {
        System.out.println("transformPage " + page.getTag());
        if (page.getTag() != null) {
             // do something 
        }
  }

problem is that page.getTag() is always null. I have also tried to set the tag at the root view in my Fragment's XML file and still not working.

Thanks

1

There are 1 answers

0
Leon Wu On

You can call (page.parent as RecyclerView).getChildLayoutPosition(page) to get the index of the page.