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;
}
Honestly, my suggestion is to stop whacking on a platform API in a way that it is clearly not intended to be used. Even if through some contortions you managed to play enough games with the base GridView code to make it do what you want to do… how confident are you that your code will continue to work with the slightly changes to the GridView implementation as the platform evolves.
And really there is just no need to play these kinds of games. There is nothing special about GridView — it is just an implementation of a view that puts things in a grid that you can scroll horizontally through?
If GridView’s behavior is not what you want, the solution is to write your own view that does what you want. And with Android this is even easier because you can just go and take the GridView code from the open-source platform as a basis, get that compiling in your app (you will probably need to tweak a few things because the code as it stands takes doesn’t need to be written purely against the SDK so probably isn’t… but there is nothing it is doing that you can’t do in a regular app build against the SDK), and then modify that code in your app to your heart’s content, to make it do what want. Without fighting with a built-in widget that doesn’t actually do what you want. And without fear of your carefully constructed house of cards collapsing on you if the underlying GridView implementation changes in the future.