仿网易客户端首页 Listview+Gallery

本文介绍了一种仿网易Gallery的实现方式,该Gallery可嵌套于ListView的第一项,并提供了详细的代码实现。通过自定义ReacnnListViewGallery类,实现了图片轮播、触控拦截等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Listview 代码

 xListView为下拉刷新,上拉加载的控件

package com.reacnn.android.view.listviewgallery;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.reacnn.android.ReaCnnApplication;
import com.reacnn.android.utils.LogUtil;
import com.reacnn.android.view.listview.XListView;

public class ReacnnListView extends XListView {
	private static final String tag = "ReacnnListView";

	public ReacnnListView(Context context) {
		super(context);

	}

	public ReacnnListView(Context context, AttributeSet attrs) {
		super(context, attrs);

	}

	public ReacnnListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	// onInterceptTouchEvent

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		return super.dispatchTouchEvent(ev);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		float eY = ev.getY();
		Rect r = new Rect();
		if (getChildAt(0) != null) {
			getChildAt(0).getGlobalVisibleRect(r);
			int index_in_adapter = getFirstVisiblePosition();
			if (eY > r.height()) {
				index_in_adapter += (int) ((eY - r.height()) / getChildAt(1)
						.getHeight());
			} else {
				index_in_adapter -= 1;
			}
			if (index_in_adapter == 0) {
				LogUtil.e(tag, "index_in_adapter=" + index_in_adapter, false);
				// -第一项为gallery。判断是否为左右滑动、左右滑动则要下发给子view做处理。设置首页的ViewPager的滑动事件是否可用
				ReaCnnApplication.mCustomerViewPager.setTouchIntercept(false);
			} else {
				ReaCnnApplication.mCustomerViewPager.setTouchIntercept(true);
			}
		}

		return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}
}


gallery

package com.reacnn.android.view.listviewgallery;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.reacnn.android.R;
import com.reacnn.android.view.listviewgallery.asyncloader.AsyncImageLoader;
import com.reacnn.android.view.listviewgallery.bean.ListViewGalleryImageBean;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 仿网易的Gallery,可以嵌套到ListView的第一项中
 * 
 * @author tom
 * 
 */
public class ReacnnListViewGallery extends FrameLayout {
	List<ListViewGalleryImageBean> showings = new ArrayList<ListViewGalleryImageBean>();
	private static final int GALLERY_SPACING = 2;
	private Context mContext;
	private PromotionImages promotionImages;
	private LinearLayout mBottomLayout;
	private TextView tips;
	private int mPcount;
	private ImageView[] icons;

	public ReacnnListViewGallery(Context context) {
		super(context);
	}

