Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android Listview下滑,自動加載數據.修改多處地方,去除重繪listvew,防止閃爍

android Listview下滑,自動加載數據.修改多處地方,去除重繪listvew,防止閃爍

編輯:關於Android編程

感謝其他大神的的源碼支持
我僅在此源碼上進行修改以及注釋。
使用方法:
//設置加載分頁數據監聽
loadingListView.setOnLoadMoreListener(new OnLoadMoreListener() {
public void onLoadMore() {
if(page<3){
}else{
loadingListView.onLoadMoreComplete();//移除加載更多布局,加載完成
loadingListView.setIsEnable(false); //ture:允許開啟下滑,自動加載數據,false不允許下滑加載數據
}
}

});



整個代碼比較簡單只有一個類

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.ferris.ferrisapp.R;

public class LoadMoreListView extends ListView implements OnScrollListener {

	private static final String TAG = "LoadMoreListView";

	/**
	 * Listener that will receive notifications every time the list scrolls.
	 */
	private OnScrollListener mOnScrollListener;
	private LayoutInflater mInflater;
	private Boolean isEnable = true;

	public Boolean getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(Boolean isEnable) {
		this.isEnable = isEnable;
	}

	// footer view
	private RelativeLayout mFooterView;
	// private TextView mLabLoadMore;
	private ProgressBar mProgressBarLoadMore;

	// Listener to process load more items when user reaches the end of the list
	private OnLoadMoreListener mOnLoadMoreListener;
	// To know if the list is loading more items
	private boolean mIsLoadingMore = false;
	private int mCurrentScrollState;

	public LoadMoreListView(Context context) {
		super(context);
		init(context);
	}

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

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

	private void init(Context context) {

		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

		// footer
		mFooterView = (RelativeLayout) mInflater.inflate(
				R.layout.load_more_footer, this, false);//加載底部view
		/*
		 * mLabLoadMore = (TextView) mFooterView
		 * .findViewById(R.id.load_more_lab_view);
		 */
		mProgressBarLoadMore = (ProgressBar) mFooterView
				.findViewById(R.id.load_more_progressBar);

		addFooterView(mFooterView);

		super.setOnScrollListener(this);//設置滾動監聽
	}

	@Override
	public void setAdapter(ListAdapter adapter) {
		super.setAdapter(adapter);
	}

	/**
	 * Set the listener that will receive notifications every time the list
	 * scrolls.
	 * 
	 * @param l
	 *            The scroll listener.
	 */
	@Override
	public void setOnScrollListener(AbsListView.OnScrollListener l) {
		mOnScrollListener = l;
	}

	/**
	 * Register a callback to be invoked when this list reaches the end (last
	 * item be visible)
	 * 
	 * @param onLoadMoreListener
	 *            The callback to run.
	 */

	public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
		mOnLoadMoreListener = onLoadMoreListener;
	}

	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if (isEnable) {//是否開啟滑動
		if (mOnScrollListener != null) {
			mOnScrollListener.onScroll(view, firstVisibleItem,
					visibleItemCount, totalItemCount);
		}

		

			if (mOnLoadMoreListener != null) {

				if (visibleItemCount == totalItemCount) {//剛好鋪滿一個屏幕,
					mProgressBarLoadMore.setVisibility(View.GONE);
					return;
				}

				boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;//滑動到底部

				if (!mIsLoadingMore && loadMore
						&& mCurrentScrollState != SCROLL_STATE_IDLE) { //判斷如果滾動到底部,並且已經停止滾動的時候,我們就顯示底部加載更多。
					mProgressBarLoadMore.setVisibility(View.VISIBLE);
					mIsLoadingMore = true; 
					onLoadMore();//回調監聽
				}

			}
		}

	}
	

	/**
	 *   禁止滑動 ,view重繪,否則導致閃爍
	 */
public void onScrollStateChanged(AbsListView view, int scrollState) {
		
		// bug fix: listview was not clickable after scroll //由於我並不需要listvie item是否可以按。所以我屏蔽掉了這個,否則有可能加載圖片會閃爍不停哦。這個地方要注意。
		if (isEnable) {//是否開啟滑動
			
//		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
//			view.invalidateViews();
//		}

		mCurrentScrollState = scrollState;

		if (mOnScrollListener != null) {
			mOnScrollListener.onScrollStateChanged(view, scrollState);
		}
		}
	}
	

	public void onLoadMore() {
		Log.d(TAG, "onLoadMore");
		if (mOnLoadMoreListener != null) {
			mOnLoadMoreListener.onLoadMore();
		}
	}

	/**
	 * Notify the loading more operation has finished
	 */
	public void onLoadMoreComplete() {
		mIsLoadingMore = false;
		mProgressBarLoadMore.setVisibility(View.GONE);
	}

	/**
	 * Interface definition for a callback to be invoked when list reaches the
	 * last item (the user load more items in the list)
	 */
	public interface OnLoadMoreListener {
		/**
		 * Called when the list reaches the last item (the last item is visible
		 * to the user)
		 */
		public void onLoadMore();
	}

}




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