歡迎關(guān)注我的程序員博客網(wǎng)站-葉為正博客:http://www.yeweizheng.com/
5-4 首頁列表—單圖Item初始化
現(xiàn)在我們來更新UI
//發(fā)送推薦產(chǎn)品請求
private void requestRecommandData() {
RequestCenter.requestRecommandData(new DisposeDataListener() {
@Override
public void onSuccess(Object responseObj) {
mRecommandData = (BaseRecommandModel) responseObj;
//更新UI
showSuccessView();
}
@Override
public void onFailure(Object reasonObj) {
//顯示請求失敗View
showErrorView();
}
});
}
//顯示請求成功UI
private void showSuccessView() {
if (mRecommandData.data.list != null && mRecommandData.data.list.size() > 0) {
mLoadingView.setVisibility(View.GONE);
mListView.setVisibility(View.VISIBLE);
//為listview添加頭
mListView.addHeaderView(new HomeHeaderLayout(mContext, mRecommandData.data.head));
mAdapter = new CourseAdapter(mContext, mRecommandData.data.list);
mListView.setAdapter(mAdapter);
mListView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
mAdapter.updateAdInScrollView();
}
});
} else {
showErrorView();
}
}
private void showErrorView() {
}
下面就是adapter的代碼:
package com.youdu.adapter;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.gson.Gson;
import com.youdu.R;
import com.youdu.activity.AdBrowserActivity;
import com.youdu.activity.PhotoViewActivity;
import com.youdu.core.AdContextInterface;
import com.youdu.core.video.VideoAdContext;
import com.youdu.module.recommand.RecommandBodyValue;
import com.youdu.share.ShareDialog;
import com.youdu.util.ImageLoaderManager;
import com.youdu.util.Util;
import com.youdu.adutil.Utils;
import java.util.ArrayList;
import cn.sharesdk.framework.Platform;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* @author: qndroid
* @function:
* @date: 16/6/15
*/
public class CourseAdapter extends BaseAdapter {
/**
* Common
*/
private static final int CARD_COUNT = 4;
private static final int VIDOE_TYPE = 0x00;
private static final int CARD_TYPE_ONE = 0x01;
private static final int CARD_TYPE_TWO = 0x02;
private static final int CARD_TYPE_THREE = 0x03;
private LayoutInflater mInflate;
private Context mContext;
private ArrayList<RecommandBodyValue> mData;
private ViewHolder mViewHolder;
private VideoAdContext mAdsdkContext;
/**
* 異步圖片加載類
*/
private ImageLoaderManager mImagerLoader;
/**
* 在構(gòu)造方法中初始化這些context诀姚,mInflate,mImagerLoader
* @param context
* @param data
*/
public CourseAdapter(Context context, ArrayList<RecommandBodyValue> data) {
mContext = context;
mData = data;
mInflate = LayoutInflater.from(mContext);
mImagerLoader = ImageLoaderManager.getInstance(mContext);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return CARD_COUNT;
}
@Override
public int getItemViewType(int position) {
RecommandBodyValue value = (RecommandBodyValue) getItem(position);
return value.type;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
final RecommandBodyValue value = (RecommandBodyValue) getItem(position);
//無tag時
if (convertView == null) {
switch (type) {
case VIDOE_TYPE:
//顯示video卡片
mViewHolder = new ViewHolder();
convertView = mInflate.inflate(R.layout.item_video_layout, parent, false);
mViewHolder.mVieoContentLayout = (RelativeLayout)
convertView.findViewById(R.id.video_ad_layout);
mViewHolder.mLogoView = (CircleImageView) convertView.findViewById(R.id.item_logo_view);
mViewHolder.mTitleView = (TextView) convertView.findViewById(R.id.item_title_view);
mViewHolder.mInfoView = (TextView) convertView.findViewById(R.id.item_info_view);
mViewHolder.mFooterView = (TextView) convertView.findViewById(R.id.item_footer_view);
mViewHolder.mShareView = (ImageView) convertView.findViewById(R.id.item_share_view);
//為對應布局創(chuàng)建播放器
mAdsdkContext = new VideoAdContext(mViewHolder.mVieoContentLayout,
new Gson().toJson(value), null);
mAdsdkContext.setAdResultListener(new AdContextInterface() {
@Override
public void onAdSuccess() {
}
@Override
public void onAdFailed() {
}
@Override
public void onClickVideo(String url) {
Intent intent = new Intent(mContext, AdBrowserActivity.class);
intent.putExtra(AdBrowserActivity.KEY_URL, url);
mContext.startActivity(intent);
}
});
break;
case CARD_TYPE_ONE:
mViewHolder = new ViewHolder();
convertView = mInflate.inflate(R.layout.item_product_card_one_layout, parent, false);
mViewHolder.mLogoView = (CircleImageView) convertView.findViewById(R.id.item_logo_view);
mViewHolder.mTitleView = (TextView) convertView.findViewById(R.id.item_title_view);
mViewHolder.mInfoView = (TextView) convertView.findViewById(R.id.item_info_view);
mViewHolder.mFooterView = (TextView) convertView.findViewById(R.id.item_footer_view);
mViewHolder.mPriceView = (TextView) convertView.findViewById(R.id.item_price_view);
mViewHolder.mFromView = (TextView) convertView.findViewById(R.id.item_from_view);
mViewHolder.mZanView = (TextView) convertView.findViewById(R.id.item_zan_view);
mViewHolder.mProductLayout = (LinearLayout) convertView.findViewById(R.id.product_photo_layout);
break;
case CARD_TYPE_TWO:
mViewHolder = new ViewHolder();
convertView = mInflate.inflate(R.layout.item_product_card_two_layout, parent, false);
mViewHolder.mLogoView = (CircleImageView) convertView.findViewById(R.id.item_logo_view);
mViewHolder.mTitleView = (TextView) convertView.findViewById(R.id.item_title_view);
mViewHolder.mInfoView = (TextView) convertView.findViewById(R.id.item_info_view);
mViewHolder.mFooterView = (TextView) convertView.findViewById(R.id.item_footer_view);
mViewHolder.mProductView = (ImageView) convertView.findViewById(R.id.product_photo_view);
mViewHolder.mPriceView = (TextView) convertView.findViewById(R.id.item_price_view);
mViewHolder.mFromView = (TextView) convertView.findViewById(R.id.item_from_view);
mViewHolder.mZanView = (TextView) convertView.findViewById(R.id.item_zan_view);
break;
case CARD_TYPE_THREE:
mViewHolder = new ViewHolder();
convertView = mInflate.inflate(R.layout.item_product_card_three_layout, null, false);
mViewHolder.mViewPager = (ViewPager) convertView.findViewById(R.id.pager);
//add data
ArrayList<RecommandBodyValue> recommandList = Util.handleData(value);
mViewHolder.mViewPager.setPageMargin(Utils.dip2px(mContext, 12));
mViewHolder.mViewPager.setAdapter(new HotSalePagerAdapter(mContext, recommandList));
mViewHolder.mViewPager.setCurrentItem(recommandList.size() * 100);
break;
}
convertView.setTag(mViewHolder);
}//有tag時
else {
mViewHolder = (ViewHolder) convertView.getTag();
}
//填充item的數(shù)據(jù)
switch (type) {
case VIDOE_TYPE:
mImagerLoader.displayImage(mViewHolder.mLogoView, value.logo);
mViewHolder.mTitleView.setText(value.title);
mViewHolder.mInfoView.setText(value.info.concat(mContext.getString(R.string.tian_qian)));
mViewHolder.mFooterView.setText(value.text);
mViewHolder.mShareView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ShareDialog dialog = new ShareDialog(mContext, false);
dialog.setShareType(Platform.SHARE_VIDEO);
dialog.setShareTitle(value.title);
dialog.setShareTitleUrl(value.site);
dialog.setShareText(value.text);
dialog.setShareSite(value.title);
dialog.setShareTitle(value.site);
dialog.setUrl(value.resource);
dialog.show();
}
});
break;
case CARD_TYPE_ONE:
mImagerLoader.displayImage(mViewHolder.mLogoView, value.logo);
mViewHolder.mTitleView.setText(value.title);
mViewHolder.mInfoView.setText(value.info.concat(mContext.getString(R.string.tian_qian)));
mViewHolder.mFooterView.setText(value.text);
mViewHolder.mPriceView.setText(value.price);
mViewHolder.mFromView.setText(value.from);
mViewHolder.mZanView.setText(mContext.getString(R.string.dian_zan).concat(value.zan));
mViewHolder.mProductLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, PhotoViewActivity.class);
intent.putStringArrayListExtra(PhotoViewActivity.PHOTO_LIST, value.url);
mContext.startActivity(intent);
}
});
mViewHolder.mProductLayout.removeAllViews();
//動態(tài)添加多個imageview
for (String url : value.url) {
mViewHolder.mProductLayout.addView(createImageView(url));
}
break;
case CARD_TYPE_TWO:
mImagerLoader.displayImage(mViewHolder.mLogoView, value.logo);
mViewHolder.mTitleView.setText(value.title);
mViewHolder.mInfoView.setText(value.info.concat(mContext.getString(R.string.tian_qian)));
mViewHolder.mFooterView.setText(value.text);
mViewHolder.mPriceView.setText(value.price);
mViewHolder.mFromView.setText(value.from);
mViewHolder.mZanView.setText(mContext.getString(R.string.dian_zan).concat(value.zan));
//為單個ImageView加載遠程圖片
mImagerLoader.displayImage(mViewHolder.mProductView, value.url.get(0));
break;
case CARD_TYPE_THREE:
break;
}
return convertView;
}
//自動播放方法
public void updateAdInScrollView() {
if (mAdsdkContext != null) {
mAdsdkContext.updateAdInScrollView();
}
}
//動態(tài)添加ImageView
private ImageView createImageView(String url) {
ImageView photoView = new ImageView(mContext);
LinearLayout.LayoutParams params = new LinearLayout.
LayoutParams(Utils.dip2px(mContext, 100),
LinearLayout.LayoutParams.MATCH_PARENT);
params.leftMargin = Utils.dip2px(mContext, 5);
photoView.setLayoutParams(params);
mImagerLoader.displayImage(photoView, url);
return photoView;
}
private static class ViewHolder {
//所有Card共有屬性
private CircleImageView mLogoView;
private TextView mTitleView;
private TextView mInfoView;
private TextView mFooterView;
//Video Card特有屬性
private RelativeLayout mVieoContentLayout;
private ImageView mShareView;
//Video Card外所有Card具有屬性
private TextView mPriceView;
private TextView mFromView;
private TextView mZanView;
//Card One特有屬性
private LinearLayout mProductLayout;
//Card Two特有屬性
private ImageView mProductView;
//Card Three特有屬性
private ViewPager mViewPager;
}
}
主要就是去重寫我們的getView方法
最終效果