首页 > 代码库 > 自动轮播,类似字幕或图片自动滚动播放

自动轮播,类似字幕或图片自动滚动播放

功能: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 = http://www.mamicode.com/dataSet.get(from[i]);>
使用

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下载

自动轮播,类似字幕或图片自动滚动播放