对于我的应用程序,我使用了一个在整个布局中实现它自己的 ScrollView的库。
我的问题是我需要在片段中有一个 GridView ,但这样做会导致大部分片段像这样被砍掉:( 忽略顶部的滑块)
我最终创建了自己的 GridView,它根据行数进行扩展,但禁用了视图回收。由于我们使用了很多图像,如果我们加载超过 10 个图像,我们最终会得到各种 OutOfMemoryExceptions。我们也不能使用您在滚动时看到的动画和其他东西。
这是我的自定义 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 而无需扩展?