關於android中gridview資料量很大的時候,在載入gridview時會出現卡頓的現象

來源:互聯網
上載者:User

標籤:android   資料   

好的解決辦法就是先載入一定數量的資料,然後在最下方提示正在載入!
動態載入就是把放入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);}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {}/** * 列表視圖滾動 */@Overridepublic 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:

@Overrideprotected 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);}


 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.