[置顶] Android ViewPager无限自动轮播开源库(多布局、自定义hintView)

13人阅读 评论(0) 收藏 举报
分类:

GitHub
APK

如有任何问题或者是建议,可以QQ群:安娇德IT技术交流群757173381讨论,开源库会根据需求持续更新。

使用方法

将libray模块复制到项目中,或者直接在build.gradle中依赖:

allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
dependencies {
            compile 'com.github.AnJiaoDe:LoopViewPager:1.1.1'
    }

1.VP+Layout

这里写图片描述

<?xml version="1.0" encoding="utf-8"?>
<com.cy.rollpagerview.CYRollPagerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/vp"
    android:layout_width="match_parent"
    android:layout_height="400dp">

</com.cy.rollpagerview.CYRollPagerView>
package com.cy.cyrollpagerview;

import android.os.Bundle;
import android.view.View;

import com.cy.rollpagerview.CYColorPointHintView;
import com.cy.rollpagerview.CYLoopPagerAdapter;
import com.cy.rollpagerview.CYRollPagerView;

import java.util.ArrayList;
import java.util.List;

public class VPLayoutActivity extends BaseActivity {
    private CYLoopPagerAdapter<VPBean> cyLoopPagerAdapter;
    private CYRollPagerView<VPBean> cyRollPagerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vplayout);
        List<VPBean> list=new ArrayList<>();

        list.add(new VPBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523860722694&di=6671f0dd55b6b164a096fc52145b75a6&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fbaike%2Fpic%2Fitem%2Fd009b3de9c82d158b62f49ef890a19d8bc3e423a.jpg",
                "添加hi偶就能够"));
        list.add(new VPBean("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2777350900,2402717967&fm=27&gp=0.jpg",
                "德国人和"));
        list.add(new VPBean("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2666040239,1399277915&fm=27&gp=0.jpg",
                "如果和认同和"));
        list.add(new VPBean("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2341746217,231983218&fm=11&gp=0.jpg",
                "人皇太后"));
        list.add(new VPBean("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2469049613,2268261369&fm=27&gp=0.jpg",
                "而天花板"));

        cyRollPagerView= (CYRollPagerView<VPBean>) findViewById(R.id.vp);

        cyLoopPagerAdapter=new CYLoopPagerAdapter<VPBean>(cyRollPagerView,list) {


            @Override
            public void bindDataToView(ViewHolder holder, int position, VPBean bean) {


                holder.setImage(R.id.iv,bean.getUrl(),1080,500);
                holder.setText(R.id.tv,bean.getText());
            }


            @Override
            public int getItemLayoutID(int position, VPBean bean) {
                return R.layout.item_layout;
            }
            @Override
            public void onItemClick(int position, VPBean bean) {

            }
        };

        cyRollPagerView.setAdapter(cyLoopPagerAdapter,3000,new CYColorPointHintView(this));
    }

    @Override
    public void onClick(View v) {

    }
}

2.VP+Layout+Multi

这里写图片描述

<?xml version="1.0" encoding="utf-8"?>
<com.cy.rollpagerview.CYRollPagerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/vp"
    android:layout_width="match_parent"
    android:layout_height="400dp">

</com.cy.rollpagerview.CYRollPagerView>
package com.cy.cyrollpagerview;

import android.os.Bundle;
import android.view.View;

import com.cy.rollpagerview.CYColorPointHintView;
import com.cy.rollpagerview.CYLoopPagerAdapter;
import com.cy.rollpagerview.CYRollPagerView;

import java.util.ArrayList;
import java.util.List;

public class VPLayoutMultiActivity extends BaseActivity {

    private CYLoopPagerAdapter<VPBean> cyLoopPagerAdapter;
    private CYRollPagerView<VPBean> cyRollPagerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vplayout_multi);
        List<VPBean> list=new ArrayList<>();

        list.add(new VPBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523860722694&di=6671f0dd55b6b164a096fc52145b75a6&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fbaike%2Fpic%2Fitem%2Fd009b3de9c82d158b62f49ef890a19d8bc3e423a.jpg",
                "添加hi偶就能够"));
        list.add(new VPBean("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2777350900,2402717967&fm=27&gp=0.jpg",
                "德国人和"));
        list.add(new VPBean("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2666040239,1399277915&fm=27&gp=0.jpg",
                "如果和认同和"));
        list.add(new VPBean("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2341746217,231983218&fm=11&gp=0.jpg",
                "人皇太后"));
        list.add(new VPBean("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2469049613,2268261369&fm=27&gp=0.jpg",
                "而天花板"));

        cyRollPagerView= (CYRollPagerView<VPBean>) findViewById(R.id.vp);

        cyLoopPagerAdapter=new CYLoopPagerAdapter<VPBean>(cyRollPagerView,list) {


            @Override
            public void bindDataToView(ViewHolder holder, int position, VPBean bean) {


                holder.setImage(R.id.iv,bean.getUrl(),1080,500);
                holder.setText(R.id.tv,bean.getText());
            }


            @Override
            public int getItemLayoutID(int position, VPBean bean) {

                if (position==0||position==3){
                    return R.layout.item_layout2;
                }
                return R.layout.item_layout;
            }
            @Override
            public void onItemClick(int position, VPBean bean) {

            }
        };

        cyRollPagerView.setAdapter(cyLoopPagerAdapter,new CYColorPointHintView(
                this,0xffff0000,0xffffffff,8,8,4));
    }

    @Override
    public void onClick(View v) {

    }
}

