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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/confusing_awakening/article/details/79959785

GitHub
APK

使用方法

将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

GitHub

CSDN

简书

微信公众号
这里写图片描述

QQ群
这里写图片描述

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页