首页 > 代码库 > 关于android中gridview数据量很大的时候,在加载gridview时会出现卡顿的现象

关于android中gridview数据量很大的时候,在加载gridview时会出现卡顿的现象

好的解决办法就是先加载一定数量的数据,然后在最下方提示正在加载!
动态加载就是把放入adapter中的数据分好几次加载。在用户拖动gridview时再加载一定的数据,和sina微博的客户端类似。
给gridview添加OnScrollListener监听事件默认会覆盖下面两个方法:

 

下面列举个列子:

<com.ui.widget.LazyGridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:listSelector="@android:color/transparent"
    android:numColumns="2"
    android:padding="20dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />


lazyGridView:

/**
 * 滚动到底部自动刷新
 * 
 * @author yinghui.hong
 * 
 */
public class LazyGridView extends GridView implements OnScrollListener {

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

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

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

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

	}

	/**
	 * 列表视图滚动
	 */
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		switch (scrollState) {
		case OnScrollListener.SCROLL_STATE_IDLE:
			Log.v("onScrollStateChanged", "已经停止:SCROLL_STATE_IDLE");
			// 判断滚动到底部
			if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
				Log.v("onScrollStateChanged", "到底部啦。可以请求刷新咯~~~~~~");
				if (listener != null) {
					listener.onScrollBottom();
				}
			}
			break;
		case OnScrollListener.SCROLL_STATE_FLING:
			Log.v("onScrollStateChanged", "开始滚动:SCROLL_STATE_FLING");
			break;
		case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
			Log.v("onScrollStateChanged", "正在滚动:SCROLL_STATE_TOUCH_SCROLL");
			break;
		}

	}

	private OnScrollBottomListener listener;

	public void setOnScrollBottomListener(OnScrollBottomListener listener) {
		this.setOnScrollListener(this);
		this.listener = listener;
	}

	public void removeOnScrollBottomListener() {
		listener = null;
		System.out.println("removeOnScrollBottomListener");
	}

	/**
	 * 列表视图滚动到底部监听器
	 * 
	 * @author yinghui.hong
	 * 
	 */
	public interface OnScrollBottomListener {
		/**
		 * 列表视图滚动到底部时响应
		 */
		public void onScrollBottom();
	}
}


主activity:

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_circle);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		mBeans = new ArrayList<CircleResBean>();
		mAdapter = new CircleAdapter(this, mBeans);
		gridView = (LazyGridView) findViewById(R.id.gridview);
		gridView.setAdapter(mAdapter);

		gridView.setOnScrollBottomListener(this);
		mPageIndex = 0;
		queryJoinedCircles(mPageIndex);
	}