ViewPager实现可以自动滚动的广告展示

广告展示用 ViewPager + PagerAdapter 实现,自动滚动直接用postDelayed实现。

指示器用的是系统的RadioButton,不是很和谐,可以自己重写一个。

要注意的是:postDelayed不要重复new Runnable,不然手动滑动的时候会产生很多个runnable,会出现乱滚动。

不知道RadioGroup为什么不能这check(1),这里只能默认选择最后一个。

效果图如下:
ViewPager实现可以自动滚动的广告展示_第1张图片

MainActivity.java

package myviewpager.xiaopan.com.myviewpager;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ViewPager vpShow ;
    private ShowViewPagerAdapter showAdapter;
    private ArrayList<AdBean> arr_Ads;
    private RadioGroup rgCursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        arr_Ads = new ArrayList<>();
        arr_Ads.add(new AdBean("第一幅广告图",R.drawable.pic1));
        arr_Ads.add(new AdBean("第二幅广告图",R.drawable.pic2));
        arr_Ads.add(new AdBean("第三幅广告图",R.drawable.pic3));

        initView();
    }
    private void initView(){
        vpShow = (ViewPager) findViewById(R.id.vp_show);

        showAdapter = new ShowViewPagerAdapter(this,arr_Ads);
        vpShow.setAdapter(showAdapter);

        rgCursor = (RadioGroup) findViewById(R.id.rg_cursor);
        for(int i = 0; i < arr_Ads.size(); i++){
            RadioButton rb = new RadioButton(this);
            rb.setId(i); rb.setClickable(false);
            rgCursor.addView(rb);
            rgCursor.check(i);
        }

        vpShow.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction()== MotionEvent.ACTION_DOWN){
                    doAutoScroll(false);
                }else if(motionEvent.getAction()== MotionEvent.ACTION_UP){
                    doAutoScroll(true);
                }
                return false; // 不能消费掉,还要响应滑动等其它事件
            }
        });
        vpShow.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Log.d("onPageScrolled","="+position);
            }

            @Override
            public void onPageSelected(final int position) {
                Log.d("OnPageSelected","="+position);
                rgCursor.check(position);
                doAutoScroll(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
// Log.d("PageScrollStateChanged","="+state);
            }
        });
        doAutoScroll(true);

    }
    Runnable autoScroll = new Runnable() {
        @Override
        public void run() {
            int currPos = vpShow.getCurrentItem();
            vpShow.setCurrentItem(currPos>=arr_Ads.size()-1?0:currPos+1);
        }
    };
    private void doAutoScroll(boolean bAuto){
        Log.d("doAutoScroll"," = "+bAuto);
        vpShow.removeCallbacks(autoScroll);
        if(bAuto){
            vpShow.postDelayed(autoScroll,6000);
        }
    }

    class AdBean{
        public String strTitle;
        public int resId;
        public AdBean(String strTitle, int resId){
            this.strTitle = strTitle;
            this.resId = resId;
        }
    }
}

适配器
ShowViewPagerAdapter.java

package myviewpager.xiaopan.com.myviewpager;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/** * Created by Pansing on 2015/7/13. */
public class ShowViewPagerAdapter extends PagerAdapter {

    private Context context;
    private ArrayList<MainActivity.AdBean> arr_Ads;

    public ShowViewPagerAdapter(Context context, ArrayList<MainActivity.AdBean> arr_Ads){
        this.context = context;
        this.arr_Ads = arr_Ads;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(arr_Ads==null){
            return 0;
        }
        return arr_Ads.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = View.inflate(context,R.layout.view_show_item,null);
        ImageView ivShow = (ImageView) view.findViewById(R.id.iv_show_item);
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_title_item);
        ivShow.setImageResource(arr_Ads.get(position).resId);
        tvTitle.setText(arr_Ads.get(position).strTitle);
        container.addView(view);

        return view;
    }

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

布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_show"
        android:layout_width="match_parent"
        android:layout_height="200dp">
    </android.support.v4.view.ViewPager>
    <RadioGroup
        android:id="@+id/rg_cursor"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:padding="5dp"
        android:layout_alignBottom="@id/vp_show"
        android:layout_alignRight="@id/vp_show"
        android:orientation="horizontal">
    </RadioGroup>

</RelativeLayout>

Item布局文件

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

    <ImageView  android:id="@+id/iv_show_item" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" />
    <TextView  android:id="@+id/tv_title_item" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="34dp" android:gravity="center" android:textColor="@android:color/white" android:background="#A0333333" />

</RelativeLayout>

你可能感兴趣的:(ViewPager实现可以自动滚动的广告展示)