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