ViewPager总结

混江龙づ霸主 提交于 2020-03-21 16:16:10

https://github.com/youth5201314/banner

compile 'com.youth.banner:banner:1.4.9'
private void setBanner() {
        if (banner == null)
            return;
        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(images);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //设置标题集合(当banner样式有显示title时)
//        banner.setBannerTitles(titles);
        //设置自动轮播,默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(5000);
        //设置指示器位置(当banner模式中有指示器时)
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

 

 

1.添加布局fragment_hall_head.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/transparent"    android:orientation="vertical">    <FrameLayout
        android:id="@+id/fl"        android:layout_width="match_parent"        android:layout_height="300px">        <android.support.v4.view.ViewPager            android:id="@+id/viewPager"            android:layout_width="match_parent"            android:layout_height="300px" />        <LinearLayout            android:id="@+id/ll_points"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="bottom|center_horizontal"            android:layout_marginBottom="11dp"            android:orientation="horizontal" />    </FrameLayout></LinearLayout>2.添加资源文件
shape_xiao_yuan_quan_selected.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="oval">    <size        android:width="8dp"        android:height="8dp" />    <stroke        android:width="1px"        android:color="@color/colorAccent" />    <solid android:color="@color/colorAccent" /></shape>
shape_xiao_yuan_quan_nomal.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="oval">    <size        android:width="8dp"        android:height="8dp" />    <stroke        android:width="1px"        android:color="@color/white" />    <solid android:color="@color/white" /></shape>3.查找控件
List<String> imgesUrl = new ArrayList<>();imgesUrl.add("http://img3.fengniao.com/forum/attachpics/913/114/36502745.jpg");imgesUrl.add("http://imageprocess.yitos.net/images/public/20160910/99381473502384338.jpg");imgesUrl.add("http://imageprocess.yitos.net/images/public/20160910/77991473496077677.jpg");imgesUrl.add("http://imageprocess.yitos.net/images/public/20160906/1291473163104906.jpg");Banner banner = new Banner(MainActivity.this, imgesUrl, findViewById(R.id.fl));
4.添加动画类:
import android.content.Context;import android.os.Handler;import android.os.Message;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.LinearLayout;import java.util.List;/** * Created by Administrator on 2017/2/28. */public class Banner {    private LinearLayout llPoints; //图片里面的小圆点    private ViewPager viewPager;  //图片轮播    List<String> imgesUrl;    Context context;    View view;    public Banner(Context context, List<String> imgesUrl, View view) {        this.context = context;        this.imgesUrl = imgesUrl;        this.view = view;        viewPager = (ViewPager) view.findViewById(R.id.viewPager);        llPoints = (LinearLayout) view.findViewById(R.id.ll_points);        startBanner();    }    /**     * 处理轮播图     */    public void startBanner() {        MyPagerAdapter myPagerAdapter = new MyPagerAdapter();        viewPager.setAdapter(myPagerAdapter);        viewPager.setCurrentItem(imgesUrl.size() * 10000);        startRool();        //初始化ViewPager轮播小圆圈        llPoints.removeAllViews();        for (int i = 0; i < imgesUrl.size(); i++) {            ImageView point = new ImageView(context);            if (i == 0) {                point.setImageResource(R.drawable.shape_xiao_yuan_quan_selected);            } else {                point.setImageResource(R.drawable.shape_xiao_yuan_quan_nomal);            }            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);            if (i > 0) {                params.leftMargin = dp2px(context, 8);            }            llPoints.addView(point, params);        }        //ViewPager的轮播效果(小圆圈改变)        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {            @Override            public void onPageSelected(int position) {                position = position % imgesUrl.size();                for (int i = 0; i < llPoints.getChildCount(); i++) {                    ImageView image = (ImageView) llPoints.getChildAt(i);                    if (i == position) {                        image.setImageResource(R.drawable.shape_xiao_yuan_quan_selected);                    } else {                        image.setImageResource(R.drawable.shape_xiao_yuan_quan_nomal);                    }                }            }            @Override            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {            }            @Override            public void onPageScrollStateChanged(int state) {            }        });    }
   /**     * 轮播图的adapter     */    class MyPagerAdapter extends PagerAdapter {        @Override        public int getCount() {            return Integer.MAX_VALUE;        }        @Override        public boolean isViewFromObject(View view, Object object) {            return view == object;        }        @Override        public void destroyItem(ViewGroup container, int position, Object object) {            container.removeView((View) object);        }        @Override        public Object instantiateItem(ViewGroup container, int position) {            ImageView imageView = new ImageView(context);            imageView.setScaleType(ImageView.ScaleType.FIT_XY);//            ImageLoader.getInstance().displayImage(imgesUrl.get(position % imgesUrl.size()), imageView);            imageView.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                }            });            imageView.setOnTouchListener(new View.OnTouchListener() {                public boolean onTouch(View v, MotionEvent event) {                    switch (event.getAction()) {                        case MotionEvent.ACTION_DOWN:                            handler.removeCallbacksAndMessages(null);                            break;                        case MotionEvent.ACTION_UP:                            handler.sendEmptyMessageDelayed(1, 2000);                            break;                        /**                         * 当保持按下操作,并从此控件移到外层控件时,就会触发ACTION_CANCEL事件时,                         * 当前的手势被中断,不会在接收它的记录                         * 将它当做ACTION_UP事件进行处理比较好                         */                        case MotionEvent.ACTION_CANCEL:                            handler.sendEmptyMessageDelayed(1, 2000);                            break;                        default:                            break;                    }                    // true处理这个事件                    return true;                }            });            container.addView(imageView);            return imageView;        }    }    Handler handler = new Handler() {
 
  @Override        public void handleMessage(Message msg) {            if (msg.what == 1) {                int currentItem = viewPager.getCurrentItem();                currentItem++;                viewPager.setCurrentItem(currentItem);                startRool();            }        }    };    private void startRool() {        handler.sendEmptyMessageDelayed(1, 3000);    }    public static int dp2px(Context context, int dp) {        // 获取像素密码        float density = context.getResources().getDisplayMetrics().density;        return (int) (dp * density + 0.5f);    }}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!