• 大小: 1.16M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2020-12-23
  • 标签: 轮播  wp  广告  

资源简介

MainActivity.class

package com.example.viewpagerview;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

/**
 * 首页图片轮播
 * 作者:yangfang
 * @author Administrator 
 */
public class FinishActivity extends Activity {
private ViewPager viewPager;
private TextView text;
private LinearLayout layout;
/**
* 用于小圆点的图片
*/
private List<ImageView> dotlist;
/**
* 用于轮播的图片
*/
private List<ImageView> imglist;
private int currentItem = 0;// 当前页面
private boolean isAutoPlay = true;// 是否自动轮播
private ScheduledExecutorService scheduledExecutorService;
private String title[] = { "第一张图片", "第二张图片", "第三张图片", "第四张图片", "第五张图片" };
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 100) {
viewPager.setCurrentItem(currentItem);
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finish);
viewPager = (ViewPager) findViewById(R.id.viewpager);
text = (TextView) findViewById(R.id.text);
layout = (LinearLayout) findViewById(R.id.dian);
layout.removeAllViews();
initView();
if (isAutoPlay) {
startPlay();
}
}

/**
* 开始轮播图切换
*/
private void startPlay() {
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate(new SlideShowTask(), 1, 5,
TimeUnit.SECONDS);
// 根据他的参数说明,第一个参数是执行的任务,第二个参数是第一次执行的间隔,第三个参数是执行任务的周期;
}

/**
* 执行轮播图切换任务

*/
class SlideShowTask implements Runnable {

@Override
public void run() {

synchronized (viewPager) {
currentItem = (currentItem 1) % imglist.size();
mHandler.sendEmptyMessage(100);
}
}
}

private void initView() {
dotlist = new ArrayList<ImageView>();
imglist = new ArrayList<ImageView>();
if (imglist.size() == dotlist.size()) {

for (int i = 0; i < 5; i ) {
ImageView dotView = new ImageView(FinishActivity.this);
LayoutParams params = new LayoutParams(20, 20);
params.leftMargin = 20;// 设置小圆点的外边距
// params.rightMargin = 6;
/*
* params.height = 20;// 设置小圆点的大小 params.width = 20;
*/
dotView.setLayoutParams(params);
// dotView.setEnabled(false);

dotView.setBackgroundResource(R.drawable.pointer_selector);
layout.addView(dotView, params);
dotlist.add(dotView);// 动态添加四个点
text.setText(title[0]);
}
}
ImageView img1 = new ImageView(this);
ImageView img2 = new ImageView(this);
ImageView img3 = new ImageView(this);
ImageView img4 = new ImageView(this);
ImageView img5 = new ImageView(this);
img1.setBackgroundResource(R.drawable.b1);
img2.setBackgroundResource(R.drawable.b2);
img3.setBackgroundResource(R.drawable.b3);
img4.setBackgroundResource(R.drawable.b4);
img5.setBackgroundResource(R.drawable.b5);
imglist.add(img1);
imglist.add(img2);
imglist.add(img3);
imglist.add(img4);
imglist.add(img5);
// viewPager.getChildAt(0).setEnabled(true);
FinishAdapter adapter = new FinishAdapter(this, imglist);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
/**
* ViewPager的监听器 当ViewPager中页面的状态发生改变时调用

*/
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
boolean isAutoPlay = false;

@Override
public void onPageSelected(int arg0) {
// 这里面动态改变小圆点的被背景,来实现效果
currentItem = arg0;
for (int i = 0; i < dotlist.size(); i ) {
if (i == arg0) {
dotlist.get(arg0).setBackgroundResource(
R.drawable.pointer_focus);
text.setText(title[arg0]);
} else {
dotlist.get(i).setBackgroundResource(
R.drawable.pointer_disable);
}
}
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageScrollStateChanged(int arg0) {
switch (arg0) {
case 1:// 手势滑动空闲中
isAutoPlay = false;
System.out.println("手势滑动空闲中");
break;

case 2:// 界面切换
isAutoPlay = true;
System.out.println("界面切换中");
break;
case 0:// 切换结束,加载完毕
// 当前为最后一张,此时从右向左滑,则切换到第一张
if (viewPager.getCurrentItem() == viewPager.getAdapter()
.getCount() - 1 && !isAutoPlay) {
viewPager.setCurrentItem(0);
System.out.println("滑动到最后一张");
}
// // 当前为第一张,此时从左向右滑,则切换到最后一张
else if (viewPager.getCurrentItem() == 0 && !isAutoPlay) {
viewPager.setCurrentItem(viewPager.getAdapter()
.getCount() - 1);
System.out.println("滑动到第一张");
}
break;
case 3:
break;
}

}
});

}
}


