Force grid display to draw all fragments

I have an android gridview in which I use some kind of custom scroll to scroll it in two dimensions - this means that the scroll is not called by default.

I suspect this may be the reason that lines that are off the screen are invisible. I know that they are there, they affect the layout and that's it, but they never draw.

So my question is this: is there a way to get gridview to draw all its tiles at boot time, not just visible ones?

Thank.

Edit: clarify. In my tileadapter, I set the child counter to exactly 225. In my gridview, calling getChildCount () returns 165.

Change again: this happens only when the height of the grid is greater than the screen - children who are off the screen on the y axis are simply subtracted from the child account - setting the size of the children to the number where they all fit snugly on the screen eliminates the problem, but kills the scroll target.

the code

XML layout action:

  <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 that I define small and large size here - double-clicking the screen allows you to switch between them.

Scrolling (what you helped me before)

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 in 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;
    }
+5
source share
4 answers

, , API , . GridView, , ... , GridView .

. GridView - , , ?

GridView , , , , , . Android , GridView , (, , , SDK, , , ... , SDK), , , . , , . , , , GridView .

+4

,

onDraw(). Draw(canvas) GridView. , , . :

 //This may be used in your GridView or Activity -- whichever provides the best result.
    public void draw(Canvas canvas)
    {   int _num = myGridView.getChildCount();
        for (int _i = _num; --_i >= 0; )
        {   View _child = (View)myGridView.getChildAt(_i);
            if (_child != null)
                _child.draw(canvas);
        }
    }

(EDIT)

draw() . , , , . invalidate() , . , draw(), , -, .

//This definitely goes in your GridView
public void invalidate()
{   int _num = myGridView.getChildCount();
    for (int _i = _num; --_i >= 0; )
    {   View _child = (View)myGridView.getChildAt(_i);
        if (_child != null)
            _child.invalidate();
    }
}

, , , , , . View, ImageView , GridView, . draw() . , invalidate() , .

. , Lazy Loading . - , , , , . Lazy Loading, . . GridView.

( )

, , -, , , Force Close ( , ). , . - LogCat . LogCat ( , ).

, . , , . , . , , . . (, 2x2 16 , 4x4 64 [400% !].)

, System.gc() , , . ( , , ).

BEST, , , , . , , draw() onMeasure() onLayout() , , .

+5

, , , . , .

:

GridView (kinda), , . , , . " ".

Android

Android , , , , . , , Android , , . .

, . (YAY!) , Android ( ) " , !" , , Android , .

GridView GridView. GridView. Android View Android , , soooo.... GridView . : " RelativeLayout. ?" - . . , .

, . Android . all, , , , . reset, . .

, "" , ...

( I):

, , GridView, . , . , . , , . , ( ).

:

, - . , Adapter TileAdapter . , . , .

, ( ). , , . ImageViews , , , .

- , , , , .

-, , , "" . ChildCount, , . , . , , . - , . , . , , . , GameGrid.:)

, , : ", ".

0
source

override getCount()your adapter and return the length of the base array for your adapter. Then use mAdapter.getCount instead of mGrid.getChildCount. getChildCount returns only visible children, while getCount will give you the total number of children in your dataset.

0
source

All Articles