布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
tools:ignore="MissingConstraints">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#66000000"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="5dp"
android:text="112212131313213"
android:textColor="#fff"
android:textSize="18dp" />
<LinearLayout
android:id="@+id/dot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:orientation="horizontal"/>
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
private ViewPager vp;
private TextView tv;
private LinearLayout dot;
private int[] imgId;
private ArrayList<ImageView> list;
private View point;
private String[] text;
private int lastDot = 0;
private int Max = Integer.MAX_VALUE;
private boolean isrunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initApater();
//无限循环
new Thread(new Runnable() {
@Override
public void run() {
try {
isrunning = true;
while (isrunning){
Thread.sleep(2000);
runOnUiThread(new Runnable() {
@Override
public void run() {
//滑动到下一个页面
vp.setCurrentItem(vp.getCurrentItem()+1);
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
private void initData() {
//图片数组
imgId = new int[]{R.drawable.q, R.drawable.w, R.drawable.e, R.drawable.r, R.drawable.t};
//图片集合
list = new ArrayList<>();
//文本数组
text = new String[]{"1", "2", "3", "4", "5"};
LinearLayout.LayoutParams params;
ImageView imageView;
for (int i = 0; i < imgId.length; i++) {
//添加图片
imageView = new ImageView(this);
imageView.setBackgroundResource(imgId[i]);
list.add(imageView);
//圆形点
point = new View(this);
point.setBackgroundResource(R.drawable.selector_bg_point);
//设置小白点的宽高
params = new LinearLayout.LayoutParams(20, 20);
//除去第一个都添加左边距
if (i != 0)
params.leftMargin = 10;
//默认为灰色,false
point.setEnabled(false);
//在线性布局里面添加5个小白点
dot.addView(point, params);
}
}
private void initApater() {
//第一个小白点设置为true
dot.getChildAt(0).setEnabled(true);
//第一个文字
tv.setText(text[0]);
vp.setAdapter(new MyAdapter());
//向左无限滑动思路:设置到Max中间,第一张图片
vp.setCurrentItem(Max/2-(Max/2%list.size()));
}
private void initView() {
vp = findViewById(R.id.vp);
tv = findViewById(R.id.tv);
dot = findViewById(R.id.dot);
//viewpager添加监听
vp.addOnPageChangeListener(this);
}
@Override
public void onPageScrolled(int i, float v, int i1) {
//滚动时调用
}
@Override
public void onPageSelected(int i) {
//向右无限滑动思路,当i=5,10等,变回0,所以取余
int newposition = i%imgId.length;
//新的条目被选中时调用
//设置当前文字
tv.setText(text[newposition]);
//进来把上一个小白点设置为灰色
dot.getChildAt(lastDot).setEnabled(false);
//设置当前小白点
dot.getChildAt(newposition).setEnabled(true);
//记录上一个小白点
lastDot = newposition;
}
@Override
public void onPageScrollStateChanged(int i) {
//滚动状态时调用
}
class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return Max;
}
//判断能否复用,一般写法
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
//创建条目
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
int newposition = position%imgId.length;
ImageView imageView = list.get(newposition);
container.addView(imageView);
return imageView;
}
//销毁条目
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//因为开启了一个无限循环,不设置为false,会在后台一直循环
isrunning = false;
}
}
selector_bg_point.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:drawable="@drawable/shape_bg_point_enable"/>
<item android:state_enabled="false" android:drawable="@drawable/shape_bg_point_disable"/>
</selector>
shape_bg_point_enable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<corners android:radius="5dp" />
<solid android:color="#FFFFFF" />
</shape>
shape_bg_point_disable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<corners android:radius="5dp" />
<solid android:color="@android:color/darker_gray" />
</shape>
网友评论