这里是MainActivity .java
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
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.ListView;
public class MainActivity extends Activity {
private LinearLayout ll;
private ListView listView;
private ViewPager viewPager;
int[] images = {R.drawable.hzw1,
R.drawable.hzw2, R.drawable.hzw3, R.drawable.hzw4 };
List<ImageView> ivList = new ArrayList<ImageView>();
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
int num = msg.what;
switch (num) {
case 0:
viewPager.setAdapter(new MyPagerAdapter(MainActivity.this,
images, handler));
initDot();
viewPager.setCurrentItem(10000);
sendDelayedMessage();
ViewPagerListener();
break;
case 1:
int position = viewPager.getCurrentItem();
position++;
viewPager.setCurrentItem(position);
sendDelayedMessage();
break;
default:
break;
}
}
};
// 用handler来实现停留
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.ll);
listView = (ListView) findViewById(R.id.lv);
viewPager = (ViewPager) findViewById(R.id.vp);
new Thread(){
public void run() {
handler.sendEmptyMessage(0);
};
}.start();
}
//初始化点
private void initDot() {
if (ivList != null) {
ivList.clear();
}
for (int i = 0; i < images.length; i++) {
ImageView iv = new ImageView(this);
if (i == 0) {
//如果
iv.setImageResource(R.drawable.dian1);
} else {
iv.setImageResource(R.drawable.dian2);
}
LayoutParams params = new LayoutParams(10, 10);
//设置小圆点的大小
params.setMargins(5, 0, 5, 0);
ivList.add(iv);
ll.addView(iv, params);
}
}
//延时两秒去发送一个消息给handler
private void sendDelayedMessage() {
handler.sendEmptyMessageDelayed(1, 2000);
};
//小圆点的设置
private void ViewPagerListener() {
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
for (int i = 0; i < ivList.size(); i++) {
if (position % images.length == i) {
//viewpager的position和下面的占的位置相等,我们就让点变颜色
ivList.get(position % images.length)
.setImageResource(R.drawable.dian1);
} else {
ivList.get(i).setImageResource(R.drawable.dian2);
}
}
}
@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
}
});
}
}
</pre><pre class="java" name="code"><strong>以上是MainActivity.java</strong>
<strong></strong>
<strong></strong>
<strong></strong>
<strong></strong>
<strong>这里是activity_main</strong>
<strong>
</strong>
<strong>
</strong>
<strong>
</strong>
</pre><pre class="java" name="code">
<LinearLayout xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
xmlns:tools="<a target=_blank href="http://schemas.android.com/tools">http://schemas.android.com/tools</a>"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="200dp"
android:id="@+id/vp" />
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
></LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
<strong>这里是MyPagerAdapter</strong>
package com.bwei.lianxi;
import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MyPagerAdapter extends PagerAdapter {
private Context context;
private Handler handler;
private int[] images;
public MyPagerAdapter(Context context, int[] images, Handler handler) {
this.context = context;
this.images = images;
this.handler = handler;
}
@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;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view= View.inflate(context, R.layout.vp_item, null);
ImageView imageView=(ImageView) view.findViewById(R.id.imageView);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
handler.sendEmptyMessageDelayed(0, 3000);
break;
case MotionEvent.ACTION_DOWN:
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_CANCEL:
handler.sendEmptyMessageDelayed(0, 3000);
break;
default:
break;
}
return true;
}
});
imageView.setImageResource(images[position%images.length]);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
<strong>点的xml是两个颜色,只需要建两个xml在drawable文件夹下。。。</strong>
<strong>
</strong>
<strong>MyPagerAdapter中的vp_item是一个imageView 的xml</strong>
<strong>
</strong>
<strong>代码:</strong>
<strong>
</strong>
<strong><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</strong>
<strong></LinearLayout>
</strong>
版权声明:本文为zs1054707910原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。