重温强大自定义控件之广告轮播(2)

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

 

公众号可手机查看:

自定义轮播图,通过ViewPager实现,准备自定义成一个简单框架实现轮播的各种效果。代码如下:

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private TextView tv_intro;
    private ArrayList<Ad> list= new ArrayList<>();
    private LinearLayout dot_layout;
    public String[] shape = new String[]{"oval","rect","ring"};
    private String type = "ring";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
        initListener();

    }

    private void initListener() {

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                updateIntroAndDot();
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {

        list.add(new Ad(R.mipmap.a,"巩俐不低俗,我就不低俗"));
        list.add(new Ad(R.mipmap.b,"朴树又回来了,再唱经典老歌,再唱经典老歌再唱经典老歌"));
        list.add(new Ad(R.mipmap.c,"揭秘北京电影如何升级"));
        list.add(new Ad(R.mipmap.d,"乐视版TV大放送"));
        list.add(new Ad(R.mipmap.e,"热血屌丝的反杀"));
        initDots();
        viewPager.setAdapter(new MyPagerAdapter());
        int centerValue = Integer.MAX_VALUE/2;
        int value = centerValue%list.size();
        viewPager.setCurrentItem(centerValue-value);
        updateIntroAndDot();
    }

    private void initView() {
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tv_intro = (TextView) findViewById(R.id.tv_intro);
        dot_layout= (LinearLayout) findViewById(R.id.dot_layout);
    }
    private void initDots(){
        for(int i = 0;i<list.size();i++){
            if(shape[0]==type){
                View view = new View(this);
                LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(16,16);
                if(i!=0){
                    param.leftMargin = 18;
                }
                view.setLayoutParams(param);
                view.setBackgroundResource(R.drawable.selector_dot_oval);
                dot_layout.addView(view);
            }else if(shape[1]==type){
                View view = new View(this);
                LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(32,12);
                if(i!=0){
                    param.leftMargin = 18;
                }
                view.setLayoutParams(param);
                view.setBackgroundResource(R.drawable.selector_dot_rect);
                dot_layout.addView(view);
            }else if(shape[2]==type){
                View view = new View(this);
                LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(16,16);
                if(i!=0){
                    param.leftMargin = 18;
                }
                view.setLayoutParams(param);
                view.setBackgroundResource(R.drawable.selector_dot_ring);
                dot_layout.addView(view);
            }

        }
    }

    private void updateIntroAndDot(){
        int currentPage = viewPager.getCurrentItem()%list.size();
        tv_intro.setText(list.get(currentPage).getIntro());
        for(int i = 0;i<dot_layout.getChildCount();i++){
            dot_layout.getChildAt(i).setEnabled(i==currentPage);
        }
    }

    class MyPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        /*
        返回true表示不去创建使用缓存,返回false表示重新创建
         */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        /*
        类似于BaseAdapter的getView方法
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = View.inflate(MainActivity.this,R.layout.adapter_ad,null);
            ImageView imageview = (ImageView) view.findViewById(R.id.image);
            Ad ad = list.get(position%list.size());

            imageview.setImageResource(ad.getIconResId());
            container.addView(view);
            return view;
        }

        /*
        销毁page
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
            container.removeView((View) object);
        }
    }
}
public class Ad {
    private int iconResId;
    private String intro;

    public Ad(int iconResId, String intro) {
        this.iconResId = iconResId;
        this.intro = intro;
    }

    public int getIconResId() {
        return iconResId;
    }

    public void setIconResId(int iconResId) {
        this.iconResId = iconResId;
    }

    public String getIntro() {
        return intro;
    }

    public void setIntro(String intro) {
        this.intro = intro;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="net.xiaolu.italker.bigmap.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="200dp">

    </android.support.v4.view.ViewPager>
    <LinearLayout
        android:layout_alignBottom="@id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#77000000"
        android:gravity="center_horizontal"
        android:padding="8dp"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/tv_intro"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textColor="@color/text_des_color"
            android:textSize="@dimen/text_des_size"
            android:lines="1"
            android:focusable="true"
            android:marqueeRepeatLimit="marquee_forever"
            android:ellipsize="marquee"
            android:text="@string/image_des_text"
            />
        <LinearLayout
            android:id="@+id/dot_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

        </LinearLayout>

    </LinearLayout>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@mipmap/a"
        />

</LinearLayout>

代码下载github 完整代码下载

猜你喜欢

转载自blog.csdn.net/qq_35114086/article/details/81587755
今日推荐