ads 輪播

package com.example.appuser.testother.widget;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.signature.StringSignature;
import com.corelibs.utils.DisplayUtil;
import com.example.appuser.testother.R;

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

/**
 * Created by appuser on 2017/12/18.
 */

public class AdsView extends FrameLayout implements View.OnClickListener{

    public static final int SCROLL_SPEED=3000;
    private ViewPager vp_ads;
    private List<String> ads;
    private List<ImageView> images;
    private int count = 0;

    private Handler handler = new Handler();
    private OnAdClickedListener listener;
    private boolean autoScroll = true;
    private LinearLayout ll_dot;
    private List<View> dots;


    public AdsView(Context context) {
        super(context);
        init();
    }

    public AdsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        View view= LayoutInflater.from(getContext()).inflate(R.layout.v_ad,this);
        vp_ads = (ViewPager) view.findViewById(R.id.vp_ads);
        ll_dot =view.findViewById(R.id.ll_dots);
    }

    public void setAutoScroll(boolean autoScroll){
        this.autoScroll=autoScroll;
    }

    public void setAds(List<String> ads){
        vp_ads.removeAllViews();

        this.ads=ads;
        if (ads.size() <= 0) return;
        initViews();
        initViewPager();
    }

    public void setAdsEmpty(){
        vp_ads.removeAllViews();
    }
    public void setOnAdClickedListener(OnAdClickedListener listener) {
        this.listener = listener;
    }

    /**
     * 根據數量初始化圖片和小圓點
     */
    private void initViews(){
        count=ads.size();
        images=new ArrayList<>();
        dots=new ArrayList<>();
        ll_dot.removeAllViews();
        for(int i=0;i<count;i++){
            View view=buildChildView();
            ll_dot.addView(view);
            dots.add(view);
        }

        for(int i=0;i<count;i++){
            ImageView imageView =new ImageView(getContext());
            imageView.setTag(R.id.adapter_item_tag_key,i);
            imageView.setOnClickListener(this);
            Glide.with(getContext()).load(ads.get(i))
                    .signature(new StringSignature(System.currentTimeMillis()+""))
                    .listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    Log.e("yzh","onResourceReady");
                    return false;
                }
            }).into(imageView);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            images.add(imageView);
        }
    }

    private View buildChildView(){
        View view=new View(getContext());
        LinearLayout.LayoutParams lp =new LinearLayout.LayoutParams(DisplayUtil.dip2px(getContext(),8),DisplayUtil.dip2px(getContext(),8));
        view.setBackgroundResource(R.drawable.dot_normal);
        lp.setMargins(DisplayUtil.dip2px(getContext(),3),0,DisplayUtil.dip2px(getContext(),3),0);
        view.setLayoutParams(lp);
        return view;
    }


    private void initViewPager(){
        vp_ads.setAdapter(new RecommendAdAdapter());
        vp_ads.addOnPageChangeListener(new RecommendAdPageChangedListener());
        vp_ads.setCurrentItem(0);
        dots.get(0).setBackgroundResource(R.drawable.dot_select);
        if (autoScroll) handler.postDelayed(runnable, SCROLL_SPEED);
    }

    @Override
    public void onClick(View view) {
        int postion=(int)view.getTag(R.id.adapter_item_tag_key);
        if(listener!=null) listener.onAdClicked((ImageView)view,postion);
    }

    public interface OnAdClickedListener{
        void onAdClicked(ImageView view,int position);
    }

    class RecommendAdPageChangedListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (autoScroll) handler.removeCallbacks(runnable);

            int index=position%count;
            for(int i=0;i<count;i++){
                if(i==index){
                    dots.get(i).setBackgroundResource(R.drawable.dot_select);
                }else{
                    dots.get(i).setBackgroundResource(R.drawable.dot_normal);
                }
            }

            if (autoScroll) handler.postDelayed(runnable, SCROLL_SPEED);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    private Runnable runnable =new Runnable() {
        @Override
        public void run() {
            int current=vp_ads.getCurrentItem();
            if(count!=0)
            if(autoScroll) vp_ads.setCurrentItem((current + 1)%count);
        }
    };

    private int add=0;
    class RecommendAdAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return count<3?count:Integer.MAX_VALUE;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            int i=position%count;
            add=i;
            if (images.get(i).getParent() != null) {
                ((ViewPager) images.get(i).getParent()).removeView(images.get(i));
            }
            container.addView(images.get(i));
            return images.get(i);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            int i=position%count;
            if(count==3 &&Math.abs(add-i)==0){
                return;
            }
            container.removeView(images.get(i));
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (autoScroll) handler.postDelayed(runnable, SCROLL_SPEED);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (autoScroll) handler.removeCallbacks(runnable);
    }
}



<com.example.appuser.testother.widget.AdsView
    android:layout_width="match_parent"
    android:layout_height="190dp"
    android:id="@+id/ads"
    />

---------
vp
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vp_ads"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/ll_dots"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginBottom="20dp"
        ></LinearLayout>
    <com.example.appuser.testother.widget.MyArcView
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>
use
ads.setAds(list_data);

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章