package com.guanyueyun.mylook.view;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.guanyueyun.mylook.R;
import com.guanyueyun.mylook.bean.AdvertInfo;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
/**
* 图片轮播的组合控件
*
* 从网络中获取了数据并且进行解析之后拿到轮播的数据源
* 伪无限轮播:给定Viewapager的页卡初始化为很大的值,然后每次从中间的某一个位置开始。这样左右在用户滑动的时候都相当于滑不到底部
* 自动轮播:添加线程用于三秒发送信息给主线程更新UI。
*
* 添加dot点的内容显示
*
*
* 凡是进行new出来的控件在进行添加的时候都需要指定宽和高。
* 换句话来说就是指定LayoutParams。
*
* 手动切换ViewPager会触发Viewager的滑动监听的方法----onPageScrollStateChanged,该方法中有三种状态可以进行操作。
*
*
*/
public class AdvertView {
private RelativeLayout mRelative;//返回的显示视图
private List<AdvertInfo> list;
private Context context;
private ViewPager viewPager;//图片轮播对象
private List<View> pagerList;
private LinearLayout dotLinear;//dot点的布局
private boolean isAlive = true;//控制线程是否存在
private boolean isRunning = true;//控制是否发送UI来更新操作
private DotThread thread;//线程对象
//控制接收子线程发送的消息,是否更新下一页的操作
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
int currentItem = viewPager.getCurrentItem();
//切换到下一个页卡进行显示
viewPager.setCurrentItem(++currentItem);
return true;
}
});
//停止更新
public AdvertView(List<AdvertInfo> list, Context context) {
this.list = list;
this.context = context;
}
//提供视图的方法给外界进行调用
public View getView(){
if (mRelative==null){
initViews();
}
return mRelative;
}
//内部控件的初始化
private void initViews() {
//最外层的relativeLayout的初始化
mRelative = new RelativeLayout(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
mRelative.setLayoutParams(lp);
//viewpager
initViewPager();
//圆点呈现的线性布局
initDotLinear();
//给Viewpager添加页卡切换的监听
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//页卡选中的方法
@Override
public void onPageSelected(int position) {
checkPoint(position%list.size());
}
@Override
public void onPageScrollStateChanged(int state) {
switch (state){
case ViewPager.SCROLL_STATE_DRAGGING://拖拽
//关闭切换
isRunning = false;
break;
case ViewPager.SCROLL_STATE_IDLE://空闲
isRunning = true;
break;
case ViewPager.SCROLL_STATE_SETTLING://滑翔
break;
}
}
});
}
//初始化dot布局
private void initDotLinear() {
dotLinear = new LinearLayout(context);
RelativeLayout.LayoutParams dotLp = new RelativeLayout.LayoutParams(-2,-2);
dotLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);//添加规则底部对齐 属性名称 verb 属性填充的值anchor
dotLp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);//水平居中
dotLinear.setOrientation(LinearLayout.HORIZONTAL);//设置水平呈现
dotLinear.setLayoutParams(dotLp);//设置显示的layoutparams
mRelative.addView(dotLinear);
//呈现圆点
createDots();
}
//初始化显示的点
private void createDots() {
for (int i = 0; i < list.size(); i++) {
ImageView iv = new ImageView(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-2,-2);
lp.leftMargin = 10;
lp.rightMargin = 10;
lp.bottomMargin = 10;
lp.height = 10;
lp.weight = 10;
iv.setLayoutParams(lp);//设置呈现的layoutparams
dotLinear.addView(iv);
}
//设置选中的点
checkPoint(0);
}
//设置选中的位置呈现的点
private void checkPoint(int index) {
for (int i = 0; i < list.size(); i++) {
//取出在dotLinerar中每一个Imageview
ImageView iv = (ImageView) dotLinear.getChildAt(i);
if (index==i){
//设置颜色白色
iv.setBackgroundResource(R.mipmap.tabmain_dot_icon_unselect);
}else{
//设置灰色背景
iv.setBackgroundResource(R.mipmap.tabmain_dot_icon);
}
}
}
private void initViewPager() {
viewPager = new ViewPager(context);
RelativeLayout.LayoutParams pagerLp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
viewPager.setLayoutParams(pagerLp);
mRelative.addView(viewPager);
//初始化适配器的数据源
initViewPagerList();
//适配器
AdvertAdapter adapter =new AdvertAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(list.size()*500);//设置中间的显示位置
}
//初始化适配显示的数据源view
private void initViewPagerList() {
pagerList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
ImageView iv = new ImageView(context);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
//picasso赋值
Picasso.with(context).load(list.get(i).getImage().getUrl()).into(iv);
pagerList.add(iv);
}
}
//自送开始更新轮播
public void startUpdate(){
if (thread==null) {
thread = new DotThread();
thread.start();
}
}
//停止更新
public void stopUpdate() {
isRunning = false;
isAlive = false;
if (thread!=null){
thread=null;
}
}
public class DotThread extends Thread{
@Override
public void run() {
super.run();
//如果线程还在
while(isAlive){
//是否需要发送
if (isRunning){
//切换页卡--发送空消息
handler.sendEmptyMessage(0);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
//viewpager的适配器显示
class AdvertAdapter extends PagerAdapter{
@Override
public int getCount() {
return Integer.MAX_VALUE;//大值
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(pagerList.get(position%list.size()));
return pagerList.get(position%list.size());
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// super.destroyItem(container, position, object);
container.removeView(pagerList.get(position%list.size()));
}
}
}
android 开发笔记 图片轮播
最新推荐文章于 2024-06-18 14:01:34 发布