Note that there are some explanatory texts on larger screens.

plurals
  1. POForce gridview to draw all tiles
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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?</p> <p>Thanks.</p> <p>Edit: To clarify - In my tileadapter, i set the child count to exactly 225. In my gridview, a call to getChildCount() returns 165.</p> <p>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.</p> <p>Code!</p> <p>XML Layout of activity:</p> <pre><code> &lt;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"&gt; &lt;TextView android:id="@+id/logmessage" android:theme="@style/Theme.Custom" android:layout_width="fill_parent" android:layout_height="25dip" android:text="LogMessage"/&gt; &lt;RelativeLayout android:id="@+id/boardwrap" android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent" android:gravity="center_vertical"&gt; &lt;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"/&gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/toolbar" android:layout_width="fill_parent" android:layout_height="60dip" android:background="#FFFFFFFF"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>Activity:</p> <pre><code>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)); } } </code></pre> <p>GameGrid:</p> <pre><code>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)); } </code></pre> <p>You may notice i'm defining a small and a large size here - double tapping the screen allows you to switch between the two.</p> <p>Scrolling (what you helped me with earlier)</p> <pre><code>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 &gt;= 0 &amp;&amp; xpos + deltaX &lt;= maxX &amp;&amp; ypos + deltaY &gt;= 0 &amp;&amp; ypos + deltaY &lt;= maxY ) { this.scrollBy(deltaX, deltaY); } else { this.scrollTo(xpos + deltaX &lt;= 0 ? 0 : xpos + deltaX &gt;= maxX ? maxX : xpos + deltaX, ypos + deltaY &lt;= 0 ? 0 : ypos + deltaY &gt;= maxY ? maxY : ypos + deltaY); } Common.DebugMessage(this.getChildCount()); } </code></pre> <p>Common.DebugMessage is just a helper method for printing debug messages to LogCat</p> <p>TileAdapter:</p> <pre><code>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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload