欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

轮播图

程序员文章站 2023-12-30 15:15:58
...

xml界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_gravity="center_horizontal"
        android:id="@+id/view_pager"
        />
    <RadioGroup
        android:layout_marginTop="15dp"
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" />
    </RadioGroup>
</LinearLayout>

main界面

 private ViewPager viewPager;
    private RadioGroup radioGroup;
    private int[] str={R.drawable.a1,R.drawable.ada,R.drawable.asa};
    ArrayList<ImageView> list=new ArrayList<>();
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager=findViewById(R.id.view_pager);
        //手指滑动时就不在自动轮播啦
        radioGroup=findViewById(R.id.rg);
        for (int i=0;i<str.length;i++){
            ImageView imageView=new ImageView(MainActivity.this);
            imageView.setBackgroundResource(str[i]);
            list.add(imageView);
        }
        viewPager.setAdapter(new MyAdapter(list,MainActivity.this));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        handler.sendEmptyMessageDelayed(0,2000);
    }

hander

Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    int currentitem=viewPager.getCurrentItem();
                    viewPager.setCurrentItem(currentitem+1);
                    radioGroup.check(currentitem%list.size()+1);
                    handler.sendEmptyMessageDelayed(0,2000);
                    break;
            }
        }
    };

adapter

package com.example.demo6;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

public class MyAdapter extends PagerAdapter{
    private ArrayList<ImageView> list;
    private Context context;

    public MyAdapter(ArrayList<ImageView> list, Context context) {
        this.list = list;
        this.context = context;
    }

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

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

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView imageView=list.get(position%list.size());
        if (imageView.getParent()!=null){
            ((ViewPager)imageView.getParent()).removeView(imageView);
        }
        ((ViewPager)container).addView((View) imageView,0);
        return imageView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);
    }
}

上一篇:

下一篇: