LuminiousAndroid
LuminiousAndroid

Reputation: 1577

Can GridView contain some default images?

I have a grid view shows images.In one row grid contain only three images(grid column size is 3). I am using my app in lanscape mode and wish that by default if there is nothing in grid i can visible two rows. If size of grid is greater then six items in grid that means two rows then scroll starts.

I can give a scenario:-

Image 1                            Image 2                              Image 3

Image 4                            Image 5                              Image 6

Place the images and use this in landscape mode . I need if i do not have any items in grid these six images will always show and if grid has items greater then six then this grid come in action scroll. Please help and Thanks in advance.

Upvotes: 0

Views: 668

Answers (2)

petey
petey

Reputation: 17140

Subclass GridView like below and use that custom GridView in your layout file instead, and adda field for disabling scrolling (disableScrolling) and override setAdapter to check for more than 6 items and dispatchTouchEvent to handle the scrolling ability.

public class MyGridView extends GridView {
    boolean disableScrolling = false;

    public MyGridView(Context context) {
        super(context);    
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);    
    }

    public MyGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);    
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        if (adapter!=null && adapter.getCount() < 6){
            disableScrolling = true;
        } else {
            //adapter is null or count is greater than 6
            disableScrolling = false;
        }
        super.setAdapter(adapter);    
    }
    @Override
     public boolean dispatchTouchEvent(MotionEvent ev){
        if( disableScrolling  || ev.getAction()!=MotionEvent.ACTION_MOVE){
            return true;
        }
        return super.dispatchTouchEvent(ev);
    }
}

Upvotes: 2

Ashterothi
Ashterothi

Reputation: 3282

Well, I would first construct the "default" array for the gridview of default images, then replace the images as you fill them (much like a lazy loader). and apply it to the grid view as needed.

Relevant Example: http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/

Upvotes: 2

Related Questions