无限轮播

来源:互联网 发布:手机怎么注册淘宝网 编辑:程序博客网 时间:2024/05/17 06:52

无限轮播

在MainActivity中
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

String[] paths = new String[] {            "http://img1.3lian.com/2015/w7/98/d/22.jpg",            "http://img1.3lian.com/2015/w7/98/d/1.jpg",            "http://img1.3lian.com/2015/w7/98/d/2.jpg",            "http://img1.3lian.com/2015/w7/98/d/3.jpg",            "http://img1.3lian.com/2015/w7/98/d/4.jpg",               "http://img1.3lian.com/2015/w7/98/d/21.jpg", };    Handler handler=new Handler(){        public void handleMessage(android.os.Message msg) {            //得到当前的位置            int currentItem = viewPager.getCurrentItem();            viewPager.setCurrentItem(currentItem+=1);            //继续发送,轮播            handler.sendEmptyMessageDelayed(2, 2000);        };    };    private ViewPager viewPager;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        viewPager = (ViewPager) findViewById(R.id.viewPager);        viewPager.setAdapter(new MyPagerAdapter(this,paths));        //设置viewpager当前显示的条目(默认显示的条目)        viewPager.setCurrentItem(Integer.MAX_VALUE/2 - Integer.MAX_VALUE/2%paths.length );        handler.sendEmptyMessageDelayed(2, 2000);    }

}

在适配器中

import com.lidroid.xutils.BitmapUtils;

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

public class MyAdapter extends PagerAdapter {

private Context

context;
private String[] path;
private BitmapUtils bitmapUtils;

public MyAdapter(Context context, String[] path) {    this.context = context;    // TODO Auto-generated constructor stub    this.path = path;    bitmapUtils = new BitmapUtils(context);}@Overridepublic int getCount() {    // TODO Auto-generated method stub    return Integer.MAX_VALUE;}@Overridepublic Object instantiateItem(ViewGroup container, int position) {    // TODO Auto-generated method stub    View v = View.inflate(context, R.layout.item, null);    ImageView img = (ImageView) v.findViewById(R.id.img);    bitmapUtils.display(img, path[position % path.length]);    container.addView(v);    return v;}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {    // TODO Auto-generated method stub    return arg0 == arg1;}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {    // TODO Auto-generated method stub    // super.destroyItem(container, position, object);    container.removeView((View) object);}

}

**在activity_main.xml中**
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"`这里写代码片`    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.lunbo.MainActivity" >    <android.support.v4.view.ViewPager        android:id="@+id/viewpagr"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >    </android.support.v4.view.ViewPager></RelativeLayout>在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/img"        android:layout_width="fill_parent"        android:layout_height="fill_parent" /></LinearLayout> 
0 0
原创粉丝点击