关闭

ViewPager Picture Dots

标签: viewpager布局
82人阅读 评论(0) 收藏 举报
分类:

用线性布局添加dots  

<android.support.v4.view.ViewPager 

android:layout_height="200dp" 

android:layout_width="match_parent" 

android:id="@+id/viewPager"/>


<LinearLayout 

android:layout_height="wrap_content" 

android:layout_width="match_parent" 

android:id="@+id/ll_dots" 

android:layout_alignBottom="@id/viewPager" 

android:gravity="center"> 

</LinearLayout>


package com.example.roolpage;



import java.util.ArrayList;


import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;


public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
String[] picUrls = new String[] { "http://169.254.238.174:8080/ab.jpg",
"http://169.254.238.174:8080/ae.jpg",
"http://169.254.238.174:8080/af.jpg",
"http://169.254.238.174:8080/ah.jpg",
"http://169.254.238.174:8080/bd.jpg" };
private ViewPager viewPager;
private LinearLayout ll_dots;
private ArrayList<ImageView> dotsList;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) {
int currentItem = viewPager.getCurrentItem();
currentItem++;
viewPager.setCurrentItem(currentItem);
// 继续轮播
Log.i(TAG, "bobobo.....");
startRool();
}
};
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
ll_dots = (LinearLayout) findViewById(R.id.ll_dots);
// 初始化小点
initDots();


// 设置适配器
viewPager.setAdapter(new MyPagerAdapter(this, picUrls, handler));
// 设置当前页
viewPager.setCurrentItem(picUrls.length * 10000);
// 设置ViewPager的监听
viewPager.setOnPageChangeListener(new OnPageChangeListener() {


@Override
public void onPageSelected(int position) {
for (int i = 0; i < picUrls.length; i++) {
if (i == position % picUrls.length) {
dotsList.get(i).setImageDrawable(
getResources().getDrawable(
R.drawable.dots_focuse));
} else {
dotsList.get(i).setImageDrawable(
getResources().getDrawable(
R.drawable.dots_normal));
}


}
}


@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {


}


@Override
public void onPageScrollStateChanged(int arg0) {


}
});


startRool();


}


private void startRool() {
// 开始轮播
handler.sendEmptyMessageDelayed(0, 2000);
}


/**
* 初始化小点
*/
private void initDots() {
dotsList = new ArrayList<ImageView>();
dotsList.clear();
// 移除小点
ll_dots.removeAllViews();
for (int i = 0; i < picUrls.length; i++) {
ImageView imageView = new ImageView(this);
Drawable drawable = null;
if (i == 0) {
// 亮色图片
drawable = getResources().getDrawable(R.drawable.dots_focuse);


} else {
drawable = getResources().getDrawable(R.drawable.dots_normal);
}
imageView.setImageDrawable(drawable);
// 考虑屏幕适配
LayoutParams params = new LayoutParams(dip2px(this, 10), dip2px(
this, 10));
params.setMargins(dip2px(this, 5), 0, dip2px(this, 5), 0);
ll_dots.addView(imageView, params);
// 将小点的控件添加到集合中
dotsList.add(imageView);
}
}


/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}


@Override
protected void onDestroy() {
// 移除所有的handler信息
handler.removeCallbacksAndMessages(null);
super.onDestroy();
}


}










package com.example.roolpage;


import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.lidroid.xutils.BitmapUtils;


public class MyPagerAdapter extends PagerAdapter {
private String[] urls;
private Context context;
private Handler handler;


public MyPagerAdapter(Context context, String[] urls,Handler handler) {
this.handler=handler;
this.context = context;
this.urls = urls;
}


@Override
public int getCount() {
return Integer.MAX_VALUE;
}


@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}


@Override
public Object instantiateItem(ViewGroup container, int position) {
// ImageView
ImageView imageView = new ImageView(context);
BitmapUtils bitmapUtils = new BitmapUtils(context);
bitmapUtils.display(imageView, urls[position % urls.length]);

container.addView(imageView);
return imageView;
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}












0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:621次
    • 积分:106
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:2篇
    • 译文:2篇
    • 评论:0条
    文章分类
    文章存档