ViewPage2使用及坑点解决
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ViewPage2使用及坑点解决相关的知识,希望对你有一定的参考价值。
参考技术A 一.ViewPage2添加新特性:* 从右到左的布局支持
* 垂直方向
* RecyclerView.Adapter取代PagerAdapter
* registerOnPageChangeCallback 取代 addPageChangeListener
* 更高效的notifyDataSetChanged
二.以下案例是使用RecyclerView结合ViewPage2实现上下滑动
1.依赖导入
//ViewPage2
implementation 'androidx.viewpager2:viewpager2:1.0.0-alpha01'
//recyclerview
implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01'
2.Activity和Adapter布局设置
1>.Activity布局
<?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"
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/viewpager2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
2>.recycleview_item.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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
//原生控件
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="32sp"
tools:text="item" />
</RelativeLayout>
3.ViewPage2的适配器
package com.wd.viewpage2demo;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.List;
import androidx.viewpager2.widget.ViewPager2;
/**
* Author : 张自力
* Created on time.
*
* ViewPage2的适配器
*
*/
public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewHolder>
private List<String> mData;
private LayoutInflater mInflater;
private ViewPager2 viewPager2;
//定义一个色彩背景数组
private int[] colorArray = new int[]android.R.color.black, android.R.color.holo_blue_dark,
android.R.color.holo_green_dark, android.R.color.holo_red_dark;
public ViewPagerAdapter(Context context, List<String> data, ViewPager2 viewPager2)
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.viewPager2 = viewPager2;
@NonNull
@Override
public ViewPagerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = mInflater.inflate(R.layout.recycleview_item, parent, false);
return new ViewHolder(view);
@Override
public void onBindViewHolder(@NonNull ViewPagerAdapter.ViewHolder holder, int position)
String animal = mData.get(position);
holder.myTextView.setText(animal);
holder.relativeLayout.setBackgroundResource(colorArray[position]);
@Override
public int getItemCount()
return mData.size();
public static class ViewHolder extends RecyclerView.ViewHolder
TextView myTextView;
RelativeLayout relativeLayout;
ViewHolder(View itemView)
super(itemView);
myTextView = itemView.findViewById(R.id.tvTitle);
relativeLayout = itemView.findViewById(R.id.container);
4.Activity界面编写
package com.wd.viewpage2demo;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
import androidx.viewpager2.widget.ViewPager2;
public class MainActivity extends AppCompatActivity
private ViewPager2 viewpager2;
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//初始化数据
initDatas();
//ViewPage2设置
setViewPage2S();
/**
* ViewPage2设置
*
* */
private void setViewPage2S()
/**
* 垂直方向属性
* 默认是水平方向ORIENTATION_HORIZONTAL,垂直是ORIENTATION_VERTICAL
*
* */
//设置方向
viewpager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
//设置adapter
viewpager2.setAdapter(new ViewPagerAdapter(this, list, viewpager2));
viewpager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback()
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
@Override
public void onPageSelected(int position)
super.onPageSelected(position);
@Override
public void onPageScrollStateChanged(int state)
super.onPageScrollStateChanged(state);
);
/**
* 初始化数据
*
* */
private void initDatas()
list = new ArrayList<>();
list.add("页面一");
list.add("页面二");
list.add("页面三");
list.add("页面四");
/**
* 初始化控件
*
* */
private void initView()
//初始化ViewPage2
viewpager2 = (ViewPager2) findViewById(R.id.viewpager2);
三.RecyclerView和ViewPage2结合使用出现的坑点及解决方案:
1.将build tools更新到3.2.0,gradle更新到4.6,依赖库统一更新到28.0.0,这一步非常重要,否则会导致第二步不能完全转换成功(如果已经更新过了,可以忽略这一步)
2.选择工程右键→Refactor→Migrate to Androidx:
https://img-blog.csdnimg.cn/20190311164212477.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mjc5NzA0OA==,size_16,color_FFFFFF,t_70
3.选中所有需要重命名的目录,执行Do Refactor:
https://img-blog.csdnimg.cn/2019031116422580.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mjc5NzA0OA==,size_16,color_FFFFFF,t_70
Numpy解决:关于 dtype=object 的含义及坑点
0.直接上两段代码:
C o d e 1 : Code1: Code1:
import numpy as np
a = []
e = 0.3
a.append(['s1', 's2', 's3', float(e)])
a = np.array(a)
print(type(a[0, 3]))
输出结果为:
输出结果为:
输出结果为:
<class 'numpy.str_'>
C o d e 2 : Code2: Code2:
import numpy as np
a = []
e = 0.3
a.append(['s1', 's2', 's3', float(e)])
a = np.array(a,dtype=object)
print(type(a[0, 3]))
输出结果为:
输出结果为:
输出结果为:
<class 'float'>
1.解释:
n
u
m
p
y
numpy
numpy数组存储为连续的内存块。它们通常有单一的数据类型(例如整数、浮点数或固定长度的字符串),然后内存中的位被解释为具有该数据类型的值。
创建
d
t
y
p
e
=
o
b
j
e
c
t
dtype=object
dtype=object的数组是不同的。数组占用的内存现在充满了存储在内存其他地方的
P
y
t
h
o
n
Python
Python对象的指针(很像
P
y
t
h
o
n
Python
Python列表实际上只是对象指针的列表,而不是对象本身)。
2.文档原话:
n
u
m
p
y
numpy
numpy arrays are stored as contiguous blocks of memory. They usually have a single datatype (e.g. integers, floats or fixed-length strings) and then the bits in memory are interpreted as values with that datatype.
Creating an array with
d
t
y
p
e
=
o
b
j
e
c
t
dtype=object
dtype=object is different. The memory taken by the array now is filled with pointers to
P
y
t
h
o
n
Python
Python objects which are being stored elsewhere in memory (much like a
P
y
t
h
o
n
Python
Python list is really just a list of pointers to objects, not the objects themselves).
3.存在的坑点:
如
C
o
d
e
1
Code1
Code1所示,如果多
a
p
p
e
n
d
append
append几行后要对
n
u
m
p
y
numpy
numpy矩阵按照第三列排序时使用np.argsort()
函数时
p
y
t
h
o
n
python
python会默认按照str
类型的字典序排序,影响最终排序结果!!!!且你如果不懂的话很难发现!!
22.08.04
22.08.04
22.08.04
♡
H
a
p
p
y
C
h
i
n
e
s
e
V
a
l
e
n
t
i
n
e
′
s
D
a
y
c
h
u
i
c
h
u
i
♡
\\hearts Happy\\ Chinese\\ Valentine's\\ Day_\\ chui\\ chui\\hearts
♡Happy Chinese Valentine′s Day chui chui♡
以上是关于ViewPage2使用及坑点解决的主要内容,如果未能解决你的问题,请参考以下文章