android应用上常用的轮播图(viewpager实现)

来源:互联网 发布:国内腊肉市场销售数据 编辑:程序博客网 时间:2024/06/04 18:30

点击打开链接资源文件下载

android进阶的一小步

费话不多说直接进入正题

每次项目用轮播图都要去网上找代码,特此自己写了一份来放着用。看得上的可以拿来,菜鸟级别,不喜勿喷

文章结尾上代码

由于项目需要binnar底部要显示文字

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <android.support.v4.view.ViewPager        android:id="@+id/viewPager"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <RelativeLayout        android:layout_width="match_parent"        android:layout_gravity="bottom"        android:background="#e1e1e1"        android:layout_height="wrap_content">        <TextView            android:id="@+id/binnarTitle"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textColor="#ffffff" />    </RelativeLayout></FrameLayout>

viewpager的关键就在于

BinnarAdapter
里面的
instantiateItem
方法

@Override        public Object instantiateItem(ViewGroup container, int position) {            position %= imageViewList.size();            View viewGroup = imageViewList.get(position);            if (viewGroup.getParent() == null) {                container.addView(viewGroup);            }            return imageViewList.get(position);        }
@Override        public void destroyItem(ViewGroup container, int position, Object object) {            position %= imageViewList.size();            container.recomputeViewAttributes(imageViewList.get(position));        }

imageViewList是传进来的轮播图的网络地址
public void setBinnarUrls(List<ImageBean> imgUrls, ImageBinnarListner imageBinnarListner) {        this.imgUrls = imgUrls;        if (imageViewList == null) {            return;        }        ImageBean imageBean = imgUrls.get(0);        binnarTitle.setText(imageBean.getTitle());        imageViewList.clear();        for (ImageBean imgUrl : imgUrls) {            ImageView imageView = new ImageView(context);            if (imageBinnarListner != null) {                imageBinnarListner.LoadImage(imageView, imgUrl.getImgUrl());            }            imageViewList.add(imageView);        }        binnarAdapter.notifyDataSetChanged();    }
这下手动滑动就没问题了。加载图片的方式用了一个回调在外面进行加载。

如果要想加viewpager自动滑动再用一个定时任务执行handler来完成

private void startAutoScroll() {        executor = Executors.newSingleThreadScheduledExecutor();        executor.scheduleAtFixedRate(new Runnable() {            @Override            public void run() {//                synchronized (this) {                    if (isPress) {//按下状态停止自动滚动                        return;                    }                    curIndex = viewPager.getCurrentItem();                    curIndex += 1;                    curIndex = curIndex >= imageViewList.size() ? 0 : curIndex;                    binnarHandler.obtainMessage().sendToTarget();                }//            }        }, INITIAL_DELAY, PERIOD, TimeUnit.SECONDS);    }

用到这步感觉viewpager自动切换 的时候动画根本 看不到。然后就有很多用放弃viewpager用ViewFlipper,暂时还没有去研究这个动画切换

调用些自定义binnar也就一个方法

imageBinnar.setBinnarUrls(urlList, new ImageBinnar.ImageBinnarListner() {            @Override            public void LoadImage(ImageView imageView, String imageUrl) {                Picasso.with(MainActivity.this)                        .load(imageUrl)                        .error(R.mipmap.ic_launcher)                        .into(imageView);            }        });


                                             
1 0
原创粉丝点击