手写无限轮播

      //  无限轮播

布局文件

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   
<android.support.v4.view.ViewPager
    android:id="@+id/vp"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    />
<LinearLayout
    android:id="@+id/line"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:layout_alignBottom="@+id/vp"
    />
</RelativeLayout>
代码实现

public class MyBase extends PagerAdapter{
    private ShouyeFragment context;
    private ArrayList<ImageView> arrayList;

    public MyBase(ShouyeFragment context, ArrayList<ImageView> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }
    public int getCount() {
        return Integer.MAX_VALUE;
    }
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0==arg1;
    }
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView=arrayList.get(position%arrayList.size());
        container.addView(imageView);
        return imageView;
    }
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
Activity

public class ShouyeFragment extends Fragment {
    private ArrayList<ImageView> arrayList2;
    private ArrayList<ImageView> arrayList;
    private DisplayImageOptions displayImageOptions;
    private String[] image = new String[] {
            "http://l2.51fanli.net//tuan//images//1//5806eac956808.jpg",
            "http://l2.51fanli.net//tuan//images//b//580991bb30560.jpg",
            "http://l0.51fanli.net//tuan//images//b//58115f2593dc3.jpg",
            "http://l2.51fanli.net//tuan//images//0//57923840b054d.jpg",
            "http://l2.51fanli.net//tuan//images//e//58101e11ab164.jpg" };
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    private ViewPager vp;
    private LinearLayout layout;
    private Handler handler=new Handler(){
        public void handleMessage(Message msg) {
            int currentItem = vp.getCurrentItem();
            currentItem++;
            vp.setCurrentItem(currentItem);
        }

    };
   
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_shouye, container, false);
        vp = (ViewPager) view.findViewById(R.id.vp);
        layout = (LinearLayout) view.findViewById(R.id.line);
        Setdata();
        SetdataYuan();
        vp.setAdapter(new MyBase(this,arrayList));
        handler.sendEmptyMessageDelayed(1,2000);
        //viewpage设置监听
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageSelected(int arg0) {
                for (int i = 0; i < image.length; i++) {
                    if (arg0%arrayList2.size()==i) {
                        arrayList2.get(i).setImageResource(R.drawable.a2);
                    }else {
                        arrayList2.get(i).setImageResource(R.drawable.a1);
                    }
                }
            }
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        return view;
    }
    //给view赋值的方法
    private void Setdata() {
        displayImageOptions = new  DisplayImageOptions.Builder().build();
        arrayList = new ArrayList<ImageView>();
        for (int i = 0; i <image.length; i++) {
            ImageView imageView=new ImageView(getActivity());
            ImageLoader.getInstance().displayImage(image[i],imageView,displayImageOptions);
            arrayList.add(imageView);
        }
    }
    //小圆点赋值
    @SuppressLint("NewApi")
    private void SetdataYuan() {
        arrayList2 = new ArrayList<>();
        for (int i = 0; i < image.length; i++) {
            ImageView imageView=new ImageView(getActivity());
            if (i==0) {
                imageView.setImageResource(R.drawable.a2);
            }else {
                imageView.setImageResource(R.drawable.a1);
            }
            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(20,20);
            layoutParams.setMargins(5, 0, 5, 0);
            layout.addView(imageView, layoutParams);
            arrayList2.add(imageView);
        }
    }

}
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="8dp" />
    <solid android:color="#cccccc"/>

</shape>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值