javaandroidtabsviewpage

Problem in swipe between tabs in Use PagerAdapter and ViewPager


I use PagerAdapter for ViewPager Adapter without any fragment and just with one layout for Tabs and It is Ok and I can Put my data and ... But I have a problem with swipe ...It is upside down ...I want right to left but Swipe left to right ...

ViewPagerAdapter.java

public class ViewPagerAdapter extends PagerAdapter {


  private static final String TAG = "ViewPagerAdapter";

  private Context context;
  private List<PersonItem> personItemList;

  public ViewPagerAdapter(Context context, List<PersonItem> personItemList) {
    this.context = context;
    this.personItemList = personItemList;
  }


  @Override
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view == object;
  }


  @Override
  public int getCount() {
    return personItemList.size();
  }


  @Override
  public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    container.removeView((View) object);
  }


  @Nullable
  @Override
  public CharSequence getPageTitle(int position) {
    return personItemList.get(position).getTabTitle();
  }

  @NonNull
  @Override
  public Object instantiateItem(@NonNull ViewGroup container, int position) {

    LayoutInflater inflater = LayoutInflater.from(context);
    ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.fragment_layout, container, false);
    PersonItem personItem = personItemList.get(position);

    ImageView imgPerson = layout.findViewById(R.id.img_person);
    TextView txtPersonContent = layout.findViewById(R.id.txt_person_content);

    if (personItem.getImageUrl() != null) {
      Glide.with(context)
        .load(personItem.getImageUrl())
        .into(imgPerson);
    } else {
      imgPerson.setVisibility(View.GONE);
    }


    if (personItem.getContent() != null) {
      txtPersonContent.setText(personItem.getContent());
    }




    container.addView(layout);

    return layout;
  }

ViewPager layout.xml

<?xml version="1.0" encoding="utf-8"?>

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">


    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/coordinator"
        tools:context=".activity.PersonalityDetailsActivity">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <include
                android:id="@+id/personality_toolbar"
                layout="@layout/personality_details_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                android:layout_marginTop="3dp"/>


            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:contextClickable="true"
                android:focusable="true"
                android:scrollbarSize="30dp"
                android:textAlignment="center"
                android:layoutDirection="rtl"
                app:tabGravity="fill"
                app:tabIndicatorColor="@color/colorAccent"
                app:tabMode="scrollable"
                app:tabPadding="3dp"
                app:tabPaddingEnd="10dp"
                app:tabSelectedTextColor="@color/colorAccent"
                app:tabTextColor="@android:color/white"/>

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

SetUp ViewPager in activity

//set adapter on View Pager
viewPagerAdapter = new ViewPagerAdapter(this, personItems);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(0);
tabLayout.setupWithViewPager(viewPager);

The OutPut is here :

The Gif link


Solution

  • Try this one

    Step 1. Change the rotation of the View pager,

    viewpager.setRotationY(180);

    Step 2. Then again change the direction of the fragment container which is the child of viewpager,

    view_pager_fragment_parent_viewgroup.setRotationY(180);

    For more info