	public ReacnnListViewGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		promotionImages = new PromotionImages(context);
		LayoutParams layoutParams = new LayoutParams(
				android.view.ViewGroup.LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.FILL_PARENT);
		layoutParams.bottomMargin = 20;
		this.addView(promotionImages, layoutParams);
		LayoutInflater inflater = ((Activity) context).getLayoutInflater();
		View indicator = inflater
				.inflate(R.layout.control_promotion_hint, null);
		this.addView(indicator);
		mBottomLayout = (LinearLayout) indicator
				.findViewById(R.id.promotion_index_layout);
		tips = (TextView) indicator.findViewById(R.id.promotion_tip);
		initBottomIcons();
		// promotionImages.Start();
	}

	public void SetAdapter(List<ListViewGalleryImageBean> alistList) {
		this.showings = alistList;
		invalidate();
		this.promotionImages.setAdapter(new PromotionImagesAdapter(mContext,
				showings));
		initBottomIcons();
	}

	public interface GalleryItemClickedListener {
		void OnItemClicked(AdapterView<?> arg0, View arg1, int arg2, long arg3,
				ListViewGalleryImageBean mListViewGalleryImageBean);
	}

	GalleryItemClickedListener mOnItemClickListener = null;

	public void SetOnItemClickedLisener(
			GalleryItemClickedListener mClickListener) {
		this.mOnItemClickListener = mClickListener;
	}

	class PromotionImages extends Gallery {
		PromotionImagesAdapter promotionAdapter;

		private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
			return e2.getX() > e1.getX();
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			int position = getSelectedItemPosition();
			if (isScrollingLeft(e1, e2)) {
				if (position == 0) {
					position = getCount() - 1;
				} else {
					position--;
				}
			} else {
				if (position >= getCount() - 1) {
					position = 0;
				} else {
					position++;
				}
			}
			setSelection(position);
			return false;
		}

		@Override
		public void setUnselectedAlpha(float unselectedAlpha) {
			unselectedAlpha = 1.0f;
			super.setUnselectedAlpha(unselectedAlpha);
		}

		private static final int timerAnimation = 1;
		private final Handler mHandler = new Handler() {
			public void handleMessage(android.os.Message msg) {
				switch (msg.what) {
				case timerAnimation:
					int position = getSelectedItemPosition();
					if (position >= getCount() - 1) {
						position = 0;
					} else {
						position++;
					}
					setSelection(position);
					break;
				default:
					break;
				}
			};
		};
		private final Timer timer = new Timer();
		private final TimerTask task = new TimerTask() {
			public void run() {
				mHandler.sendEmptyMessage(timerAnimation);
			}
		};

		public void Start() {
			if (timer != null) {
				timer.schedule(task, 1500, 1000);
			}
		}

		public void cancel() {
			if (timer != null) {
				timer.cancel();
			}
			if (task != null) {
				task.cancel();
			}
		}

		public PromotionImages(Context context) {
			super(context);
			this.setSpacing(GALLERY_SPACING);
			promotionAdapter = new PromotionImagesAdapter(context, showings);
			this.setAdapter(promotionAdapter);

			this.setOnItemSelectedListener(new OnItemSelectedListener() {

				public void onItemSelected(AdapterView<?> parent, View view,
						int position, long id) {
					update(position);
				}

				public void onNothingSelected(AdapterView<?> parent) {

				}
			});
			this.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					ListViewGalleryImageBean mListViewGalleryImageBean = (ListViewGalleryImageBean) getAdapter()
							.getItem(arg2);
					
					if (mOnItemClickListener != null) {
						mOnItemClickListener.OnItemClicked(arg0, arg1, arg2,
								arg3, mListViewGalleryImageBean);
					}
				}
			});
		}

		private final void update(int selected) {
			tips.setText(showings.get(selected).getText());
			for (int i = 0; i < mPcount; i++) {
				if (selected == i) {
					// icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_focus));
					icons[i].setBackgroundResource(R.drawable.promotion_select_focus);
				} else {
					// icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_default));
					icons[i].setBackgroundResource(R.drawable.promotion_select_default);
				}
			}
		}
	}

	private final void initBottomIcons() {
		mPcount = showings.size();
		icons = new ImageView[mPcount];
		mBottomLayout.removeAllViews();
		mBottomLayout.setWeightSum(mPcount);
		for (int i = 0; i < mPcount; i++) {
			icons[i] = new ImageView(mContext);
			if (i == 0) {
				// icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_focus));
				icons[i].setBackgroundResource(R.drawable.promotion_select_focus);
			} else {
				// icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_default));
				icons[i].setBackgroundResource(R.drawable.promotion_select_default);
			}
			mBottomLayout.addView(icons[i]);
		}
	}

	private final class PromotionImagesAdapter extends BaseAdapter {
		private AsyncImageLoader asyncImageLoader;
		private List<ListViewGalleryImageBean> smalllist = new ArrayList<ListViewGalleryImageBean>();

		public PromotionImagesAdapter(Context context,
				List<ListViewGalleryImageBean> aList) {
			asyncImageLoader = new AsyncImageLoader();
			smalllist = aList;
		}

		public int getCount() {
			return smalllist.size();
		}

		public Object getItem(int position) {
			return smalllist.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, final View convertView,
				ViewGroup parent) {
			final ListViewGalleryImageBean promotion = smalllist.get(position);
			ImageView promotionImage = (ImageView) convertView;
			if (promotionImage == null) {
				promotionImage = new ImageView(mContext);
			}
			String curr_URL = promotion.getImageurlString();
			promotionImage.setTag(curr_URL);
			Drawable cachedImage = asyncImageLoader.loadDrawable(mContext,
					curr_URL, new AsyncImageLoader.ImageCallback1() {

						public void imageLoaded(Drawable imageDrawable,
								String imageUrl, ImageView mImageView) {
							if (mImageView != null && imageDrawable != null) {
								mImageView.setImageDrawable(imageDrawable);
								notifyDataSetChanged();
							}
						}
					}, promotionImage);
			if (cachedImage != null) {
				promotionImage.setImageDrawable(cachedImage);
			} else {
				promotionImage.setImageResource(R.drawable.defaultejoycoupon);
			}
			promotionImage.setAdjustViewBounds(true);
			promotionImage.setLayoutParams(new Gallery.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			return promotionImage;
		}
	}
}


