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

仿百度壁纸客户端(二)——主页自定义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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<h3>回答1:</h3><br/>Android Studio可以使用ViewPager和Fragment实现轮播图。具体步骤如下: 1. 创建一个新的Android Studio项目。 2. 在布局文件中添加ViewPager和指示器(可以使用第三方库或自定义View实现)。 3. 创建一个Fragment,用于显示轮播图的内容。 4. 创建一个PagerAdapter,用于管理Fragment的显示。 5. 在Activity中设置ViewPager的Adapter,并启动自动轮播。 6. 在Fragment中加载轮播图的图片和文字等内容。 7. 可以使用第三方库或自定义View实现轮播图的动画效果。 以上是实现轮播图的基本步骤,具体实现可以参考相关教程或示例代码。 <h3>回答2:</h3><br/>Android studio是一款Android应用开发的IDE,它提供了许多开发工具和API,为开发人员提供了许多便利。在实现轮播图方面,Android studio可以使用几种不同的方法,包括ViewPager、ImageSwitcher和ViewFlipper。 ViewPager是Android自带的一种视图容器,可以在其中嵌套多个视图,在应用中实现轮播图时常用到。使用ViewPager可以通过定义一个PagerAdapter来实现对AdapterView的管理,PagerAdapter可以实现多张轮播图片的循环播放,适配器可以包含多种界面视图,例如ImageView、TextView等。通过实现PagerAdapter的getCount方法和getView方法,可以自定义实现图片的显示效果。同时,在该方法中,我们还需要管理轮播图的滑动、缓存等状态。 ImageSwitcher是一种专门用于显示图片的视图容器,其可以通过设置转换效果来实现图片的过渡效果。ImageSwitcher的使用需要定义一个ImageSwitcher对象,然后通过实现一个ViewFactory对象来实现对ImageSwitcher的管理。在该ViewFactory对象中,需要通过实现makeView方法来定义视图效果。 ViewFlipper是一种既可以显示文本视图,又可以显示图片视图的视图容器,通过实现ViewFlipper对象和定义动画效果来实现轮播图的显示效果。在使用ViewFlipper时,我们可以通过设置属性值来控制视图的逐渐隐藏和逐渐显示的效果。此外,也可以通过设置动画效果来控制视图滑动的方式。 总之,Android Studio提供了很多可以用于实现轮播图的方法,开发人员可以根据自身的需求选择适合自己的方式来实现。无论使用哪种方法,都需要在代码实现中,保证广告展示正常、稳定,不会影响应用的性能和用户使用体验。 <h3>回答3:</h3><br/>Android Studio是一个流行的集成开发环境,可用于开发Android应用程序。轮播图是一个很好的UI特性,可以在应用程序中增加动态的交互性。在Android Studio中,可以使用ViewPager、Fragment和Timer类来实现轮播图。 首先,我们需要添加ViewPager和Fragment支持库到我们的build.gradle文件,以便在应用程序中使用这些组件。 ``` dependencies { implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:support-v4:28.0.0' } ``` 接下来,在布局文件中添加ViewPager元素。 ``` <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` 然后,我们需要创建一个Fragment类来显示每个轮播图中的内容。我们可以创建一个简单的布局文件,并在Fragment中加载它。 ``` public class MyFragment extends Fragment { private int imageId; public MyFragment() {} @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_layout, container, false); ImageView imageView = view.findViewById(R.id.imageView); imageView.setImageResource(imageId); return view; } // Set image resource id for the fragment public void setImageId(int imageId) { this.imageId = imageId; } } ``` 最后,我们需要在MainActivity类中创建一个ViewPager适配器,并将其与ViewPager相关联。在适配器中,我们需要设置Fragment的数量和每个Fragment显示的内容。 ``` public class MainActivity extends AppCompatActivity { private int[] imageIds = {R.drawable.image1, R.drawable.image2, R.drawable.image3}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = findViewById(R.id.viewPager); viewPager.setAdapter(new MyAdapter(getSupportFragmentManager())); } private class MyAdapter extends FragmentPagerAdapter { public MyAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { MyFragment fragment = new MyFragment(); fragment.setImageId(imageIds[position]); return fragment; } @Override public int getCount() { return imageIds.length; } } } ``` 现在我们已经成功地实现了一个简单的轮播图。为了使轮播图自动滚动,我们可以使用Timer类在一定的时间间隔内切换Fragment。这涉及到一些额外的编程挑战,具体实现方法可以根据需求来灵活处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值