Adapter.class:

package com.example.viewpagerview;

import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

public class FinishAdapter extends PagerAdapter {
private Context context;
private List<ImageView> mList;

public FinishAdapter(Context context, List<ImageView> mList) {
this.context = context;
this.mList = mList;
}

/**
* 集合的数量
*/
@Override
public int getCount() {

return mList.size();
}

/**
* 返回的参数
*/
@Override
public boolean isViewFromObject(View arg0, Object arg1) {

return arg0 == arg1;
}

/**
* 生成视图
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView img = mList.get(position);
ViewParent vp = img.getParent();
if (vp != null) {
ViewPager parent = (ViewPager) vp;
parent.removeView(img);
}
container.addView(mList.get(position));
return mList.get(position);
}

/**
* 销毁视图 在这里super.destroyItem(container, position,
* object);这句不需要,但是可以加上:container.removeView(mList.get(position));
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {

// super.destroyItem(container, position, object);
// container.removeView(mList.get(position));
}
}



资源截图

代码片段和文件信息

/** Automatically generated file. DO NOT MODIFY */
package com.example.viewpager;

public final class BuildConfig {
    public final static boolean DEBUG = true;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        364  2015-12-16 14:49  ViewPager图片轮播\.classpath

     文件        857  2015-12-16 14:49  ViewPager图片轮播\.project

     文件        177  2015-12-16 14:49  ViewPager图片轮播\.settings\org.eclipse.jdt.core.prefs

     文件        882  2015-12-16 14:49  ViewPager图片轮播\AndroidManifest.xml

     文件        882  2015-12-16 14:49  ViewPager图片轮播\bin\AndroidManifest.xml

     文件        351  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\BuildConfig.class

     文件       1149  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\MainActivity$1.class

     文件       1776  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\MainActivity$2.class

     文件       4542  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\MainActivity.class

     文件       2055  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\MyPagerAdapter.class

     文件        349  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\R$attr.class

     文件        436  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\R$color.class

     文件        470  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\R$dimen.class

     文件        662  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\R$drawable.class

     文件        493  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\R$id.class

     文件        412  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\R$layout.class

     文件        397  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\R$menu.class

     文件        509  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\R$string.class

     文件        440  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\R$style.class

     文件        754  2015-12-16 14:49  ViewPager图片轮播\bin\classes\com\example\viewpager\R.class

     文件     455044  2015-12-16 14:49  ViewPager图片轮播\bin\classes.dex

     文件     151838  2015-12-16 14:49  ViewPager图片轮播\bin\dexedLibs\android-support-v4-17c814e2f56f89d92f940ed460c6c1b6.jar

     文件       5964  2015-12-16 14:49  ViewPager图片轮播\bin\res\drawable-hdpi\ic_launcher.png

     文件       3112  2015-12-16 14:49  ViewPager图片轮播\bin\res\drawable-mdpi\ic_launcher.png

     文件       9355  2015-12-16 14:49  ViewPager图片轮播\bin\res\drawable-xhdpi\ic_launcher.png

     文件      17889  2015-12-16 14:49  ViewPager图片轮播\bin\res\drawable-xxhdpi\ic_launcher.png

     文件     151133  2015-12-16 14:49  ViewPager图片轮播\bin\resources.ap_

     文件     308563  2015-12-16 14:49  ViewPager图片轮播\bin\ViewPager图片轮播.apk

     文件        163  2015-12-16 14:49  ViewPager图片轮播\gen\com\example\viewpager\BuildConfig.java

     文件       3279  2015-12-16 14:49  ViewPager图片轮播\gen\com\example\viewpager\R.java

............此处省略68个文件信息

评论

共有 条评论