调用:

package com.reacnn.android.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.reacnn.android.R;
import com.reacnn.android.bean.UserCard;
import com.reacnn.android.bitmapoperation.FinalBitmap;
import com.reacnn.android.config.HttpConstant;
import com.reacnn.android.utils.LogUtil;
import com.reacnn.android.utils.MessageBox;
import com.reacnn.android.utils.StringUtils;
import com.reacnn.android.view.listview.XListView.IXListViewListener;
import com.reacnn.android.view.listviewgallery.ReacnnListView;
import com.reacnn.android.view.listviewgallery.ReacnnListViewGallery;
import com.reacnn.android.view.listviewgallery.ReacnnListViewGallery.GalleryItemClickedListener;
import com.reacnn.android.view.listviewgallery.bean.ListViewGalleryImageBean;

import android.R.xml;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * 首页显示内置的第一页产品展示页面
 * 
 * @author tom
 * 
 */
public class FrmInternalMainProductActivity extends BaseActivity implements
		IXListViewListener {
	private static final String TAG = "com.reacnn.android.activity.FrmInternalMainProductActivity";
	private ReacnnListView mXListView = null;
	private List<UserCard> mList = new ArrayList<UserCard>();

	List<ListViewGalleryImageBean> showings = null;
	FinalBitmap mFinalBitmap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_internal_mainhomeone);
		mFinalBitmap = FinalBitmap.create(FrmInternalMainProductActivity.this);
		mXListView = (ReacnnListView) findViewById(R.id.activity_internal_mainhomeone_mainlistview);
		mXListView.setPullLoadEnable(true);
		mXListView.setXListViewListener(this);
		mXListView.SetVisableFootView();
		mList = GetData();

		showings = new ArrayList<ListViewGalleryImageBean>();
		ListViewGalleryImageBean show1 = new ListViewGalleryImageBean();
		show1.setImageurlString("http://img.carschina.com/uploads/110729/162_162459_4.jpg");
		show1.setText("baidu");

		ListViewGalleryImageBean show2 = new ListViewGalleryImageBean();
		show2.setImageurlString("http://img.cheshi-img.com/201205/563588/4fa0d40715f30.jpg");
		show2.setText("iteye");

		ListViewGalleryImageBean show3 = new ListViewGalleryImageBean();
		show3.setImageurlString("http://csdnimg.cn/www/images/csdnindex_logo.gif");
		show3.setText("csdn");

		showings.add(show1);
		showings.add(show2);
		showings.add(show3);

		mXListView.setAdapter(new NormalViewListAdapter(
				FrmInternalMainProductActivity.this));
		mXListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				MessageBox.ShowMakeText(
						FrmInternalMainProductActivity.this,
						"onItemClick\t arg2=" + arg2 + "\t"
								+ mList.get(arg2 - 1).getShopname());
			}
		});
	}

	/**
	 * 停止XListview的刷新跟加载更多
	 */
	private void onLoad() {
		mXListView.stopRefresh();
		mXListView.stopLoadMore();
	}

	@Override
	public void onRefresh() {
		MessageBox.ShowMakeText(FrmInternalMainProductActivity.this,
				"onRefresh");
		onLoad();
	}

	@Override
	public void onLoadMore() {
		MessageBox.ShowMakeText(FrmInternalMainProductActivity.this,
				"onLoadMore");
		onLoad();
	}

	/**
	 * 获取ListView的数据源
	 * 
	 * @return
	 */
	List<UserCard> GetData() {
		try {
			List<UserCard> alist = new ArrayList<UserCard>();
			for (int k = 0; k < 30; k++) {
				UserCard mCard = new UserCard();
				mCard.setAddress("address" + k);
				mCard.setShopname("shopname:" + k);
				mCard.setUsetimes(k + 1);
				mCard.setTypeName("typename:" + k);
				mCard.setPhoto("http://img.cheshi-img.com/201205/563588/4fa0d40715f30.jpg");
				alist.add(mCard);
			}
			return alist;
		} catch (Exception e) {
			LogUtil.e(TAG, "", e, true);
			return new ArrayList<UserCard>();
		}
	}

	/**
	 * 
	 * @author tom
	 * 
	 */
	final class holederview {
		ImageView NormaleftImageView;
		TextView shopnameTextView;
		TextView typenameTextView;
		TextView countnumTextView;
		ReacnnListViewGallery gallery;
	}

	/***
	 * ListView适配器
	 * 
	 * @author tom
	 */
	class NormalViewListAdapter extends BaseAdapter {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA);
		LayoutInflater mInflater = null;

		public NormalViewListAdapter(Context mContext) {
			mInflater = LayoutInflater.from(mContext); // 加载一个空页
		}

		public int getCount() {
			return mList.size();
		}

		public Object getItem(int position) {
			return mList.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (mList == null) {
				return null;
			}
			holederview ww = null;
			if (position == 0) {
				ww = new holederview();
				convertView = mInflater.inflate(
						R.layout.layout_internal_mainhome_gallery, null);
				ww.gallery = (ReacnnListViewGallery) convertView
						.findViewById(R.id.mainhomereacnnListViewGallery);
				ww.gallery.SetAdapter(showings);
				final holederview jhHolder = ww;
				mXListView.setOnTouchListener(new View.OnTouchListener() {
					public boolean onTouch(View arg0, MotionEvent event) {
						jhHolder.gallery.onTouchEvent(event);
						return false;
					}
				});
				ww.gallery
						.SetOnItemClickedLisener(new GalleryItemClickedListener() {

							@Override
							public void OnItemClicked(
									AdapterView<?> arg0,
									View arg1,
									int arg2,
									long arg3,
									ListViewGalleryImageBean mListViewGalleryImageBean) {
								String str = "";
								if (mListViewGalleryImageBean != null) {
									str += mListViewGalleryImageBean
											.getImageurlString();
								}
								str += "Gallery.click \t arg2=" + arg2;
								MessageBox.ShowMakeText(
										FrmInternalMainProductActivity.this,
										str);

							}
						});
			} else {
				// if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.layout_item_internal_mainhomeone_listview,
						null);
				ww = new holederview();
				ww.NormaleftImageView = (ImageView) convertView
						.findViewById(R.id.mycardlist_leftimg);
				ww.countnumTextView = (TextView) convertView
						.findViewById(R.id.mycardlist_countsum);
				ww.shopnameTextView = (TextView) convertView
						.findViewById(R.id.mycardlist_title);
				ww.typenameTextView = (TextView) convertView
						.findViewById(R.id.mycardlist_titleinfo);
				convertView.setTag(ww);

				final UserCard mUserCard = mList.get(position);
				ImageView mview = ww.NormaleftImageView;
				ww.countnumTextView.setText(mUserCard.getUsetimes() + "次使用");
				ww.shopnameTextView.setText(mUserCard.getShopname());
				ww.typenameTextView.setText(mUserCard.getTypeName());
				if (!StringUtils.IsEmptyOrNull(mUserCard.getPhoto())) {
					LogUtil.e(TAG, HttpConstant.strImagePathString
							+ mUserCard.getPhoto().toString(), false);
					mFinalBitmap.display(mview, HttpConstant.strImagePathString
							+ mUserCard.getPhoto().toString());
				} else {
					mview.setImageResource(R.drawable.defaultejoycoupon);
				}
			}
			return convertView;
		}
	}

}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值