一、此处为页面布局
<LinearLayout 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:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv_des"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="wsedtgy"
android:gravity="center"/>
<LinearLayout
android:id="@+id/lin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
</LinearLayout>
</LinearLayout>
在viewpager下方对应展示viewpager内容的介绍和小圆点的对应,分别使用textview和linearlayout展示数据
<?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_logo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"/>
</LinearLayout>
viewpager内的布局文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 设置小圆点的半径 -->
<corners android:radius="8dp"/>
<!-- 设置小圆点为展示时的颜色 -->
<solid android:color="#000"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="8dp"/>
<solid android:color="#f00"/>
</shape>
小圆点的设置
二、代码的展示package com.example.day1601_viewpager;
import java.util.ArrayList;
import java.util.List;
import com.example.adapter.MyLogoPagerAdapter;
import com.example.bean.DataBean.Datas.DataList;
import com.example.utils.DataUtils;
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;
public class MainActivity extends Activity {
private ViewPager vp;
private TextView tv_des;
private LinearLayout lin;
private List<DataList> listData;
private List<ImageView> imgList = new ArrayList<ImageView>();
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int num = msg.what;
switch (num) {
case 0:
vp.setAdapter(new MyLogoPagerAdapter(MainActivity.this, listData, handler));
setdot();
vp.setCurrentItem(100000);
setsendEmptyMessageDelayed();
setPageChangeListener();
break;
case 1:
int position = vp.getCurrentItem();
position++;
vp.setCurrentItem(position);
setsendEmptyMessageDelayed();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager)findViewById(R.id.viewpager);
tv_des = (TextView)findViewById(R.id.tv_des);
lin = (LinearLayout)findViewById(R.id.lin);
gethttp();
}
/**
* 得到网络上的数据并用json解析出来后得到一个集合
*/
private void gethttp() {
new Thread(){
public void run() {
listData = DataUtils.getjson();
handler.sendEmptyMessage(0);
};
}.start();
}
/**
* 设置小圆点
*/
private void setdot() {
if (imgList!=null) {
imgList.clear();
}
for (int i = 0; i < listData.size(); i++) {
ImageView img_dot = new ImageView(this);
if (i==0) {
img_dot.setImageResource(R.drawable.dao_checked);
}else {
img_dot.setImageResource(R.drawable.dao_check);
}
tv_des.setText(listData.get(i).title);
LayoutParams params = new LayoutParams(20, 20);
params.setMargins(10, 0, 10, 0);
imgList.add(img_dot);
lin.addView(img_dot, params);
}
}
/**
* 让图片在一定的时间内自动轮播
*/
private void setsendEmptyMessageDelayed(){
handler.sendEmptyMessageDelayed(1, 2000);
}
/**
* 设置viewpager的滑动监听事件
*/
private void setPageChangeListener(){
vp.setOnPageChangeListener(new OnPageChangeListener() {
/*
* 此方法为滑动结束时(non-Javadoc)
* @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int)
*/
@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < imgList.size(); i++) {
if (arg0%listData.size()==i) {
imgList.get(arg0%listData.size()).setImageResource(R.drawable.dao_checked);
}else {
imgList.get(i).setImageResource(R.drawable.dao_check);
}
tv_des.setText(listData.get(arg0%listData.size()).title);
}
}
@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
}
});
}
}
adapter的配置
package com.example.adapter;
import java.util.List;
import com.example.bean.DataBean.Datas.DataList;
import com.example.day1601_viewpager.R;
import com.nostra13.universalimageloader.core.ImageLoader;
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 MyLogoPagerAdapter extends PagerAdapter {
private Context context;
private List<DataList> listData;
private Handler handler;
public MyLogoPagerAdapter(Context context, List<DataList> listData, Handler handler) {
this.context = context;
this.listData = listData;
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 void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = View.inflate(context, R.layout.viewpager, null);
ImageView img_logo = (ImageView)view.findViewById(R.id.img_logo);
/**
* 给img添加一个触发事件的监听
*/
img_logo.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_UP:
handler.sendEmptyMessageDelayed(1, 2000);
break;
case MotionEvent.ACTION_CANCEL:
handler.sendEmptyMessageDelayed(1, 2000);
break;
default:
break;
}
return true;
}
});
//imageloader缓存图片
ImageLoader.getInstance().displayImage(listData.get(position%listData.size()).img, img_logo);
container.addView(view);
return view;
}
}
三、此处为imageloader配置
package com.example.application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import android.app.Application;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration loader = new ImageLoaderConfiguration.Builder(
getApplicationContext()).memoryCacheSize(2*1024*1024).build();
ImageLoader.getInstance().init(loader);
}
}
需要在配置文件中添加MyApplication的配置name