RecyclerView.LayoutManager似乎是一個可能的解決方案。但在這個時候改變我的設計非常困難,以符合RecyclerView.LayoutManager
的方法。
因此,我制定了一項適合自己的工作。滾動是非常順利的,我不必改變很多代碼。 張貼在這裏爲我自己和其他人蔘考。
基本上我創建所有的子視圖,並將它們添加到我的自定義Viewgroup中。但是,我重寫了dispatchDraw
方法將繪圖限制爲僅可見的那些。
此處不使用回收方法。這只是一個巨大的Viewgroup
包裹在Scrollview中。
@Override
protected void dispatchDraw(Canvas canvas) {
int clipSaveCount = canvas.save();
canvas.clipRect(getScrollX() + getPaddingLeft(), getScrollY() + getPaddingTop(),
getScrollX() + getRight() - getLeft() - getPaddingRight(),
getScrollY() + getBottom() - getTop() - getPaddingBottom());
int rowHeightNoGap = (getHeight() - getPaddingTop() - getPaddingBottom())/mDataScreen.getRowCount();
int startIndex = Math.max(0, (mScrollPosY - getPaddingTop())/rowHeightNoGap * mDataScreen.getColumnCount()-1);
int endIndex = Math.min(getChildCount()-1, startIndex + (mDataScreen.getVisibleRowCount()+1) * mDataScreen.getColumnCount());
final long drawingTime = getDrawingTime();
for (int i = startIndex; i <= endIndex; i++) {
drawChild(canvas, getChildAt(i), drawingTime);
}
canvas.restoreToCount(clipSaveCount);
}
int mScrollPosY;
public void setScrollPosition(int scrollPosY){
mScrollPosY = scrollPosY;
invalidate();
}
''我知道爲此,我必須重新使用視圖「'所以使用'RecyclerView'。 docs:「Class Overview 一個靈活的視圖,用於爲大型數據集提供有限的窗口。」 – pskink
我認爲Recyclerview也不能使用,因爲子項目不完全是垂直列表。我把孩子的觀點排列成一個交錯的網格。 :( –
請參閱[RecyclerView.LayoutManager](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.LayoutManager.html) – pskink