Viewpager2的简单使用

Viewpager2的简单使用

  1. MainActivity.java
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private ViewPager2 mViewPager;

    private int[] iconsSelected = new int[]{R.drawable.item_selected_one, R.drawable.item_selected_second,
            R.drawable.item_selected_third, R.drawable.item_selected_fourth};

    private int[] iconsUnSelected = new int[]{R.drawable.item_unselected_one, R.drawable.item_unselected_second,
            R.drawable.item_unselected_third, R.drawable.item_unselected_fourth};

    private String[] iconDes = new String[]{"蓝色", "红色", "黄色", "粉色"};

    private List<Integer> mList;

    private LinearLayout colorBallList;
    private static final int mLooperCount = 500;

    private int currentPosition;

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

    private void initView() {
        mViewPager = findViewById(R.id.vp2);
        mList = new ArrayList<>();
        mList.add(R.drawable.img1);
        mList.add(R.drawable.img2);
        mList.add(R.drawable.img3);
        mList.add(R.drawable.img4);
        ViewPagerAdapter adapter = new ViewPagerAdapter(this, mList);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(mList.size() * mLooperCount / 2, false);
        mViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                Log.i(TAG, "position: " + position);
                int realPos = position % mList.size();
                currentPosition = realPos;
                Log.i(TAG, "realPos: " + realPos);
                View view = colorBallList.getChildAt(realPos);
                ImageView icon = view.findViewById(R.id.icon);
                icon.setImageResource(iconsSelected[realPos]);
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
        });
        initLayout();
    }

    private void initLayout() {
        colorBallList = findViewById(R.id.color_ball);
        for (int i = 0; i < mList.size(); i++) {
            View view = getViewAtI(i);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
                    (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(30, 0, 30, 0);
            view.setLayoutParams(layoutParams);
            colorBallList.addView(view);
        }
    }

    private View getViewAtI(int position) {
        View view = getLayoutInflater().inflate(R.layout.item_icon_layout, null, false);
        ImageView imageView = view.findViewById(R.id.icon);
        TextView textView = view.findViewById(R.id.icon_des);
        imageView.setImageResource(iconsUnSelected[position]);
        textView.setText(iconDes[position]);
        return view;
    }
}
  1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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/vp2"
        android:layout_width="250dp"
        android:layout_height="450dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp" />

    <LinearLayout
        android:id="@+id/color_ball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal"
        android:layout_marginBottom="100dp" />

</RelativeLayout>
  1. ViewPagerAdapter.java
public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.MyViewHolder> {

    private static final String TAG = "ViewPagerAdapter";
    private Context mContext;
    private List<Integer> mList;

    private static final int mLooperCount = 500;

    public ViewPagerAdapter(Context context, List<Integer> list) {
        mContext = context;
        mList = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_img, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Log.i(TAG, "position: " + position);
        int realPos = position % mList.size();
        Log.i(TAG, "realPos: " + realPos);
        holder.imageView.setImageResource(mList.get(realPos));
    }

    @Override
    public int getItemCount() {
        return getRealCount() * 500;
    }

    private int getRealCount() {
        return mList == null ? 0 : mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img);
        }
    }
}
  1. item_img.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
  1. item_icon_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" />

    <TextView
        android:id="@+id/icon_des"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/icon"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:textColor="@color/black"
        android:textSize="15sp"
        tool:text="白色" />

</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值