0

對於我的應用,我使用的是library,它在整個佈局中實現了自己的ScrollView。在自定義展開的GridView中回收視圖?

我的問題是,我需要有一個片段一個GridView,但這樣做會導致它的大部分被砍掉,像這樣: Why You No Work?! http://oi43.tinypic.com/vgtlvs.jpg (忽略頂部的滑塊)

我結束了創建我的自己的GridView根據行數擴展,但禁用視圖回收。由於我們使用了大量圖片,因此如果加載超過10個圖片,我們最終會得到各種各樣的OutOfMemoryException。我們也無法使用滾動時看到的動畫和內容。

這裏是我的自定義GridView控件代碼:

public class PkGridView extends GridView implements OnTouchListener, OnScrollListener 
{ 

private int listViewTouchAction; 
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 50; 

public PkGridView(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 
    listViewTouchAction = -1; 
    setOnScrollListener(this); 
    setOnTouchListener(this); 
} 

@Override 
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
{ 
    if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) 
    { 
     if (listViewTouchAction == MotionEvent.ACTION_MOVE) 
     { 
      scrollBy(0, -1); 
     } 
    } 
} 

@Override 
public void onScrollStateChanged(AbsListView view, int scrollState) 
{ 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{ 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    int newHeight = 0; 
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
    int heightSize = MeasureSpec.getSize(heightMeasureSpec); 
    if (heightMode != MeasureSpec.EXACTLY) 
    { 
     ListAdapter listAdapter = getAdapter(); 
     if (listAdapter != null && !listAdapter.isEmpty()) 
     { 
      int listPosition = 0; 
      for (listPosition = 0; listPosition < listAdapter.getCount() && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) 
      { 
       View listItem = listAdapter.getView(listPosition, null, this); 
       // now it will not throw a NPE if listItem is a ViewGroup instance 
       if (listItem instanceof ViewGroup) 
       { 
        listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
       } 
       listItem.measure(widthMeasureSpec, heightMeasureSpec); 
       newHeight += listItem.getMeasuredHeight(); 
      } 
      //newHeight += 5 * listPosition; 
     } 
     if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) 
     { 
      if (newHeight > heightSize) 
      { 
       newHeight = heightSize; 
      } 
     } 
    } 
    else 
    { 
     newHeight = getMeasuredHeight(); 
    } 
    setMeasuredDimension(getMeasuredWidth(), newHeight); 
} 

@Override 
public boolean onTouch(View v, MotionEvent event) 
{ 
    if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) 
    { 
     if (listViewTouchAction == MotionEvent.ACTION_MOVE) 
     { 
      scrollBy(0, 1); 
     } 
    } 
    return false; 
} 
} 

有什麼辦法,我可以有我的物品回收再同時仍然能夠看到整個GridView控件不擴大?

回答

1

你一定要使用你自己的適配器實現。有一個關於Android開發一個很好的訓練,在一個ListView/GridView控件提供了顯示位圖一個完整的解決方案,並完成了所有的異步圖像加載並查看回收:

http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

在getView您的自定義適配器(... )你只需要相應地使用ViewHolder模式,然後調用loadBitmap(...),你就完成了。請參閱有關創建自定義適配器本教程:

http://www.vogella.com/articles/AndroidListView/article.html#adapterown_custom

對於自定義視圖,也許這一個很有趣也:

http://developer.android.com/training/custom-views/index.html

+0

其實,我已經用我自己的適配器和畢加索庫我用於加載圖像的效率可能高。哦,我也已經在使用ViewHolder模式。 這裏的問題是我使用的FadingActionBar庫需要GridView/ListView有一個Header。我目前正在嘗試這個[HeaderGridView](https://github.com/maurycyw/HeaderGridView)庫來解決問題,但仍然沒有運氣。 – Pkmmte