我有一个android gridview,我正在使用一些自定义滚动,让它在二维中滚动——这意味着不调用默认滚动。
我怀疑这可能是屏幕外的行不可见的原因。我知道他们在那里,他们影响布局和一切,但他们从不画画。
所以我的问题是——有没有办法强制gridview在加载时绘制它的所有图块,而不仅仅是可见的图块?
谢谢。
编辑:澄清 - 在我的 tileadapter 中,我将子计数设置为 225。在我的 gridview 中,对 getChildCount() 的调用返回 165。
再次编辑:这只发生在gridview的高度大于屏幕的高度时 - y轴上不在屏幕上的孩子只是从childcount中减去 - 将孩子的大小设置为他们全部的数字紧贴屏幕可以消除问题,但会破坏滚动的目的。
代码!
活动的 XML 布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:theme="@style/Theme.Custom"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/logmessage"
android:theme="@style/Theme.Custom"
android:layout_width="fill_parent"
android:layout_height="25dip"
android:text="LogMessage"/>
<RelativeLayout android:id="@+id/boardwrap"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center_vertical">
<com.MyProject.GameGrid
android:id="@+id/board"
android:theme="@style/Theme.Custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="15"
android:stretchMode="none"
android:verticalSpacing="0dip"
android:horizontalSpacing="0dip"
android:padding="0dip"
android:columnWidth="20dip"
android:scrollbars="none"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="60dip"
android:background="#FFFFFFFF"/>
</LinearLayout>
活动:
public class GameBoardActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameboard);
GameGrid Board = (GameGrid)findViewById(R.id.board);
Board.setAdapter(new TileAdapter(this));
}
}
游戏网格:
public GameGrid(Context context, AttributeSet attrs) {
super(context, attrs);
this.setNumColumns(15);
DisplayMetrics metrics = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
scale = metrics.density;
smallSize = Math.round(20 * scale);
largeSize = Math.round(40 * scale);
columnwidth = largeSize;
this.setColumnWidth(columnwidth);
Common.DebugMessage(Float.toString(columnwidth));
}
您可能会注意到我在这里定义了一个小尺寸和一个大尺寸 - 双击屏幕可让您在两者之间切换。
滚动(你之前帮助过我的)
if (myState == TOUCH_STATE_SCROLLING) {
final int deltaX = (int) (mLastX - x);
final int deltaY = (int) (mLastY - y);
mLastX = x;
mLastY = y;
int xpos = this.getScrollX();
int ypos = this.getScrollY();
int maxX = (columnwidth * 15) - super.getWidth();
int maxY = (columnwidth * 15) - super.getHeight();
if (xpos + deltaX >= 0 && xpos + deltaX <= maxX && ypos + deltaY >= 0 && ypos + deltaY <= maxY )
{
this.scrollBy(deltaX, deltaY);
}
else {
this.scrollTo(xpos + deltaX <= 0 ? 0 : xpos + deltaX >= maxX ? maxX : xpos + deltaX,
ypos + deltaY <= 0 ? 0 : ypos + deltaY >= maxY ? maxY : ypos + deltaY);
}
Common.DebugMessage(this.getChildCount());
}
Common.DebugMessage 只是将调试消息打印到 LogCat 的辅助方法
瓷砖适配器:
public TileAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
return 225;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
int colWidth = ((GameGrid)parent).getColumnWidth();
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(colWidth , colWidth));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
}
else {
imageView = (ImageView)convertView;
}
imageView.setImageResource(R.drawable.tile);
return imageView;
}