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

我将 PagerAdapter 用于 ViewPager Adapter,没有任何片段,只有一个选项卡布局,这很好,我可以把我的数据和......但我有一个滑动问题......它是颠倒的......我想要正确的向左但从左向右滑动...


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;

  }


湖上湖
浏览 142回答 2
2回答

ibeautiful

试试这个步骤 1. 更改视图寻呼机的旋转,viewpager.setRotationY(180);Step 2. 然后再次改变作为viewpager的子元素的fragment容器的方向,view_pager_fragment_parent_viewgroup.setRotationY(180);

holdtom

您也可以尝试添加android:layoutDirection="rtl"到您的ViewPager:<androidx.viewpager.widget.ViewPager&nbsp; &nbsp; android:id="@+id/viewpager"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:layoutDirection="rtl"&nbsp; &nbsp; app:layout_behavior="@string/appbar_scrolling_view_behavior"/>并将当前页面设置为最后一个以防万一:viewPagerAdapter = new ViewPagerAdapter(this, personItems);viewPager.setAdapter(viewPagerAdapter);viewPager.setCurrentItem(viewPagerAdapter.getCount() - 1); // instead of 0tabLayout.setupWithViewPager(viewPager);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java