1、
public class BannerAdView implements ViewPager.OnPageChangeListener { private ViewPager mViewPager; private AdsAdapter mAdsAdapter; private List<View> mBanner_Views; private List<Banner> mBannersList; private ImageView[] mImageViews; private RemoteImageView mRemoteImageView; private int mViewCount; private int mCurSel; private LinearLayout ll_point; private Context context; private LayoutInflater mInflater; private TextView tv_ads_description; //广告描述信息 private String[] ads_description; public BannerView(Context context, List<Banner> mBannersList) { mInflater = LayoutInflater.from(context); this.context = context; this.mBannersList = mBannersList; } public View initView() { View view = mInflater.inflate(R.layout.game_banner, null); mViewPager = (HackyViewPager) view .findViewById(R.id.group_viewpager); tv_ads_description = (TextView) view .findViewById(R.id.tv_ads_description); ll_point = (LinearLayout) view.findViewById(R.id.ll_point); initViewPage(); initPoint(); return view; } public AdsAdapter getAdapter(){ return mAdsAdapter; } private void initViewPage() { mBanner_Views = new ArrayList<View>(); if (mBannersList != null && mBannersList.size() > 0) { Collections.sort(mBannersList, new Comparator<Banner>(){ @Override public int compare(Banner lhs, Banner rhs) { return lhs.getAdv_priority().compareTo(rhs.getAdv_priority()); } }); ads_description = new String[mBannersList.size()]; for (int j = 0; j < mBannersList.size(); j++) { View view = mInflater.inflate(R.layout.game_banner_item, null); mRemoteImageView = (RemoteImageView) view.findViewById(R.id.banner_item_image); mRemoteImageView.setDefaultImage(R.drawable.ad_default_cc); Banner mBanner = mBannersList.get(j); ads_description[j]=mBanner.getAdv_context(); mRemoteImageView.MASTERWINE_DIR = Utils.IMAGE_ADS_CACHE_DIR; mRemoteImageView.setImageUrl(mBanner.getAdv_url()); mBanner_Views.add(view); } mViewCount = mBannersList.size(); } mAdsAdapter = new AdsAdapter(mBanner_Views, mBannersList); mViewPager.setAdapter(mAdsAdapter); mViewPager.setOnPageChangeListener(this); mViewPager.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: isRunning=false; break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: isRunning=true; handler.removeCallbacksAndMessages(null); handler.sendEmptyMessageDelayed(0, 3000); break; } return false; } }); } /** * 初始化点 */ private void initPoint() { mImageViews = new ImageView[mViewCount]; LinearLayout.LayoutParams img_layoutparams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); img_layoutparams.rightMargin = 15; ll_point.removeAllViews(); if (mViewCount == 0) return; for (int i = 0; i < mViewCount; i++) { mImageViews[i] = new ImageView(context); mImageViews[i].setLayoutParams(img_layoutparams); mImageViews[i].setImageResource(R.drawable.point_bg); ll_point.addView(mImageViews[i]); } mCurSel = 0; mImageViews[mCurSel].setEnabled(false); tv_ads_description.setText(ads_description[mCurSel]); //轮播 Message message = new Message(); isRunning = true; handler.sendMessageDelayed(message, 3000); } /** * 判断是否自动滚动 */ private boolean isRunning = false; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { if(isRunning){ //让viewPager 滑动到下一页 mViewPager.setCurrentItem(mViewPager.getCurrentItem()+1); handler.sendEmptyMessageDelayed(0, 3000); } }; }; //停止 public void onStop() { isRunning = false; }; //恢复 public void onResume() { isRunning = true; handler.sendEmptyMessageDelayed(0, 3000); }; @Override public void onPageSelected(int position) { position = position%mBanner_Views.size(); //设置文字描述内容 tv_ads_description.setText(ads_description[position]); //改变指示点的状态 //把当前点enbale 为true mImageViews[position].setEnabled(false); //把上一个点设为false mImageViews[lastPosition].setEnabled(true); lastPosition = position; } /** * 上一个页面的位置 */ protected int lastPosition; @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } }
public class AdsAdapter extends PagerAdapter { private List<View> mViewList; private List<Banner> mBannerList; public ViewPagerAdsAdapter(List<View> viewList,List<Banner> mPhotolist) { mViewList = viewList; mBannerList = mPhotolist; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(mViewList.get(position%mViewList.size())); } @Override public boolean isViewFromObject(View view, Object obj) { return (view == obj); } public Object instantiateItem(View container, final int position) { View view = mViewList.get(position%mViewList.size()); if (null == container) return null; ((ViewPager) container).addView(mViewList.get(position%mViewList.size())); if (mBannerList != null && mBannerList.size() > 0) { (view.findViewById(R.id.banner_item_image)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (listener != null ) { listener.onClick(mBannerList.get(position%mViewList.size()),position); } } }); } return mViewList.get(position%mViewList.size()); } private OnPageClickListener listener; public void setOnPageClickListener(OnPageClickListener listener){ this.listener = listener; } public interface OnPageClickListener{ public void onClick(Banner banner,int position); } }
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/banner_item_image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY" android:src="@drawable/ad_default_cc" /> </FrameLayout>
<?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="wrap_content" android:background="#D0D6DC" android:orientation="vertical" > <ViewPager android:id="@+id/group_viewpager" android:layout_width="fill_parent" android:layout_height="120dp" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="30dp" android:background="@color/apply_gray_bg" android:paddingLeft="10dp" android:paddingRight="10dp" > <TextView android:id="@+id/tv_ads_description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="热门团购" android:textColor="@color/black" android:textSize="16sp" /> <LinearLayout android:id="@+id/ll_point" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:orientation="horizontal" android:visibility="visible" > </LinearLayout> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1dip" android:background="@color/gray" /> </LinearLayout>