CYLoopPagerAdapter

package com.cy.rollpagerview;

import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.LoopPagerAdapter;

import java.util.List;

/**
 * Created by lenovo on 2018/1/10.
 */

public abstract class CYLoopPagerAdapter<T> extends LoopPagerAdapter {
    private List<T> list_bean;

    public CYLoopPagerAdapter(RollPagerView viewPager) {
        super(viewPager);
    }

    public CYLoopPagerAdapter(RollPagerView viewPager, List<T> list_bean) {
        super(viewPager);
        this.list_bean = list_bean;
    }

    @Override
    public View getView(ViewGroup container, final int position) {

        View view=LayoutInflater.from(container.getContext()).inflate
                (getItemLayoutID(position,list_bean.get(position)), container, false);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick(position,list_bean.get(position));
            }
        });
        bindDataToView(new ViewHolder(view),position,list_bean.get(position));
        return view;

    }

    @Override
    protected int getRealCount() {
        return list_bean.size();
    }
    //填充数据
    public abstract void bindDataToView(ViewHolder holder, int position, T bean);
    /*
         取得ItemView的布局文件
         @return
        */
    public abstract int getItemLayoutID(int position, T bean);
     /*
      ItemView的单击事件

      @param position
     */

    public abstract void onItemClick(int position, T bean);

    public int addAll(List<T> beans) {
        list_bean.addAll(beans);

        notifyDataSetChanged();
        return beans.size();
    }
    public int clearAddAll(List<T> beans) {
        list_bean.clear();
        list_bean.addAll(beans);

        notifyDataSetChanged();
        return beans.size();
    }

    public static class ViewHolder {
        private View itemView;
        private SparseArray<View> array_view;

        public ViewHolder(View itemView) {
            this.itemView = itemView;

            array_view = new SparseArray<View>();

        }


        public <T extends View> T getView(int viewId) {

            View view = array_view.get(viewId);
            if (view == null) {
                view = itemView.findViewById(viewId);
                array_view.put(viewId, view);
            }
            return (T) view;
        }


        public ViewHolder setVisible(int res_id) {
            getView(res_id).setVisibility(View.VISIBLE);
            return this;
        }

        public ViewHolder setInVisible(int res_id) {
            getView(res_id).setVisibility(View.INVISIBLE);
            return this;
        }

        public void setViewGone(int res_id) {
            getView(res_id).setVisibility(View.GONE);
        }

        public void setViewVisible(int res_id) {
            getView(res_id).setVisibility(View.VISIBLE);
        }


        public void setText(int tv_id, String text) {
            TextView tv = getView(tv_id);


            tv.setText(nullToString(text));
        }

        public String nullToString(Object object) {
            return object == null ? "" : object.toString();
        }

        public void setPriceText(int tv_id, String text) {
            TextView tv = getView(tv_id);

            tv.setText("¥" + text);
        }

        public void setCountText(int tv_id, String text) {
            TextView tv = getView(tv_id);

            tv.setText("x" + text);
        }

        public void setCountText(int tv_id, int text) {
            TextView tv = getView(tv_id);

            tv.setText("x" + text);
        }

        public void setPriceText(int tv_id, int text) {
            TextView tv = getView(tv_id);

            tv.setText("¥" + text);
        }

        public void setPriceText(int tv_id, float text) {
            TextView tv = getView(tv_id);

            tv.setText("¥" + text);
        }

        public void setText(int tv_id, int text) {
            TextView tv = getView(tv_id);
            tv.setText(String.valueOf(nullToString(text)));
        }

        public void setTextColor(int tv_id, int color) {
            TextView tv = getView(tv_id);
            tv.setTextColor(color);
        }

        public String getTVText(int tv_id) {
            TextView tv = getView(tv_id);
            return tv.getText().toString().trim();
        }

        public String getETText(int tv_id) {
            EditText tv = getView(tv_id);
            return tv.getText().toString().trim();
        }

        public void setBackgroundResource(int v_id, int resid) {
            View view = getView(v_id);
            view.setBackgroundResource(resid);
        }

        public void setImageBitmap(int v_id, Bitmap bitmap) {
            ImageView view = getView(v_id);
            view.setImageBitmap(bitmap);
        }

        public void setImageResource(int v_id, int resID) {
            ImageView view = getView(v_id);
            view.setImageResource(resID);
        }

        public void setProgress(int progress_id, int progress) {
            ProgressBar progressBar = getView(progress_id);
            progressBar.setProgress(progress);

        }


        public void setImage( int iv_id, String url) {
            ImageView iv = getView(iv_id);

            GlideUtils.loadImageByGlide(itemView.getContext(), url, iv);
        }
        public void setImage( int iv_id, String url, int width,int height) {
            ImageView iv = getView(iv_id);

            GlideUtils.loadImageByGlide(itemView.getContext(), url, iv, width,height);
        }
        public void setOnClickListener(int res_id, View.OnClickListener onClickListener) {
            getView(res_id).setOnClickListener(onClickListener);
        }

        public void setOnLongClickListener(int res_id, View.OnLongClickListener onLongClickListener) {
            getView(res_id).setOnLongClickListener(onLongClickListener);
        }

    }
}

