I have an android gridview which i'm using some custom scrolling going on in, to let it scroll in two dimensions - this means that the default scrolling isn't called.
I suspect this may be the reason that the rows that are off-screen are invisible. I know they're there, they affect the layout and everything, but they never draw.
So my question is this - is there any way to force the gridview to draw all of its tiles when it's loaded, and not just the visible ones?
Thanks.
Edit: To clarify - In my tileadapter, i set the child count to exactly 225. In my gridview, a call to getChildCount() returns 165.
Edit again: This only happens when the height of the gridview is greater than that of the screen - the children that are off-screen on the y axis are simply subtracted from the childcount - setting the size of the children to a number where they all fit snugly on screen removes the problem, but kills the purpose of scrolling.
Code!
XML Layout of activity:
<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>
Activity:
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));
}
}
GameGrid:
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));
}
You may notice i'm defining a small and a large size here - double tapping the screen allows you to switch between the two.
Scrolling (what you helped me with earlier)
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 is just a helper method for printing debug messages to LogCat
TileAdapter:
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;
}
myGridView.invalidate()
to the end yourACTION_MOVE:
in youronTouchEvent()
(after the scrollBy() statement....) – Crossruff