ViewPager的哥,ViewPager2來了

ViewPager在我們的實際工作中,用到的地方很多:比如banner圖、tab等等,與我們工作息息相關(guān)。

ViewPager這個控件我們蹂躪了這么多年,他的內(nèi)心也是憤怒的。所以它就怒吼一聲,把他的哥哥 ViewPager2官方地址 喊來了,但他萬萬沒有想到,它的哥哥逃不過我們的蹂躪,哈哈哈哈。

由于這一兩年,短視頻猶如一個點燃的彗星撞向地球,吸引了大部分人的注意力,所以對于的短視頻的需求也就多了起來。比如:抖音、快手、小紅書、微視等等。為了提高用戶體驗度,他們的視頻播放都是采用了上下滑動,切換視頻的方式。當然也有好多人使用了自定義RecyclerView或者自定義ViewPager實現(xiàn)了這個功能。但是ViewPager2是官方提供,直接自帶實現(xiàn)的。
下邊來看看我們怎么蹂躪ViewPager2。

 dependencies {
     implementation 'androidx.viewpager2:viewpager2:1.0.0-alpha06'
  }

在實際的使用遇見了一個問題,發(fā)現(xiàn) Androidx 與 Android support 庫 不共存,那就support庫轉(zhuǎn)成Androidx。
解決方法:Androidx和Android support庫共存問題解決

查看ViewPager2源碼可知,ViewPager2繼承ViewGroup,支持橫向縱向。通過設(shè)置Orientation確定方向。

    public final class ViewPager2 extends ViewGroup {
        /** @hide */
        @RestrictTo(LIBRARY_GROUP_PREFIX)
        @Retention(SOURCE)
        @IntDef({ORIENTATION_HORIZONTAL, ORIENTATION_VERTICAL})
        public @interface Orientation {
        }
      }

內(nèi)部是通過RecyclerView和LinearLayoutManager實現(xiàn)的

    LinearLayoutManager mLayoutManager;
    private int mPendingCurrentItem = NO_POSITION;
    private Parcelable mPendingAdapterState;
    private RecyclerView mRecyclerView;
    private PagerSnapHelper mPagerSnapHelper;
    private ScrollEventAdapter mScrollEventAdapter;
    private FakeDrag mFakeDragger;
    private PageTransformerAdapter mPageTransformerAdapter;

device-2019-07-12-174755.gif
device-2019-07-12-175011.gif

代碼如下:

public class MainActivity extends AppCompatActivity {

    private ViewPager2 vp;
    private MyListAdapter mListAdapter;
    private List<Integer> lists = new ArrayList<Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        for (int i = 0; i < 100; i++) {
            lists.add(Color.GRAY);
            lists.add(Color.BLUE);
            lists.add(Color.YELLOW);
            lists.add(Color.RED);
            lists.add(Color.GREEN);
            lists.add(Color.BLACK);
        }

        vp = findViewById(R.id.vp);

        //豎向滑動
        //橫向滑動
        mListAdapter = new MyListAdapter(this, lists);
        vp.setAdapter(mListAdapter);


    }
}

import java.util.List;

public class MyListAdapter extends RecyclerView.Adapter {

    Context context;
    List<Integer> list;

    MyListAdapter(Context context, List<Integer> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view_pager, parent, false);
        final MyViewHolder mViewHolder = new MyViewHolder(v);
        return mViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ((MyViewHolder) holder).mCL.setBackgroundColor(list.get(position));
        ((MyViewHolder) holder).mTvShow.setText("當前顯示 position = " + (position + 1));
        ((MyViewHolder) holder).itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ConstraintLayout mCL;
        TextView mTvShow;

        public MyViewHolder(View itemView) {
            super(itemView);
            mTvShow = (TextView) itemView.findViewById(R.id.tv_Show);
            mCL = (ConstraintLayout) itemView.findViewById(R.id.cl);
        }
    }
}

<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <!--<androidx.viewpager2.widget.ViewPager2-->
       <!--android:id="@+id/vp"-->
       <!--android:layout_width="match_parent"-->
       <!--android:layout_height="match_parent"-->
       <!--android:orientation="vertical"-->
       <!--tools:ignore="MissingConstraints" />-->

   <androidx.viewpager2.widget.ViewPager2
       android:id="@+id/vp"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="horizontal"
       tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cl"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <TextView
       android:id="@+id/tv_Show"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:gravity="center"
       android:orientation="vertical"
       android:text="aaaaaa"
       android:textColor="#ffffffff"
       tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容