在上一篇博客當中,咱們講了頻道佈局的實現,接下來咱們講解一下活動廣告佈局的實現,效果以下圖: java
這個是用viewpager去實現的,新建一個act_item.xml,代碼以下所示:android
1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:clipChildren="false"> 7 8 <android.support.v4.view.ViewPager 9 android:id="@+id/act_viewpager" 10 android:layout_width="match_parent" 11 android:layout_height="120dp" 12 android:layout_gravity="center" 13 android:layout_marginRight="20dp" 14 android:layout_marginLeft="20dp" 15 android:clipChildren="false"/> 16 17 </FrameLayout>
在HomeFragmentAdapter類中繼續完善活動的適配器,在onCreateViewHolder方法中建立ActViewHolder,接着在onBindViewHolder方法中綁定ActViewHolder數據,代碼以下所示:git
1 package com.nyl.shoppingmall.home.adapter; 2 3 import android.content.Context; 4 import android.support.v4.view.PagerAdapter; 5 import android.support.v4.view.ViewPager; 6 import android.support.v7.widget.RecyclerView; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.AdapterView; 11 import android.widget.GridView; 12 import android.widget.ImageView; 13 import android.widget.Toast; 14 15 import com.bumptech.glide.Glide; 16 import com.nyl.shoppingmall.R; 17 import com.nyl.shoppingmall.home.bean.ResultBeanData; 18 import com.nyl.shoppingmall.utils.Constants; 19 import com.youth.banner.Banner; 20 import com.youth.banner.BannerConfig; 21 import com.youth.banner.Transformer; 22 import com.youth.banner.listener.OnBannerClickListener; 23 import com.youth.banner.listener.OnLoadImageListener; 24 25 import java.util.ArrayList; 26 import java.util.List; 27 28 /** 29 * 首頁適配器 30 */ 31 32 public class HomeFragmentAdapter extends RecyclerView.Adapter{ 33 34 /** 35 * 廣告幅類型 36 */ 37 public static final int BANNER = 0; 38 39 /** 40 * 頻道類型 41 */ 42 public static final int CHANNEL = 1; 43 44 /** 45 * 活動類型 46 */ 47 public static final int ACT = 2; 48 49 /** 50 * 秒殺類型 51 */ 52 public static final int SECKILL = 3; 53 54 /** 55 * 推薦類型 56 */ 57 public static final int RECOMMEND = 4; 58 59 /** 60 * 熱賣類型 61 */ 62 public static final int HOT = 5; 63 64 /** 65 * 初始化佈局 66 */ 67 private LayoutInflater mLayoutInflater; 68 69 /** 70 * 數據 71 */ 72 private ResultBeanData.ResultBean resultBean; 73 private Context mContext; 74 75 /** 76 * 當前類型 77 */ 78 private int currenType = BANNER; 79 80 public HomeFragmentAdapter(Context mContext, ResultBeanData.ResultBean resultBean) { 81 this.mContext = mContext; 82 this.resultBean = resultBean; 83 mLayoutInflater = LayoutInflater.from(mContext); 84 } 85 86 /** 87 * 至關於getView建立ViewHolder部分代碼 88 * @param parent 89 * @param viewType 當前的類型 90 * @return 91 */ 92 @Override 93 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 94 //廣告福 95 if (viewType == BANNER){ 96 //建立BannerViewHolder,Banner裏面傳佈局文件 97 return new BannerViewHolder(mContext,mLayoutInflater.inflate(R.layout.banner_viewpager,null)); 98 //頻道 99 }else if (viewType == CHANNEL){ 100 return new ChannelViewHolder(mContext,mLayoutInflater.inflate(R.layout.channel_item,null)); 101 //活動 102 }else if (viewType == ACT){ 103 return new ActViewHolder(mContext,mLayoutInflater.inflate(R.layout.act_item,null)); 104 } 105 return null; 106 } 107 108 /** 109 * 至關於getView中綁定數據模塊 110 * @param holder 111 * @param position 112 */ 113 @Override 114 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 115 //輪循廣告 116 if (getItemViewType(position) == BANNER){ 117 BannerViewHolder bannerViewHolder = (BannerViewHolder) holder; 118 bannerViewHolder.setData(resultBean.getBanner_info()); 119 /** 120 * 頻道 121 */ 122 }else if (getItemViewType(position) == CHANNEL){ 123 ChannelViewHolder channelViewHolder = (ChannelViewHolder) holder; 124 channelViewHolder.setData(resultBean.getChannel_info()); 125 /** 126 * 活動 127 */ 128 }else if (getItemViewType(position) == ACT){ 129 ActViewHolder actViewHolder = (ActViewHolder) holder; 130 actViewHolder.setData(resultBean.getAct_info()); 131 } 132 } 133 134 /** 135 * 廣告幅 136 */ 137 class BannerViewHolder extends RecyclerView.ViewHolder{ 138 139 private Context mContext; 140 private Banner banner; 141 142 public BannerViewHolder(Context mContext, View itemView) { 143 super(itemView); 144 this.mContext = mContext; 145 this.banner = (Banner) itemView.findViewById(R.id.banner); 146 } 147 148 public void setData(List<ResultBeanData.ResultBean.BannerInfoEntity> banner_info) { 149 //獲得圖片集合地址 150 List<String> imagesUrl = new ArrayList<>(); 151 for (int i = 0;i<banner_info.size();i++){ 152 String imageUrl = banner_info.get(i).getImage(); 153 imagesUrl.add(imageUrl); 154 } 155 //設置循環指示點 156 banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR); 157 //設置手風琴效果 158 banner.setBannerAnimation(Transformer.Accordion); 159 //設置Banner圖片數據 160 banner.setImages(imagesUrl, new OnLoadImageListener() { 161 @Override 162 public void OnLoadImage(ImageView view, Object url) { 163 //聯網請求圖片-Glide 164 Glide.with(mContext).load(Constants.BASE_URL_IMAGE + url).into(view); 165 } 166 }); 167 //設置點擊事件 168 banner.setOnBannerClickListener(new OnBannerClickListener() { 169 @Override 170 public void OnBannerClick(int position) { 171 Toast.makeText(mContext,"position=="+position,Toast.LENGTH_SHORT).show(); 172 } 173 }); 174 } 175 } 176 177 /** 178 * 頻道 179 */ 180 class ChannelViewHolder extends RecyclerView.ViewHolder{ 181 182 private Context mContext; 183 private GridView gv_channel; 184 private ChannelAdapter adapter; 185 186 public ChannelViewHolder(Context mContext, View itemView) { 187 super(itemView); 188 this.mContext = mContext; 189 gv_channel = (GridView) itemView.findViewById(R.id.gv_channel); 190 } 191 192 public void setData(List<ResultBeanData.ResultBean.ChannelInfoBean> channel_info) { 193 //獲得數據後,就設置GridView的適配器 194 adapter = new ChannelAdapter(mContext,channel_info); 195 gv_channel.setAdapter(adapter); 196 197 //設置item的點擊事件 198 gv_channel.setOnItemClickListener(new AdapterView.OnItemClickListener() { 199 @Override 200 public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 201 Toast.makeText(mContext,"position"+position,Toast.LENGTH_SHORT).show(); 202 } 203 }); 204 } 205 } 206 207 /** 208 * 活動 209 */ 210 class ActViewHolder extends RecyclerView.ViewHolder{ 211 212 private Context mContext; 213 private ViewPager act_viewpager; 214 215 public ActViewHolder(Context mContext,View itemView) { 216 super(itemView); 217 this.mContext = mContext; 218 act_viewpager = (ViewPager) itemView.findViewById(R.id.act_viewpager); 219 } 220 221 public void setData(final List<ResultBeanData.ResultBean.ActInfoEntity> act_info) { 222 //設置間距 223 act_viewpager.setPageMargin(20); 224 //有數據後,就設置數據適配器 225 act_viewpager.setAdapter(new PagerAdapter() { 226 @Override 227 public int getCount() { 228 return act_info.size(); 229 } 230 231 /** 232 * 233 * @param view 頁面 234 * @param object instantiateItem方法返回的值 235 * @return 236 */ 237 @Override 238 public boolean isViewFromObject(View view, Object object) { 239 return view == object; 240 } 241 242 /** 243 * 244 * @param container ViewPager 245 * @param position 對應頁面的位置 246 * @return 247 */ 248 @Override 249 public Object instantiateItem(ViewGroup container, final int position) { 250 //實例化ImageView 251 ImageView imageView = new ImageView(mContext); 252 //設置ImageView的拉伸 253 imageView.setScaleType(ImageView.ScaleType.FIT_XY); 254 //根據Glide設置圖片 255 Glide.with(mContext).load(Constants.BASE_URL_IMAGE+act_info.get(position).getIcon_url()).into(imageView); 256 //添加到容器中 257 container.addView(imageView); 258 259 //設置點擊事件 260 imageView.setOnClickListener(new View.OnClickListener() { 261 @Override 262 public void onClick(View view) { 263 Toast.makeText(mContext,"position=="+position,Toast.LENGTH_SHORT).show(); 264 } 265 }); 266 return imageView; 267 } 268 269 @Override 270 public void destroyItem(ViewGroup container, int position, Object object) { 271 container.removeView((View) object); 272 } 273 }); 274 } 275 } 276 277 /** 278 * 獲得不一樣的類型 279 * @param position 280 * @return 281 */ 282 @Override 283 public int getItemViewType(int position) { 284 switch (position){ 285 case BANNER: //廣告幅 286 currenType = BANNER; 287 break; 288 case CHANNEL: //頻道 289 currenType = CHANNEL; 290 break; 291 case ACT: //活動 292 currenType = ACT; 293 break; 294 case SECKILL: //秒殺 295 currenType = SECKILL; 296 break; 297 case RECOMMEND: //推薦 298 currenType = RECOMMEND; 299 break; 300 case HOT: //熱賣 301 currenType = HOT; 302 break; 303 } 304 return currenType; 305 } 306 307 /** 308 * 總共有多少個item 309 * @return 310 */ 311 @Override 312 public int getItemCount() { 313 return 3; 314 } 315 316 317 }
咱們還能夠讓ViewPager切換效果更加炫酷,這裏介紹一個第三方庫,下載地址:https://github.com/hongyangAndroid/MagicViewPager,接着去引入compile 'com.zhy:magic-viewpager:1.0.1',將切換動畫進行美化,代碼以下圖:github
效果圖就沒法截圖展示出來了,有興趣的朋友,能夠動手去操做。好了,關於活動廣告佈局的內容就介紹這麼多。ide