1.HomeAdView
package com.tengw.jinhuaqi.view; 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.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.LinearLayout; import android.widget.TextView; import com.tengw.jinhuaqi.R; import com.tengw.jinhuaqi.entity.AdvertEntity; import com.tengw.jinhuaqi.util.LayoutUtil; import com.tengw.jinhuaqi.util.LogUtil; import com.tengw.jinhuaqi.util.StringUtil; /** * 首页广告 * @author xcp001 * */ public class HomeAdView extends LinearLayout { private Context context; private TextView title;//新闻标题 private LinearLayout dotView; private ViewPager viewPager; private List<View> views = new ArrayList<View>(); private List<View> dots = new ArrayList<View>(); private int currentItem; private List<AdvertEntity> adList = new ArrayList<AdvertEntity>(); private ScheduledExecutorService scheduledExecutorService; /** * 切换当前显示的图片 */ private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { // 切换当前显示的图片 viewPager.setCurrentItem(currentItem); }; }; public HomeAdView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; LayoutInflater.from(context).inflate(R.layout.advert_layout, this); dotView = (LinearLayout) findViewById(R.id.dot); title = (TextView) findViewById(R.id.tv_title); viewPager = (ViewPager) findViewById(R.id.vp); // 设置填充ViewPager页面的适配器 viewPager.setAdapter(new MyAdapter()); // 设置一个监听器,当ViewPager中的页面改变时调用 viewPager.setOnPageChangeListener(new MyPageChangeListener()); } /** * 更新页面 */ public void updateView(List<AdvertEntity> adList) { this.adList = adList; if (adList.size() == 0) { return; } views.clear(); dots.clear(); dotView.removeAllViews(); int size = adList.size(); AdvertEntity info = null; for (int i = 0; i < size; i++) { info = adList.get(i); ImageView imageView = new ImageView(context); // String minImg=StringUtil.getMinUrl(info.getPhotoUrl()); LayoutUtil.setBitmap(imageView, info.getImgUrl()); //TODO 测试 // imageView.setImageResource(info.getImgId()); imageView.setScaleType(ScaleType.FIT_XY); views.add(imageView); imageView = new ImageView(context); imageView.setImageResource(R.drawable.gsdt_icondian2); dots.add(imageView); if (views.size() == 1) { views.get(0).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LogUtil.i("=pos11=", ""); // callback.gotoAdDetail(0); } }); } title.setText(info.getDesc()); } for (int i = 0; i < dots.size(); i++) { dotView.addView(dots.get(i)); } viewPager.setAdapter(new MyAdapter()); onStart(); } private AdviewCallback callback; public interface AdviewCallback { void gotoAdDetail(int position); } public void setCallback(AdviewCallback callback) { this.callback = callback; } public void onStart() { LogUtil.i("=onStart=", "" + views.size()); onStop(); if (views.size() > 1) { scheduledExecutorService = Executors .newSingleThreadScheduledExecutor(); // 当Activity显示出来后,每两秒钟切换一次图片显示 scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 1, 3, TimeUnit.SECONDS); } } public void onStop() { // 当Activity不可见的时候停止切换 if (scheduledExecutorService != null) { scheduledExecutorService.shutdown(); } } /** * 换行切换任务 * * @author Administrator * */ private class ScrollTask implements Runnable { public void run() { synchronized (viewPager) { currentItem = (currentItem + 1) % views.size(); handler.obtainMessage().sendToTarget(); // 通过Handler切换图片 } } } /** * 当ViewPager中页面的状态发生改变时调用 * @author Administrator */ private class MyPageChangeListener implements OnPageChangeListener { /** * This method will be invoked when a new page becomes selected. * position: Position index of the new selected page. */ public void onPageSelected(final int position) { currentItem = position; LogUtil.i("=dots=", dots.size() + ""); for (int i = 0; i < dots.size(); i++) { dots.get(i).setBackgroundResource(R.drawable.gsdt_icondian2); } dots.get(position).setBackgroundResource(R.drawable.gsdt_icondian1); //TODO 切换标题 if (null != adList.get(position) && !StringUtil .isStringEmpty(adList.get(position).getDesc())) { title.setText(adList.get(position).getDesc()); } else { title.setText(""); } views.get(position).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LogUtil.i("=pos=", "" + position); // callback.gotoAdDetail(position); } }); } public void onPageScrollStateChanged(int arg0) { } public void onPageScrolled(int arg0, float arg1, int arg2) { } } /** * @author xcp001 * */ private class MyAdapter extends PagerAdapter { @Override public int getCount() { return adList != null ? adList.size() : 0; } @Override public Object instantiateItem(View arg0, int arg1) { if (views.size() == 0) { return null; } ((ViewPager) arg0).addView(views.get(arg1)); return views.get(arg1); } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView((View) arg2); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { } @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View arg0) { } @Override public void finishUpdate(View arg0) { } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/frameView" android:layout_width="match_parent" android:layout_height="@dimen/ad_h" > <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <!-- android:background="#33000000" --> <RelativeLayout android:layout_width="fill_parent" android:layout_height="@dimen/ad_title_h" android:layout_gravity="bottom" android:background="#66000000" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:ellipsize="marquee" android:paddingLeft="@dimen/padding_15px" android:paddingRight="@dimen/padding_15px" android:singleLine="true" android:textColor="#ffffff" android:textSize="@dimen/text_size_16" /> <LinearLayout android:id="@+id/dot" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:gravity="center_vertical" android:orientation="horizontal" android:paddingRight="@dimen/padding_15px" > </LinearLayout> </RelativeLayout> </FrameLayout> </RelativeLayout>
package com.tengw.jinhuaqi.entity; /** * 广告 */ public class AdvertEntity { private int id; private String imgUrl; private String desc; private String type; private String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getImgUrl() { return imgUrl; } public void setImgUrl(String imgUrl) { this.imgUrl = imgUrl; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } }
/**广告*/ private List<AdvertEntity> adList = new ArrayList<AdvertEntity>();
AdvertEntity entity; for (int i = 0; i < 5; i++) { entity = new AdvertEntity(); entity.setContent("广告" + i); entity.setDesc("广告描述" + i); entity.setImgUrl("/advert/patient/139883303682425453170.jpg"); adList.add(entity); }
滚动广告——viewpager,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u012252502/article/details/25614667