概述: 要用ViewPager实现图片轮播,主要是两步: 1:用PagerAdapter使图片可以滑动切换 2:用Handler来实现图片自动轮播
页面布局部分的代码:
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawerlayout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="wrap_content" android:layout_height="150dp" android:orientation="vertical"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="fill_parent" android:layout_height="match_parent" /> <AbsoluteLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/viewgroup" android:layout_width="match_parent" android:layout_height="30dp" android:layout_alignParentRight="true" android:orientation="horizontal" android:gravity="right|bottom" android:layout_y="120dip"> </LinearLayout> </AbsoluteLayout> </FrameLayout> </LinearLayout></android.support.v4.widget.DrawerLayout>控制逻辑部分的java代码:
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener { PRivate MyHandler handler = new MyHandler(new WeakReference<>(this)); private ViewPager viewPaper; private ImageView[] tips = null; private ImageView[] imgs = null; private ViewGroup group; private int[] ids = new int[]{R.drawable.sc_1,R.drawable.sc_2,R.drawable.sc_3};//载入图片 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); group = (ViewGroup)findViewById(R.id.viewgroup); viewPaper = (ViewPager)findViewById(R.id.viewPager); //将标识点装载入ViewGroup中 tips = new ImageView[ids.length]; for(int i=0;i<ids.length;i++){ ImageView imageview = new ImageView(this); imageview.setLayoutParams(new LinearLayout.LayoutParams(7,7));//设置标识点的大小 tips[i] = imageview; if(i==0){//加载图片 tips[i].setBackgroundResource(R.drawable.page_indicator_focused); }else{ tips[i].setBackgroundResource(R.drawable.page_indicator_focused); } //设置标识点的位置参数 LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); layoutparams.leftMargin = 3; layoutparams.rightMargin = 3; group.addView(imageview,layoutparams); } group.bringToFront(); //将轮播图片加载到数组中 imgs = new ImageView[ids.length]; for(int i=0;i<ids.length;i++){ ImageView imageview = new ImageView(this); imgs[i] = imageview; imageview.setBackgroundResource(ids[i]); } //设置Adapter viewPaper.setAdapter(new MyAdapter()); //设置监听。主要是设置标识点背景 viewPaper.setOnPageChangeListener(this); viewPaper.setCurrentItem((imgs.length)*100); //启动轮播 handler.sendEmptyMessageDelayed(MyHandler.MSG_UPDATE_IMG, MyHandler.MSG_DELAY); } public class MyAdapter extends PagerAdapter{ @Override public int getCount(){ return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View arg0, Object arg1){ return arg0 == arg1; } @Override public void destroyItem(View container,int position,Object object){// ((ViewPager)container).removeView(imgs[position % imgs.length]); } //载入图片进去 @Override public Object instantiateItem(View container, int position){ position %= imgs.length; ImageView view = imgs[position % imgs.length]; ViewParent vp = view.getParent(); if(vp != null){ ViewGroup parent = (ViewGroup)vp; parent.removeView(view); } try { ((ViewPager) container).addView(imgs[position],0); }catch(Exception e){ } return imgs[position % imgs.length]; } } @Override public void onPageScrollStateChanged(int arg0) { switch(arg0){ case ViewPager.SCROLL_STATE_DRAGGING://正在滑动 handler.sendEmptyMessage(MyHandler.MSG_STOP_IMG); break; case ViewPager.SCROLL_STATE_IDLE://已经完成切换 handler.sendEmptyMessageDelayed(MyHandler.MSG_UPDATE_IMG,MyHandler.MSG_DELAY); break; default: break; } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int arg0) { setImageBackground(arg0 % imgs.length); handler.sendMessage(Message.obtain(handler,MyHandler.MSG_PAGE_OLD,arg0,0));//记录切换到的页码 } /** * 设置选中的tip的背景 * @param selectItems */ private void setImageBackground(int selectItems){ for(int i=0; i<tips.length; i++){ if(i == selectItems){ tips[i].setBackgroundResource(R.drawable.page_indicator_focused); }else{ tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused); } } } public class MyHandler extends Handler{ /* 请求轮播 */ protected static final int MSG_UPDATE_IMG = 1; /* 请求暂停轮播 */ protected static final int MSG_STOP_IMG = 2; /* 请求开始轮播 */ protected static final int MSG_START_IMG = 3; /* 记录要开始轮播的页号 */ protected static final int MSG_PAGE_OLD = 4; /* 轮播时间间隔 */ protected static final long MSG_DELAY = 1000; //使用弱引用 private WeakReference<MainActivity> weakReference; private int currentItem = 0; protected MyHandler(WeakReference<MainActivity> wk){ this.weakReference = wk; } public void handleMessage(Message msg) { super.handleMessage(msg); MainActivity activity = weakReference.get(); if(activity == null){ return; } //检查并移除队列中未发送的消息 if (activity.handler.hasMessages(MSG_UPDATE_IMG) && currentItem > 1){ activity.handler.removeMessages(MSG_UPDATE_IMG); } switch(msg.what){ case MSG_UPDATE_IMG: currentItem ++; //设置一个显示图片的下标,实现切换效果 activity.viewPaper.setCurrentItem(currentItem); //准备下次切换 activity.handler.sendEmptyMessageDelayed(MSG_UPDATE_IMG,MSG_DELAY); break; case MSG_STOP_IMG: //不发送消息就暂停了 break; case MSG_START_IMG: activity.handler.sendEmptyMessageDelayed(MSG_UPDATE_IMG,MSG_DELAY); break; case MSG_PAGE_OLD: //记录页号 currentItem = msg.arg1; break; default: break; } } }}新闻热点
疑难解答