参考:Android ViewPager

查看评论

自定义ViewPager实现图片自动轮播无限循环

Viewpager图片自动轮播无限循环是Android开发中经常用到的功能,功能实现起来也比较简单。虽然如此,但是很多情况下做出来的效果并不太让人满意,甚至有些上线的项目自动轮播上也会出现一些bug。...
  • qq_20521573
  • qq_20521573
  • 2016-07-26 17:33:51
  • 10583

Android ViewPager无限自动轮播

ViewPager android support v4 中的控件 布局当中用全包名的类名声明 android.support.v4.view.ViewPager viewpager依赖数据,通过se...
  • confusing_awakening
  • confusing_awakening
  • 2017-06-27 18:03:36
  • 1473

ViewPager实现图片自动轮播和无限循环(完美流畅)

  • 2016年07月26日 22:45
  • 1.04MB
  • 下载

ViewPager实现图片的自动轮播和无限循环

在开发的时候想做个图片的轮播,在网上找了一些资料,发现不符合我个人的习惯,然后就自己写了个,还是直接贴上代码吧... DishDetailActivity.java import java.util...
  • zhaowei_zhang
  • zhaowei_zhang
  • 2015-09-30 16:08:31
  • 2336

ViewPager图片自动+手动左右无限轮播

写在前面: 最近做的一个小项目有图片轮播的需求,各种查资料发现大部分都是通过设置adapter的getCount方法返回Integer.MAX_VALUE实现的。很显然,这种方法有很多弊端,比如很容...
  • qq_32199531
  • qq_32199531
  • 2016-07-03 23:04:33
  • 3866

Android中级:轮播图(一):实现ViewPager的无线自动循环

无限自动循环 = 无限循环 + 自动循环 无限循环 = 无限向左循环 + 无限向右循环 接下来我们通过demo一步步的实现无限向右循环–>无限向左循环–>自动循环 Demo中viewpager...
  • ss1168805219
  • ss1168805219
  • 2016-08-23 01:10:04
  • 3648

Android:ViewPager轮播图

  • 2016年07月10日 11:46
  • 13.41MB
  • 下载

自定义垂直跑马灯

  • 2017年03月30日 11:55
  • 2.98MB
  • 下载

使用ViewPager实现自动无限循环的轮播

别人都说使用ViewPager轮播很简单,但还是需要自己手动来操作下。顺便总结下实现无限循环轮播的思路。要使用ViewPager实现无限循环播放的轮播,会涉及到以下知识点: 1、ViewPager的...
  • ChasingYourDream
  • ChasingYourDream
  • 2015-10-27 16:55:41
  • 933

ViewPager无限自动轮播+动画

ViewPager几乎在每一个app中都会有使用,可以做导航页,也可以做广告、图片的播放。最近在做的app也用到了ViewPager,具体要实现的功能是能够无限轮播,并且在自动轮播时,图片在切换时能够...
  • My_First_ID
  • My_First_ID
  • 2016-04-27 15:30:50
  • 1041
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 4万+
    积分: 2343
    排名: 1万+
    博客专栏
    最新评论
    JAVAEE