package com.example.lunboxiaoyuandaun;
import java.util.ArrayList;
import java.util.List;
import com.baidu.adapter.PagerAdapter;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
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 int[] imas = {
R.drawable.lun1,
R.drawable.lun2
};
private ViewPager vp;
private List<ImageView> list_ima;
private List<View> dots;
int oldindex=0;
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==111){
int item = vp.getCurrentItem();
item++;
vp.setCurrentItem(item);
handler.sendEmptyMessageDelayed(111, 2000);
}
};
};
private LinearLayout line;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
line = (LinearLayout)findViewById(R.id.line);
initdata();
vp = (ViewPager) findViewById(R.id.vp);
vp.setAdapter(new PagerAdapter(imas, handler, MainActivity.this));
handler.sendEmptyMessageDelayed(111, 2000);
vp.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
for(int i=0 ;i <imas.length;i++){
if(i==arg0%imas.length){
list_ima.get(i).setBackgroundResource(R.drawable.an);
}else{
list_ima.get(i).setBackgroundResource(R.drawable.liang);
}
}
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private void initdata() {
// TODO Auto-generated method stub
list_ima = new ArrayList<ImageView>();
list_ima.clear();
line.removeAllViews();
for(int i=0;i<imas.length;i++){
ImageView ima_lin = new ImageView(MainActivity.this);
if(i==0){
ima_lin.setBackgroundResource(R.drawable.an);
}else{
ima_lin.setBackgroundResource(R.drawable.liang);
}
LayoutParams params = new LayoutParams(16, 16);
params.setMargins(5, 0, 5, 0);
list_ima.add(ima_lin);
line.addView(ima_lin, params);
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#f00"/>
<corners android:radius="8dip"/>
</shape>
<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"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
></android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="22dp"
android:layout_marginLeft="133dp"
android:gravity="center" >
</LinearLayout>
</RelativeLayout>
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" >
<View
android:id="@+id/doview"
android:layout_width="8dp"
android:layout_height="8dp"
android:layout_margin="15dp"
android:background="@drawable/an"
/>
</LinearLayout>