ViewPager轮播图

package com.example.week3.Fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.week3.R;

public class Five extends Fragment {

private View view;
private ViewPager viewpager;
private int []imgs=new int[]{R.drawable.a,R.drawable.b,R.drawable.c};
private int index=0;
private Handler mHandler=new Handler(){
    @Override
    public void handleMessage(Message msg) {

        index++;
        viewpager.setCurrentItem(index);
        mHandler.sendEmptyMessageDelayed(0,1000);
    }
};

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate( R.layout.five, null);
    initView(view);
    MyAdapter myAdapter = new MyAdapter();
    viewpager.setAdapter(myAdapter);
    mHandler.sendEmptyMessage(0);
    return view;
    //初始化数据

}

private void initView(View view) {
    viewpager = view.findViewById(R.id.viewpager);
}

class MyAdapter extends PagerAdapter{
@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 = new ImageView(getActivity());
imageView.setImageResource(imgs[position%imgs.length]);
container.addView(imageView);
return imageView;
}

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

}
FlyBanner的使用https://blog.csdn.net/weixin_39738488/article/details/78148421

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值