1,首先,声明一些引用:
SharedPreferences sp; private ViewPager vp_top_pic; private List viewList; private ViewPager rollViewPager; private LinearLayout ll_point,ll_fragment_unlogin; /**用于小圆点图片*/ private List<ImageView> dotViewList; private int currentItem = 0;//当前页面 int maxValue; int extra;2,轮播图要显示的图片:
//顶部轮播图 vp_top_pic= (ViewPager) findViewById(R.id.viewpager); ll_point= (LinearLayout)findViewById(R.id.ll_point); dotViewList = new ArrayList<ImageView>(); View view1 = LayoutInflater.from(this).inflate(R.layout.top_pic, null); View view2 = LayoutInflater.from(this).inflate(R.layout.top_pic, null); View view3 = LayoutInflater.from(this).inflate(R.layout.top_pic, null); viewList = new ArrayList<View>();// 将要分页显示的View装入数组中 viewList.add(view1); viewList.add(view2); viewList.add(view3);3,设置适配器,动态添加指示点,开启轮播:
MyPagerAdapter adapter= new MyPagerAdapter(); vp_top_pic.setAdapter(adapter); //动态添加指示点 addPointToContainer(viewList); //开启轮播 maxValue= Integer.MAX_VALUE/2; extra = maxValue % viewList.size(); //这里以一个%3为0的数开始,这样打开默认会在第0个item显示 vp_top_pic.setCurrentItem(maxValue-extra); startScroll(); //为viewPager设置页面改变的监听 vp_top_pic.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i1) { } @Override public void onPageSelected(int position) { // TODO Auto-generated method stub //这里面动态改变小圆点的被背景,来实现效果 currentItem = position % dotViewList.size(); //Toast.makeText(MainActivity.this,position+"",Toast.LENGTH_LONG).show(); for(int i=0;i < dotViewList.size();i++){ //Toast.makeText(MainActivity.this,dotViewList.size()+"",Toast.LENGTH_LONG).show(); if(i == currentItem){ (dotViewList.get(currentItem)).setBackgroundResource(R.drawable.dot_focus); }else { (dotViewList.get(i)).setBackgroundResource(R.drawable.dot_normal); } } } @Override public void onPageScrollStateChanged(int i) { } });4,添加指示点以及适配器的代码如下:
/** * 给容器添加点 * @param data */ protected void addPointToContainer(List<String> data) { ll_point.removeAllViews(); for (int i = 0; i < data.size(); i++) { ImageView view = new ImageView(this); view.setBackgroundResource(R.drawable.dot_normal); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT); params.leftMargin = 15;//设置小圆点的外边距 params.rightMargin = 15; params.height = 20;//设置小圆点的大小 params.width = 20; if(i == 0){ view.setBackgroundResource(R.drawable.dot_focus); }else{ view.setBackgroundResource(R.drawable.dot_normal); } ll_point.addView(view, params); dotViewList.add(view); //上面是动态添加了三个小圆点 } } Handler handler = new Handler(){ public void handleMessage(Message msg) { super.handleMessage(msg); int currentItem = vp_top_pic.getCurrentItem(); currentItem ++; vp_top_pic.setCurrentItem(currentItem); handler.sendEmptyMessageDelayed(1, 2000);//2s发送消息 } }; //每隔2s中发送一条消息,开启轮播 private void startScroll(){ //开启轮播 handler.sendEmptyMessageDelayed(1, 2000);//2s发送消息 } //当界面退出的时候,停止轮播 private void stopScroll(){ //停止轮播 handler.removeCallbacksAndMessages(1); } @Override protected void onPause() { super.onStop(); //界面失去焦点的时候停止轮播 stopScroll(); } class MyPagerAdapter 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(ViewGroup container, int position, Object object) { //container.removeView((View) viewList.get(position));//删除页卡 } @Override public Object instantiateItem(ViewGroup container, int position) { //对ViewPager页号求模取出View列表中要显示的项 position %= viewList.size(); if (position<0){ position = viewList.size()+position; } View view = (View) viewList.get(position); //如果View已经在之前添加到了一个父组件,则必须先remove,否则会抛出IllegalStateException。 ViewParent vp =view.getParent(); if (vp!=null){ ViewGroup parent = (ViewGroup)vp; parent.removeView(view); } container.addView(view); //add listeners here if necessary return view; } }5,布局文件如下所示:
<RelativeLayout android:layout_width="match_parent" android:layout_height="120dp"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="wrap_content" > </android.support.v4.view.ViewPager> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:gravity="center" android:layout_height="wrap_content" android:background="#00000000" android:layout_alignParentBottom="true" android:weightSum="1"> <TextView android:textColor="@color/white" android:textSize="15sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顶部轮播图标题" android:layout_weight="1" /> <LinearLayout android:id="@+id/ll_point" android:layout_weight="0.31" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > </LinearLayout> </LinearLayout> </RelativeLayout>
以上代码就可以实现轮播了!