简体   繁体   English

在使用PagerAdapter和ViewPager中的选项卡之间滑动时出现问题

[英]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 ... 我将PagerAdapter用于ViewPager Adapter,没有任何片段,只有选项卡的一种布局,可以,可以放入数据并...但是我在滑动时遇到了问题...它倒置了...我想正确向左但从左向右滑动...

ViewPagerAdapter.java 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 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 在活动中设置ViewPager

//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 Gif链接

You could try to add android:layoutDirection="rtl" to your ViewPager too: 您也可以尝试将android:layoutDirection="rtl"ViewPager

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

And set current page to the last just in case: 并将当前页面设置为最后一个,以防万一:

viewPagerAdapter = new ViewPagerAdapter(this, personItems);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(viewPagerAdapter.getCount() - 1); // instead of 0
tabLayout.setupWithViewPager(viewPager);

Try this one 试试这个

Step 1. Change the rotation of the View pager, 步骤1.更改“查看”寻呼机的旋转方式,

viewpager.setRotationY(180);

Step 2. Then again change the direction of the fragment container which is the child of viewpager, 第2步。然后再次更改片段容器的方向,该片段容器是viewpager的子元素,

view_pager_fragment_parent_viewgroup.setRotationY(180);

For more info 欲了解更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM