仿百度壁纸客户端(二)——主页自定义ViewPager广告定时轮播图

简介: <div class="markdown_views"><h1 id="仿百度壁纸客户端二主页自定义viewpager广告定时轮播图">仿百度壁纸客户端(二)——主页自定义ViewPager广告定时轮播图</h1><hr><h2 id="百度壁纸系列">百度壁纸系列</h2><blockquote> <p><a href="http://blog.csdn.ne

仿百度壁纸客户端(二)——主页自定义ViewPager广告定时轮播图


百度壁纸系列

仿百度壁纸客户端(一)——主框架搭建,自定义Tab + ViewPager + Fragment

仿百度壁纸客户端(二)——主页自定义ViewPager广告定时轮播图

仿百度壁纸客户端(三)——首页单向,双向事件冲突处理,壁纸列表的实现

仿百度壁纸客户端(四)——自定义上拉加载实现精选壁纸墙

仿百度壁纸客户端(五)——实现搜索动画GestureDetector手势识别,动态更新搜索关键字

仿百度壁纸客户端(六)——完结篇之Gallery画廊实现壁纸预览已经项目细节优化


搭建完框架,我们来实现首页的轮播图

vp_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    >

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

    <LinearLayout
        android:alpha="0.5"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:background="#9000"
        android:layout_width="match_parent"
        android:layout_height="30dp">


        <ImageView
            android:id="@+id/imgOne"
            android:src="@mipmap/point_selected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:id="@+id/imgTwo"
            android:src="@mipmap/point_normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:layout_marginRight="10dp"
            android:id="@+id/imgThree"
            android:src="@mipmap/point_normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/imgFour"
            android:src="@mipmap/point_normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


    </LinearLayout>

</RelativeLayout>

这就是大体的样子,我们再写个图片的item,里面啥也没有,就一个imageview

vp_scroll_item.xml

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

    <ImageView
        android:id="@+id/vpImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

然后我们就可以编写自定义的组合控件了

VPScrollLayout

package com.lgl.baiduwallpaper.view;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lgl.baiduwallpaper.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 广告轮播
 * Created by lgl on 16/3/31.
 */
public class VPScrollLayout extends LinearLayout {

    private ViewPager viewpager;
    private ImageView imgOne, imgTwo, imgThree, imgFour;
    //计时器
    private Timer timer;
    private TimerTask timerTask;

    private static final int ReFish = 10;


    //标记,当前页面滑动的位置
    private int index = 0;

    //构造方法
    public VPScrollLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View views = inflater.inflate(R.layout.vp_item, this);
        findView(views);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                index = position;
                hand();

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 找控件
     *
     * @param views
     */
    private void findView(View views) {
        viewpager = (ViewPager) views.findViewById(R.id.viewpager);
        imgOne = (ImageView) views.findViewById(R.id.imgOne);
        imgTwo = (ImageView) views.findViewById(R.id.imgTwo);
        imgThree = (ImageView) views.findViewById(R.id.imgThree);
        imgFour = (ImageView) views.findViewById(R.id.imgFour);
    }

    /**
     * 轮播效果
     */
    public void setPagerFromTime(int dalayTime) {
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                //逻辑
//                initPic(index);
                hand();

                //没运行一次+1
                index++;
                //循环轮播
                if (index == 4) {
                    index = 0;
                }
            }
        };
        //隔几秒更新
        timer.schedule(timerTask, dalayTime, dalayTime);
    }



    //更新UI
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ReFish:
                    //获取数据
                    int index = msg.getData().getInt("index");
                    Log.i("Index",index+"");
                    initPic(index);
                    break;
            }
        }
    };

    /**
     * 设置轮播,定时更新页面
     *
     * @param indexs
     */
    private void initPic(int indexs) {
        viewpager.setCurrentItem(indexs);
        Log.i("Indexs", indexs + "");
        switch (indexs) {
            case 0:
                imgOne.setBackgroundResource(R.mipmap.point_selected);
                imgTwo.setBackgroundResource(R.mipmap.point_normal);
                imgThree.setBackgroundResource(R.mipmap.point_normal);
                imgFour.setBackgroundResource(R.mipmap.point_normal);

                break;
            case 1:
                imgOne.setBackgroundResource(R.mipmap.point_normal);
                imgTwo.setBackgroundResource(R.mipmap.point_selected);
                imgThree.setBackgroundResource(R.mipmap.point_normal);
                imgFour.setBackgroundResource(R.mipmap.point_normal);

                break;
            case 2:
                imgOne.setBackgroundResource(R.mipmap.point_normal);
                imgTwo.setBackgroundResource(R.mipmap.point_normal);
                imgThree.setBackgroundResource(R.mipmap.point_selected);
                imgFour.setBackgroundResource(R.mipmap.point_normal);

                break;
            case 3:
                imgOne.setBackgroundResource(R.mipmap.point_normal);
                imgTwo.setBackgroundResource(R.mipmap.point_normal);
                imgThree.setBackgroundResource(R.mipmap.point_normal);
                imgFour.setBackgroundResource(R.mipmap.point_selected);

                break;
        }
    }

    public ViewPager getViewPager() {
        //调用
        return viewpager;
    }

    /**
     * 发送消息
     */
    private void hand(){
        Bundle bundle = new Bundle();
        bundle.putInt("index", index);
        Message msg = new Message();
        msg.setData(bundle);
        msg.what = ReFish;
        handler.sendMessage(msg);
    }

}

写完这个之后,我们就可以在主页应用了

home_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.lgl.baiduwallpaper.view.VPScrollLayout
        android:id="@+id/vp_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

我们可以在HomeFragment里直接用了

HomeFragment

package com.lgl.baiduwallpaper.fragment;

import android.os.Bundle;
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 com.lgl.baiduwallpaper.R;
import com.lgl.baiduwallpaper.view.VPScrollLayout;

import java.util.ArrayList;

/**
 * 主页
 * Created by lgl on 16/3/31.
 */
public class HomeFragment extends Fragment {

    private VPScrollLayout vpScroll;
    private ViewPager myViewPager;
    private ArrayList<View> bitmap = new ArrayList<View>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);
        findView(view);
        return view;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        initVPData();
        myViewPager.setAdapter(new MyAdapter());
        //设置几秒轮播
        vpScroll.setPagerFromTime(1000);
    }

    /**
     * 初始化图片
     */
    private void initVPData() {
        LayoutInflater inflater1 = getActivity().getLayoutInflater();
        View view1 = inflater1.inflate(R.layout.vp_seroll_item, null);
        view1.findViewById(R.id.vpImg).setBackgroundResource(R.mipmap.img1);
        bitmap.add(view1);

        LayoutInflater inflater2 = getActivity().getLayoutInflater();
        View view2 = inflater2.inflate(R.layout.vp_seroll_item, null);
        view2.findViewById(R.id.vpImg).setBackgroundResource(R.mipmap.img2);
        bitmap.add(view2);

        LayoutInflater inflater3 = getActivity().getLayoutInflater();
        View view3 = inflater3.inflate(R.layout.vp_seroll_item, null);
        view3.findViewById(R.id.vpImg).setBackgroundResource(R.mipmap.img3);
        bitmap.add(view3);

        LayoutInflater inflater4 = getActivity().getLayoutInflater();
        View view4 = inflater4.inflate(R.layout.vp_seroll_item, null);
        view4.findViewById(R.id.vpImg).setBackgroundResource(R.mipmap.img4);
        bitmap.add(view4);
    }

    /**
     * 绑定
     *
     * @param view
     */
    private void findView(View view) {
        vpScroll = (VPScrollLayout) view.findViewById(R.id.vp_scroll);
        //直接拿到
        myViewPager = vpScroll.getViewPager();

    }

    /**
     * adapter
     */
    private class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return bitmap.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {

            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
            //删除
            ((ViewPager) container).removeView(bitmap.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager) container).addView(bitmap.get(position));

            return bitmap.get(position);
        }
    }
}

这里注意的是你必须要继承的是V4的fragment,而且这个有个bug,就是小圆点不动,逻辑是没有写错,那究竟是什么原因尼?

这里写图片描述

OK,其实应该是AS的原因吧,你只要在vp_item.xml中,不引用图片,就像这样

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:alpha="0.5"
        android:background="#9000"
        android:gravity="center"
        android:orientation="horizontal">


        <ImageView
            android:id="@+id/imgOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/imgTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <ImageView
            android:id="@+id/imgThree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp" />

        <ImageView
            android:id="@+id/imgFour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


    </LinearLayout>

</RelativeLayout>

现在我们运行一下

这里写图片描述

