Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 縱向跑馬燈滾動效果

Android 縱向跑馬燈滾動效果

編輯:關於Android編程

像淘寶和京東都會有跑馬燈的效果,今天給大家貢獻下以前項目的一個demo,各位看官,且看效果圖。

\\\

我們先定義一個Bean文件,這個實體類文件主要包含標題,內容描述,以及還有跳轉的鏈接。

LampBean

public class LampBean implements Serializable {
	public String title;
	public String info;
}

接下來我們要去自定義一個LampView了,Lamp主要實現數據的接受,適配器填充數據,以及滾動實現,這裡可以看出適配器模式在我們Android開發模式中用處的廣泛,為了方便後面的數據的適配我們將數據源用泛型。
public abstract class BaseAutoScrollTextView extends ListView implements
		AutoScrollData {

	private ArrayList mDataList = new ArrayList();
	private float mSize=16;
	private int mMax;
	private int position = -1;
	private int scroll_Y;
	private int mScrollY;
	private AutoScrollAdapter mAutoScrollAdapter = new AutoScrollAdapter();
	private OnItemClickListener mOnItemClickListener;
	private long mTimer = 1000;
	private Context mContext;

	protected abstract int getAdertisementHeight();

	private Handler handler = new Handler();
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			// 開啟輪播
			switchItem();
			handler.postDelayed(this, mTimer);
		}
	};

	public interface OnItemClickListener {
		public void onItemClick(int position);
	}

	public BaseAutoScrollTextView(Context context, AttributeSet attrs,
								  int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		mScrollY = dip2px(getAdertisementHeight());
		init();

	}

	public BaseAutoScrollTextView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public BaseAutoScrollTextView(Context context) {
		this(context, null);
	}

	private void init() {
		this.setDivider(null);
		this.setFastScrollEnabled(false);
		this.setDividerHeight(0);
		this.setEnabled(false);
	}

	private int dip2px(float dipValue) {
		final float scale = mContext.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	private void switchItem() {
		if (position == -1) {
			scroll_Y = 0;
		} else {
			scroll_Y = mScrollY;
		}
		smoothScrollBy(scroll_Y, 2000);
		setSelection(position);
		position++;
	}

	private class AutoScrollAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			final int count = mDataList == null ? 0 : mDataList.size();
			return count > 1 ? Integer.MAX_VALUE : count;
		}

		@Override
		public Object getItem(int position) {
			return mDataList.get(position % mMax);
		}

		@Override
		public long getItemId(int position) {
			return position % mMax;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder viewHolder;
			if (null == convertView) {
				viewHolder = new ViewHolder();
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_lamp_layout, null);
				viewHolder.mTitleView = (TextView) convertView
						.findViewById(R.id.tv_title);
				viewHolder.mInfoView = (TextView) convertView
						.findViewById(R.id.tv_info);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			T data = mDataList.get(position % mMax);
			initItemData(position, convertView, viewHolder, data);
			return convertView;
		}
	}

	private void initItemData(final int position, View convertView, ViewHolder viewHolder, T data) {
		viewHolder.mTitleView
				.setLayoutParams(new RelativeLayout.LayoutParams(
						RelativeLayout.LayoutParams.WRAP_CONTENT,
						dip2px(getAdertisementHeight())));
		viewHolder.mTitleView.setTextSize(mSize);
		viewHolder.mInfoView.setTextSize(mSize);
		viewHolder.mTitleView.setText(getTextTitle(data));
		viewHolder.mInfoView.setText(getTextInfo(data));
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mOnItemClickListener.onItemClick(position % mMax);
			}
		});
	}

	static class ViewHolder {
		TextView mTitleView;// 標題
		TextView mInfoView;// 內容
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		return false;
	}

	public void setData(ArrayList _datas) {
		mDataList.clear();
		mDataList.addAll(_datas);
		mMax = mDataList == null ? 0 : mDataList.size();
		this.setAdapter(mAutoScrollAdapter);
		mAutoScrollAdapter.notifyDataSetChanged();
	}
	public void setTextSize(float _size){
		this.mSize=_size;
	}

	public void setOnItemClickListener(OnItemClickListener _listener) {
		this.mOnItemClickListener = _listener;
	}

	public void setTimer(long _time) {
		this.mTimer = _time;
	}

	public void start() {
		handler.postDelayed(runnable, 1000);
	}

	public void stop() {
		handler.removeCallbacks(runnable);
	}

}

然後添加一個實現的類:
public class VerticalLampView extends
		BaseAutoScrollTextView {

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

	public VerticalLampView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

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

	@Override
	public String getTextTitle(LampBean data) {
		return data.title;
	}

	@Override
	public String getTextInfo(LampBean data) {
		return data.info;
	}

	/**
	 * 這裡面的高度應該和你的xml裡設置的高度一致
	 */
	@Override
	protected int getAdertisementHeight() {
		return 40;
	}

}

最後是測試代碼:
public class LampView extends FrameLayout {

    @Bind(R.id.lamp_view)
    VerticalLampView lampView;

    private Context mContext = null;
    private List list=new ArrayList<>();

    public LampView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {
        setView();
    }

    private void setView() {
        inflate(getContext(), R.layout.lamp_layout, this);
        ButterKnife.bind(this, this);

        initData();
        initView();
    }

    private void initView() {
        lampView.setData((ArrayList) list);
        lampView.setTextSize(15);
        lampView.setTimer(2000);
        lampView.start();

    }

    private void initData() {
        LampBean bean = new LampBean();
        bean.title = "爆款";
        bean.info = "踏青零食上京東,百萬零食1元秒";
        list.add(bean);

        bean = new LampBean();
        bean.title = "公告";
        bean.info = "看老劉中國行,滿129減50!";
        list.add(bean);

        bean = new LampBean();
        bean.title = "活動";
        bean.info = "高姿CC霜全渠道新品首發,領券199減50,點擊查看";
        list.add(bean);

    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved