activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="200dp"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <LinearLayout android:id="@+id/dotLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" android:gravity="center" android:orientation="horizontal" /> </RelativeLayout> </RelativeLayout>
selecter.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true"> <shape android:shape="oval"> <size android:height="10dp" android:width="10dp"></size> <solid android:color="#aa000000"></solid> </shape> </item> <item android:state_enabled="false"> <shape android:shape="oval"> <size android:height="10dp" android:width="10dp"></size> <solid android:color="#aaffffff"></solid> </shape> </item> </selector>
public class MainActivity extends AppCompatActivity { private List<ImageView> list; private LinearLayout dotLayout; private int prePosition; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initView(); //开启一个新线程,实现ViewPager的自动滚动 new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } //runOnUiThread中的代码运行在主线程中 runOnUiThread(new Runnable() { @Override public void run() { viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); } }); } } }).start(); } private void initView() { viewPager = (ViewPager) findViewById(R.id.viewPager); //构造适配器 MyAdapter adapter = new MyAdapter(list); //设置Adapter viewPager.setAdapter(adapter); //设置ViewPager预加载的页数 // viewPager.setOffscreenPageLimit(2); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { //ViewPager滑动过程中调用 @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } //ViewPager新页面确定后调用 @Override public void onPageSelected(int position) { //设置前一个View为白色 dotLayout.getChildAt(prePosition).setEnabled(false); //设置当前View为红色 dotLayout.getChildAt(position % list.size()).setEnabled(true); //将当前位置赋给prePosition prePosition = position % list.size(); } //ViewPager滑动状态改变后调用 //0--静止状态 //1--手在拖动 //2--手松开后自动滑动 @Override public void onPageScrollStateChanged(int state) { } }); //设置ViewPager的初始item viewPager.setCurrentItem(Integer.MAX_VALUE / 2 - 3); } /** * 构造数据源 */ private void initData() { dotLayout = (LinearLayout) findViewById(R.id.dotLayout); list = new ArrayList<>(); int[] imgs = new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e}; for (int img : imgs) { //使用Java代码创建一个ImageView ImageView imageView = new ImageView(this); //设置图片以及ScaleType属性 imageView.setImageResource(img); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); list.add(imageView); //创建一个View对象 View view = new View(this); //设置View的宽和高 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics())); //设置View左边距,避免View连在一起 params.leftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getResources().getDisplayMetrics()); view.setLayoutParams(params); //设置背景颜色,背景使用selector,可使View在不同状态显示不同颜色 view.setBackgroundResource(R.drawable.dot_radiobutton_style); //默认状态下View的Enable属性为false(View全为白色) view.setEnabled(false); //将View添加进LinearLayout中 dotLayout.addView(view); } //第一个View默认为红色 dotLayout.getChildAt(0).setEnabled(true); } }
时间: 2024-11-05 16:55:02