package com.weidingqiang.custommooc.views.allcourse; import android.content.Context; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.weidingqiang.custommooc.R; import com.weidingqiang.custommooc.utils.DisplayUtil; import com.weidingqiang.custommooc.views.widgets.GuidePointGroup; import java.util.ArrayList; import java.util.List; import java.util.logging.LogRecord; /** * Created by weidingqiang on 15/11/26. */ public class HomeViewPager extends RelativeLayout{ private ViewPager viewPager; private GuidePointGroup guidePointGroup; private TextView textView; private int[] imgIds; private List<ImageView> imageViews ; private RelativeLayout textlayout; private boolean isLoop = true; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); } }; public HomeViewPager(Context context) { this(context, null); } public HomeViewPager(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater layoutInflater = LayoutInflater.from(context); layoutInflater.inflate(R.layout.home_viewpager, this, true); initData(); initView(); initEvent(); initAuto(); } private void initView(){ viewPager = (ViewPager) this.findViewById(R.id.home_viewPager); textlayout = (RelativeLayout) this.findViewById(R.id.text_layout); textView = (TextView) this.findViewById(R.id.home_textview); guidePointGroup = new GuidePointGroup(getContext(),imgIds.length); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.CENTER_VERTICAL); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); layoutParams.rightMargin = DisplayUtil.dip2px(getContext(), 10); textlayout.addView(guidePointGroup, layoutParams); } private void initData(){ imgIds = new int[] { R.mipmap.home_1,R.mipmap.home_2, R.mipmap.home_3,R.mipmap.home_4}; imageViews = new ArrayList<ImageView>(); for (int imgId : imgIds) { ImageView imageView = new ImageView(getContext()); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setImageResource(imgId); imageViews.add(imageView); } } private void initEvent(){ viewPager.setAdapter(pagerAdapter); viewPager.addOnPageChangeListener(onPageChangeListener); /** * 2147483647 / 2 = 1073741820 - 1 * 設置ViewPager的當前項爲一個比較大的數,以便一開始就能夠左右循環滑動 */ int n = Integer.MAX_VALUE / 2 % imageViews.size(); int itemPosition = Integer.MAX_VALUE / 2 - n; viewPager.setCurrentItem(itemPosition); } private void initAuto(){ // 自動切換頁面功能 new Thread(new Runnable() { @Override public void run() { while (isLoop) { SystemClock.sleep(8000); handler.sendEmptyMessage(0); } } }).start(); } /** * PagerAdapter 監聽器 */ private PagerAdapter pagerAdapter = new PagerAdapter() { @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(imageViews.get(position % imageViews.size())); return imageViews.get(position % imageViews.size()); } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(imageViews.get(position % imageViews.size())); } }; /** * OnPageChangeListener 監聽器 */ private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { int current = position % imageViews.size(); guidePointGroup.changeSelectView(current); } @Override public void onPageScrollStateChanged(int state) { } }; public void destroy(){ isLoop = false; } }