Android交错的gridview实现

时间:2013-08-29 18:51:37

标签: android android-gridview

我正在尝试实现交错的网格视图库。我正在使用类似于普通gridview实现的方法,如下所示:

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;
    }

}

和适配器就是这个

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
    };
    }

,xml代码就是这个

<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>

但这不起作用,因为我认为我们需要在这里使用staggeredadapter来添加图像。我不知道如何在这种情况下使用它以及如何将其添加到我的代码中。 请帮帮我 感谢

2 个答案:

答案 0 :(得分:1)

看起来你正在使用staggeredgridview from maurycyw

为什么不好好看看its sample project here

答案 1 :(得分:0)

首先,您应该将此适配器添加到StaggeredGridView中,如此

@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);

}

我认为重要的是,在最新版本的StaggeredGridView上,方法serAdapter正在等待ListAdapter并且您正在扩展BaseAdapter,它是它的子类

  

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

也许与它无关,但以防万一。

希望它有所帮助。

祝你好运!