Shivam Bhalla
Shivam Bhalla

Reputation: 1909

Android staggered gridview implementation

I am trying to implement the staggered grid view library. I am using an approach similar to the normal gridview implementation like this:

package com.example.abcd8;

import com.origamilabs.library.views.StaggeredGridView;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.GridView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StaggeredGridView sg1=(StaggeredGridView)findViewById(R.id.staggeredGridView1);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

and adapter is this

package com.example.abcd8;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

    private Context ctx;

    public ImageAdapter(Context c)
    {
        ctx=c;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return pics.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView iv;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            iv = new ImageView(ctx);
            iv.setLayoutParams(new GridView.LayoutParams(150,150));
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iv.setPadding(8, 8, 8, 8);
        } else {
            iv = (ImageView) convertView;
        }
        iv.setImageResource(pics[position]);
        return iv;

    }

    private Integer[] pics={
            R.drawable.sample_0,R.drawable.sample_1,
            R.drawable.sample_2,R.drawable.sample_3,
            R.drawable.sample_4,R.drawable.sample_5,
            R.drawable.sample_6,R.drawable.sample_7
    };
    }

and the xml code is this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:staggered="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainLayout">

    <com.origamilabs.library.views.StaggeredGridView
        android:id="@+id/staggeredGridView1"
        staggered:numColumns="2"
        staggered:drawSelectorOnTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

But this doesnt work as i think we need to use a staggeredadapter here to add the images. I am not sure how to use it in this case and how to add it to my code. Please help me out here Thanks

Upvotes: 2

Views: 6916

Answers (2)

Robert
Robert

Reputation: 1710

Looks like you'e using staggeredgridview from maurycyw.

Why don't you take a good look of its sample project here?

Upvotes: 1

reixa
reixa

Reputation: 7011

First of all you should add yout adapter to the StaggeredGridView like this

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StaggeredGridView sg1=(StaggeredGridView)findViewById(R.id.staggeredGridView1);

    ArrayList<String> urls = new ArrayList<String>();
    ...
    urls.add("http://mypic.com/thispic");
    ...
    MyAdapter adapter = new MyAdapter(urls);
    sg1.setAdapter(adapter);

}

I think it´s important to say that on the latest version of StaggeredGridView the method serAdapter is waiting for a ListAdapter and you're extending BaseAdapter which is a subclass of it

http://developer.android.com/reference/android/widget/ListAdapter.html

Maybe has nothing to do with it but just in case.

Hope it helps.

Good luck!

Upvotes: 0

Related Questions