viewpager无限轮播

//

package com.example.viewpagertest2;



import java.util.ArrayList;
import java.util.List;


import com.example.adapter.MyAdapter;


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.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;


public class MainActivity extends Activity {
private ViewPager  viewPager;
private MyAdapter adapter;
    private List<View> viewList;
    private LayoutInflater layoutInflater;
    private LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager) findViewById(R.id.viewpager);
layout=(LinearLayout) findViewById(R.id.linearlayout);
initView();
// //发送消息
// myHandler.sendEmptyMessage(1);
}
/**
 * 初始化视图
 */
public void initView() {
viewList=new ArrayList<View>();
layoutInflater=LayoutInflater.from(MainActivity.this);
View view1=layoutInflater.inflate(R.layout.item, null);
view1.setBackgroundResource(R.drawable.meinv1);
View view2=layoutInflater.inflate(R.layout.item, null);
view2.setBackgroundResource(R.drawable.meinv2);
View view3=layoutInflater.inflate(R.layout.item, null);
view3.setBackgroundResource(R.drawable.meinv3);
View  view4=layoutInflater.inflate(R.layout.item, null);
view4.setBackgroundResource(R.drawable.meinv4);
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
viewList.add(view4);
adapter=new MyAdapter(viewList);
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
//将arg0换算到viewList的索引范围之内
int position=arg0%viewList.size();
//遍历所有的点对应的ImageView ,判断点的索引是否跟ViewPager当前的索引一致
for(int i=0;i<layout.getChildCount();i++){

ImageView imageView=(ImageView) layout.getChildAt(i);
if(i==position){
imageView.setSelected(true);
}else{
imageView.setSelected(false);
}
}

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}
});

//添加下面的圆点
for(int i=0;i<viewList.size();i++){
//动态实例化ImageView对象,添加到LinearLayout里面
ImageView imageView=new ImageView(MainActivity.this);
//手动代码设置间距
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 0, 5, 0);
//将当前属性设置给ImageView
imageView.setLayoutParams(params);
//给ImageView设置显示资源
imageView.setBackgroundResource(R.drawable.item_selector);
//将ImageView添加到LinearLayout里面
layout.addView(imageView);
//设置默认选中第一个
if(i==0){
imageView.setSelected(true);
}
}
}

/**
 * 更新显示UI
 */
public Handler myHandler =new Handler(){


@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch(msg.what){
case 1:
//获得ViewPager当前索引
int index=viewPager.getCurrentItem();
//当前页卡索引加1

//设置当前显示页卡
viewPager.setCurrentItem(index+=1);
//延迟发送消息,调用自己
myHandler.sendEmptyMessageDelayed(1, 2000);

break;
}


}




};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}



//Adapter

package com.example.adapter;


import java.util.List;


import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;


public class MyAdapter extends PagerAdapter{
List<View> mList;

public MyAdapter(List<View> list){
mList=list;
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
//将索引控制在mList索引范围之内
  position=position%mList.size();
 container.removeView(mList.get(position));
}



@Override
public Object instantiateItem(ViewGroup container, int position) {
//将索引控制在mList范围之内
position=position%mList.size();
// TODO Auto-generated method stub
container.addView(mList.get(position));
return  mList.get(position);
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;//最大的整数值
}


@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}


}






//布局

<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=".MainActivity" >


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


    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" 
       >
    </LinearLayout>


</RelativeLayout>



//item 

<?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" >
  


</LinearLayout>



//


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


    <item android:drawable="@drawable/over_select" android:state_selected="true"></item>
    <item android:drawable="@drawable/over_normal" android:state_selected="false"></item>


</selector>


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <!-- 描边 -->
    <stroke android:color="#ffffff"
        android:width="1dp"/>
    <!-- 填充 -->
    <solid android:color="#000000"/>
    <size android:width="10dp"
        android:height="10dp"/>


</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <!-- 描边 -->
    <stroke android:color="#ffffff"
        android:width="1dp"/>
    <!-- 填充 -->
    <solid android:color="#ff0000"/>
    <size android:width="10dp"
        android:height="10dp"/>


</shape>


猜你喜欢

转载自blog.csdn.net/yijiahuan/article/details/52648823