1. 程式人生 > >自動輪播,類似字幕或圖片自動滾動播放

自動輪播,類似字幕或圖片自動滾動播放

功能:ListView沒隔一段時間自動滾動一段距離,當滾動到底部時,又從頭開始顯示,類似於原始資料為0123,而滾動起來則是012301230123...一直重複顯示,這樣就達到了資訊輪播的功能。

本功能採用擴充套件ListView來實現的,

public class AutoCircleScrollListview extends ListView implements OnScrollListener{
	public final static int REFRESH_TIMES=1000;//滾動頻率,單位毫秒
	public final static int SCROLL_DISTANCE=10;//每次滾動距離,單位是pixels
	private Handler mHandler=new Handler();
	private RefreshRunnable mRefreshRunnable;
	private boolean isStop=false;
	
	public AutoCircleScrollListview(Context context) {
		super(context);
		init();
	}
	public AutoCircleScrollListview(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private void init(){
		//設定滾動監聽器
		setOnScrollListener(this);
		//不顯示滾動條
		setVerticalScrollBarEnabled(false);
		setHorizontalScrollBarEnabled(false);
		//不可觸控
		setEnabled(false);
	}
	/**
	 * 開始滾動
	 */
	public void startRoll(){
		isStop=false;
		setSelection(getCount()/AutoCircleScrollAdapter.REPEAT_TIMES);
		if(mRefreshRunnable==null){
			mRefreshRunnable=new RefreshRunnable();
		}
		mHandler.postDelayed(mRefreshRunnable, REFRESH_TIMES);
	}
	/**
	 * 停止滾動
	 */
	private void stopRoll(){
		isStop=true;
		if(mRefreshRunnable!=null){
			mHandler.removeCallbacks(mRefreshRunnable);
		}
		mRefreshRunnable=null;
	}
	/**
	 * 
	 * @Create_date 2014-10-24 下午1:02:50
	 * @TODO
	 */
	private class RefreshRunnable implements Runnable{

		@Override
		public void run() {
			if(!isStop){
				smoothScrollBy(SCROLL_DISTANCE, 0);
				mHandler.postDelayed(this, REFRESH_TIMES);
			}
		}
		
	}
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if (firstVisibleItem <= 2) {//到頂部
			int listSize=getCount()/AutoCircleScrollAdapter.REPEAT_TIMES;
			setSelection(listSize + 2);
		} else if (firstVisibleItem + visibleItemCount > getCount() - 2) {//到底部
			int listSize=getCount()/AutoCircleScrollAdapter.REPEAT_TIMES;
			setSelection(firstVisibleItem - listSize);
		}
	}
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		
	}
	
	public void onStop(){
		stopRoll();
	}
	
	public void onResume(){
		if(isStop){
			startRoll();
		}
	}
	public void onDestroy(){
		stopRoll();
	}
}
介面卡
public class AutoCircleScrollAdapter extends BaseAdapter {
	/**
	 * 迴圈展示次數,最理想的值為3
	 */
	public static final int REPEAT_TIMES = 3;
	private List<Map<String, Object>> mDatas;
	private int mLayoutId;
	private String[] mFrom;
	private int[] mTo;
	private Context mContext;

	public AutoCircleScrollAdapter(Context context, int resLayoutId,
			String[] from, int[] to, List<Map<String, Object>> datas) {
		this.mContext = context;
		this.mDatas = datas;
		this.mLayoutId = resLayoutId;
		this.mFrom = from;
		this.mTo = to;
	}

	/**
	 * 將資料迴圈展示三遍
	 */
	@Override
	public int getCount() {
		if (mDatas != null) {
			return mDatas.size() * REPEAT_TIMES;
		}
		return 0;
	}

	@Override
	public Object getItem(int arg0) {

		return mDatas.get(arg0 % mDatas.size());
	}

	@Override
	public long getItemId(int arg0) {
		return arg0 % mDatas.size();
	}

	@Override
	public View getView(int postition, View convertView, ViewGroup arg2) {
		View v;
		if (convertView == null) {
			v = LayoutInflater.from(mContext).inflate(mLayoutId, null);
		} else {
			v = convertView;
		}
		bindView(v, postition);
		return v;
	}

	private void bindView(View view, int position) {
		final Map<String, Object> dataSet = mDatas.get(position%mDatas.size());
		if (dataSet == null) {
			return;
		}
		final String[] from = mFrom;
		final int[] to = mTo;
		final int count = to.length;
		for (int i = 0; i < count; i++) {
			final View v = view.findViewById(to[i]);
			if (v != null) {
				String clzzName = v.getClass().getSimpleName();
				if (clzzName.equals("TextView")) {// 設定文字框內容
					final Object data = dataSet.get(from[i]);
					String text = data == null ? "" : data.toString();
					if (text == null) {
						text = "";
					}
					((TextView) v).setText(text);
				}else {//在此可以拓展自己的view
					
				}
			}
		}
	}
}

使用
public class MainActivity extends Activity {
	AutoCircleScrollListview mAutoCircleScrollListview;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mAutoCircleScrollListview=(AutoCircleScrollListview) findViewById(R.id.lv_autoCircleScroll);
		List<Map<String, Object>> datas=new ArrayList<Map<String,Object>>();
		for (int i = 0; i < 5; i++) {
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("txt_tire_index", ""+i);
			map.put("txt_pressure_value", "1"+i+".0");
			map.put("txt_tmper_valude", "01"+i+"'C");
			datas.add(map);
		}
		AutoCircleScrollAdapter adapter=new AutoCircleScrollAdapter(
				this, 
				R.layout.item_auto_circle_scroll_listview, 
				new String[]{"txt_tire_index","txt_pressure_value","txt_tmper_valude"}, 
				new int[]{R.id.txt_tire_index,R.id.txt_pressure_value,R.id.txt_tmper_valude}, 
				datas);
		mAutoCircleScrollListview.setAdapter(adapter);
		mAutoCircleScrollListview.startRoll();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mAutoCircleScrollListview.onDestroy();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mAutoCircleScrollListview.onResume();
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		mAutoCircleScrollListview.onStop();
	}

}

Demo下載