import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import android.content.Context;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class HeadAD extends RelativeLayout {
private ViewPager vp;
private LinearLayout symbolLayout, bottomLayout;
private LayoutParams lp;
// private ArrayList<imageview> symbolViews;
private List<viewpagerbean> showResults;
private boolean isContinue = true ;
private int currentItem = 0 ; // 当前图片的索引号
private ScheduledExecutorService updateService;
private Context context;
public void initData(List<viewpagerbean> pResult) {
showResults = pResult;
}
public HeadAD(Context context, AttributeSet attrs) {
super (context, attrs);
this .context = context;
// setBackgroundResource(R.color.black);
}
public void initView(Context context) {
// TODO Auto-generated method stub
lp = new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(
context, 180 ));
vp = new ViewPager(context);
vp.setLayoutParams(lp);
lp = new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(
context, 20 ));
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bottomLayout = new LinearLayout(context);
bottomLayout.setGravity(Gravity.CENTER);
bottomLayout.setOrientation(LinearLayout.VERTICAL);
bottomLayout.setLayoutParams(lp);
bottomLayout.setBackgroundResource(R.color.footerBg);
lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
symbolLayout = new LinearLayout(context);
symbolLayout.setGravity(Gravity.CENTER);
symbolLayout.setOrientation(LinearLayout.HORIZONTAL);
symbolLayout.setPadding( 0 , 0 , 5 , 5 );
ImageView symbolIv;
for ( int i = 0 ; i < showResults.size(); i++) {
if (showResults.size() > 1 ) {
symbolIv = new ImageView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.rightMargin = 10 ;
symbolIv.setBackgroundResource(R.drawable.point_bg);
symbolIv.setEnabled( false );
if (i == 0 ) {
symbolIv.setEnabled( true );
} else {
symbolIv.setEnabled( false );
}
symbolIv.setLayoutParams(params);
symbolLayout.addView(symbolIv);
}
}
bottomLayout.addView(symbolLayout);
adapter = new ViewPagerAdapter(context, showResults);
vp.setAdapter(adapter);
vp.setOnTouchListener( new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isContinue = false ;
break ;
case MotionEvent.ACTION_MOVE:
isContinue = false ;
break ;
case MotionEvent.ACTION_UP:
isContinue = true ;
break ;
case MotionEvent.ACTION_CANCEL:
break ;
}
return false ;
}
});
vp.setOnPageChangeListener( new MyPageChangeListener());
addView(vp);
addView(bottomLayout);
}
private Handler viewPagerHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
vp.setCurrentItem(currentItem % showResults.size()); // 切换当前显示的图片
};
};
public ViewPagerAdapter adapter;
// 直接在activity的OnResume里调用,当前界面是该activity时开始循环
public void startUpdate() {
this .updateService = Executors.newSingleThreadScheduledExecutor();
this .updateService.scheduleAtFixedRate( new ScrollTask(), 5 , 5 ,
TimeUnit.SECONDS);
}
// 直接在activity的OnPause里调用,当前界面不是该activity时停止循环
public void stopUpdate() {
this .updateService.shutdown();
updateService = null ;
}
/**
* 当ViewPager中页面的状态发生改变时调用
*
*
*/
private class MyPageChangeListener implements OnPageChangeListener {
private int oldPosition = 0 ;
public void onPageSelected( int position) {
currentItem = position % showResults.size();
symbolLayout.getChildAt(oldPosition).setEnabled( false );
symbolLayout.getChildAt(position).setEnabled( true );
oldPosition = position;
}
public void onPageScrollStateChanged( int arg0) {
}
public void onPageScrolled( int arg0, float arg1, int arg2) {
}
}
/**
* 换行切换任务
*
* @author Administrator
*
*/
private class ScrollTask implements Runnable {
public void run() {
if (isContinue) {
synchronized (vp) {
System.out.println( "currentItem: " + currentItem);
currentItem = (currentItem + 1 ) % showResults.size();
viewPagerHandler.obtainMessage().sendToTarget(); // 通过Handler切换图片
}
}
}
}
}
</viewpagerbean></viewpagerbean></imageview>
|