这个是个完整的项目,看下去对自己的帮助很大哟,我也在写,可能陈述的就不是很清晰的条例=理了,可能初学者确实会有点小困难,不过慢慢看下去,总会有收货的,考虑到轮播图,应该也有人需要,所以,这里我把Demo上传吧!

Demo下载:http://download.csdn.net/detail/qq_26787115/9480067

目录
相关文章
|
2月前
|
监控 安全 网络安全
等保 2.0 到底是个啥?开发者版人话解读
等保 2.0 是标准版本号不是安全级别,很多人连这都搞混。本文用人话拆解等保定级备案五步流程、二级三级测评周期、云上责任共担最容易踩的坑,再给出测评机构最爱抠分的五个技术自查项:日志留存、弱口令、安全审计、边界防护、集中管控。测评前先模拟考,返工费能省一大笔。
408 2
|
2月前
|
弹性计算 固态存储
阿里云服务器8核16G配置ECS实例规格族、收费标准及2026最新价格参考
本文详解阿里云8核16G云服务器ECS的2026年最新价格,涵盖经济型e、计算型c9i、通用型u2a等多规格族的按小时/月/年/3年/5年计费标准,并说明公网带宽与系统盘(ESSD等)费用组成。阿里云服务器ECS官网:https://t.aliyun.com/U/AZBUsA
319 1
|
5月前
|
运维 监控 安全
2026 多Agent协同落地深度实践指南
本文为《2026多Agent协同落地深度实践指南》,聚焦企业级多Agent协同难题:外部Agent能力孤立、上下文无法流转、数据无留痕与权限失控。提出“协同层+专业Agent”分工架构,通过飞书aily等底座实现研报生成、代码闭环、日志排障三大场景自动串联,提升效率60%。强调小步验证、安全前置、不替代专业能力三大原则。(239字)
|
6月前
|
安全 网络协议 网络安全
专网边界安全治理:跨网接入检测、一机多网管控与违规子网发现的技术架构
本文阐述专网安全从“物理隔离”迈向“逻辑感知”的范式升级,聚焦跨网接入检测、一机多网管控、违规子网发现三大核心能力。通过主动探测+被动监听、NDIS驱动级拦截、多维拓扑分析等技术,实现无Agent发现、实时定位、自动处置与根因推断,构建覆盖网络层与终端层的纵深防御闭环。(239字)
|
人工智能 供应链 算法
创新场景丨从数字化到智能化,中远海科“航运新基建”探路
随着数据治理和 AI 大模型应用深入,船视宝日趋完善,开始发挥航运业数智新底座的作用,距离中远海科的愿景也更近一步。
|
12月前
|
存储 缓存 运维
【服务器数据恢复】互联网短视频平台高并发存储故障数据恢复案例
金海境科技专业从事数据中心数据恢复技术研发、产品销售和运维技术服务的高新技术企业。业务包含服务器数据恢复、分布式数据恢复、数据库数据恢复公司以IDC数据中心为核心业务方向,拥有文件系统底层架构解析核心技术,为超融合、虚拟化云平台、分布式存储、数据库以及勒索病毒加密等场景下的数据丢失提供数据恢复解决方案。
399 2
|
JSON 自然语言处理 API
随机昵称姓名生成API:百亿级免费接口深度指南
接口盒子提供的“随机昵称姓名网名游戏名API”是一款强大名称生成工具,支持游戏ID、社交昵称等多场景应用。基于海量姓氏与词库,可生成百亿级不重复名称,具备高稳定性和完全免费特性,适用于各类开发需求。
1008 0
|
存储 监控 调度
云服务器成本优化深度解析与实战案例
本文深入探讨了云服务器成本优化的策略与实践,涵盖基本原则、具体策略及案例分析。基本原则包括以实际需求为导向、动态调整资源、成本控制为核心。具体策略涉及选择合适计费模式、优化资源配置、存储与网络配置、实施资源监控与审计、应用性能优化、利用优惠政策及考虑多云策略。文章还通过电商、制造企业和初创团队的实际案例,展示了云服务器成本优化的有效性,最后展望了未来的发展趋势,包括智能化优化、多云管理和绿色节能。
使用OpenDaylight界面下发流表(下)
使用OpenDaylight界面下发流表(下)
854 0
|
算法 数据挖掘
Faiss为啥这么快?原来是量化器在做怪!2
Faiss为啥这么快?原来是量化器在做怪!
1789 1