viewpage轮播图
<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.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="160dp" > <androidx.viewpager.widget.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" android:background="#66000000" android:gravity="center_horizontal" android:orientation="vertical" android:padding="5dp" app:layout_constraintBottom_toBottomOf="@+id/viewpager"> <TextView android:id="@+id/tv_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:text="社会" android:textColor="@android:color/white" /> <LinearLayout android:id="@+id/ll_point_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="horizontal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener { private ViewPager viewPager; private int[] imageResIds; private ArrayList<ImageView> imageViewList; private ArrayList<View> pointViews; private LinearLayout ll_point_container; private String[] contentDesc; private TextView tv_desc; private int lastEnablePoint = 0; private boolean isRunning = true; private Handler handler; private Runnable runnable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化布局 View 视图 initViews(); // Model 数据 initData(); // Controller 控制器 initAdapter(); //1. handler = new Handler(); runnable= new Runnable() { @Override public void run() { viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); handler.postDelayed(this, 2000); } }; //2 // mTimer.schedule(mTimerTask, 2000, 2000); //3 // handler.postDelayed( runnable, 2000 ); //开启轮询 // new Thread() { // public void run() { // while(isRunning) { // try { // Thread.sleep(2000); // } catch (InterruptedException e) { // e.printStackTrace(); // } // //往下跳一位 // runOnUiThread(new Runnable() { // @Override // public void run() { // viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); // } // }); // } // // } // }.start(); } @Override protected void onPause() { super.onPause(); handler.removeCallbacksAndMessages( null ); } @Override protected void onResume() { super.onResume(); handler.postDelayed( runnable, 2000 ); } @Override protected void onDestroy() { super.onDestroy(); isRunning = false; } private void initViews() { viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setOnPageChangeListener(this);// 设置滚动更新 viewPager.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: handler.removeCallbacksAndMessages( null ); break; case MotionEvent.ACTION_UP: handler.postDelayed( runnable, 2000 ); break; } return false; } }); tv_desc = (TextView) findViewById(R.id.tv_desc); ll_point_container = findViewById(R.id.ll_point_container); } private void initAdapter() { ll_point_container.getChildAt(0).setEnabled(true); tv_desc.setText(contentDesc[0]); // 设置适配器 viewPager.setAdapter(new MyAdapter()); //默认设置到中间的某个位置 int pos = Integer.MAX_VALUE / 2; //viewPager.setCurrentItem(pos - 3); viewPager.setCurrentItem(0); } private void initData() { // 初始化要显示的数据 // 图片资源id数组 imageResIds = new int[] { R.drawable.lunbo1, R.drawable.lunbo2, R.drawable.lunbo3, R.drawable.lunbo4}; // 文本描述 contentDesc = new String[] { "巩俐不低俗,我就不能低俗", "朴树又回来啦!再唱经典老歌引万人大合唱", "揭秘北京电影如何升级", "乐视网TV版大派送", "热血屌丝的反杀" }; imageViewList = new ArrayList<ImageView>(); pointViews = new ArrayList<View>(); ImageView imageView; View pointView; LinearLayout.LayoutParams layoutParams; for (int i = 0; i < imageResIds.length; i++) { imageView = new ImageView(this); imageView.setBackgroundResource(imageResIds[i]); imageViewList.add(imageView); // 加小白点,指示器 pointView = new View(this); pointView.setBackgroundResource(R.drawable.select); layoutParams = new LinearLayout.LayoutParams(7, 7); if (i != 0) { layoutParams.leftMargin = 10; } pointView.setEnabled(false); ll_point_container.addView(pointView, layoutParams); } } class MyAdapter extends PagerAdapter { @Override public int getCount() { return Integer.MAX_VALUE; } // 3、指定复用的判断逻辑 @Override public boolean isViewFromObject(View view, Object object) { // 当滑到新的条目,又返回来,view是否可以被复用 return view == object; } // 1、返回要显示的条目内容,创建条目 @Override public Object instantiateItem(ViewGroup container, int position) { // container:容器:ViewPager // position:当前要显示的条目的位置 position = position % 4; ImageView imageView = imageViewList.get(position); // a、把view对象添加到container中 container.addView(imageView); // b、把view对象返回给框架,适配器 return imageView; // 必须要重写,否则抛异常 } // 2、销毁条目 @Override public void destroyItem(ViewGroup container, int position, Object object) { // object 要销毁的对象 container.removeView((View) object); } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // 滚动时调用 } @Override public void onPageSelected(int position) { // 新条目被选中时调用 position = position % 4; tv_desc.setText(contentDesc[position]); // 把之前的禁用,把最新的启用 ll_point_container.getChildAt(lastEnablePoint).setEnabled(false); ll_point_container.getChildAt(position).setEnabled(true); lastEnablePoint = position; } @Override public void onPageScrollStateChanged(int state) { // 滚动状态变化时调用 } }
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <corners android:radius="5dp" /> <solid android:color="@android:color/white" /> </shape>
<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>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true" android:drawable="@drawable/shap1"></item> <item android:state_enabled="false" android:drawable="@drawable/shap2"></item> </selector>