0

我们可以在一个xml文件中使用两个gridview吗?它只显示第一个gridview ..我如何一起使用两个gridview。我想在第一个gridview中显示可绘制文件夹中的图片,并在第二个gridview中显示来自url的图片。请指导我..

这是gridview.xml——

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal" >

  <GridView
    android:id="@+id/gridview"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:columnWidth="100dp"
    android:gravity="center"
    android:horizontalSpacing="1dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="1dp" >
  </GridView>

  </LinearLayout>

这是在gridview 中显示图片的imageadapter 类。

   public class image extends BaseAdapter {
private Context c;
int h = 0;
public Integer[] pic = { R.drawable.i1, R.drawable.i2, R.drawable.i3,
        R.drawable.i4, R.drawable.i5, R.drawable.i6 };

public final static String[] imageThumbUrls = new String[] {
              "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s160-c/A%252520Photographer.jpg","https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s160-c/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",        "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s160-c/Another%252520Rockaway%252520Sunset.jpg","https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s160-c/Antelope%252520Butte.jpg",       "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s160-c/Antelope%252520Hallway.jpg","https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s160-c/Antelope%252520Walls.jpg",            "https://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s160-c/Apre%2525CC%252580s%252520la%252520Pluie.jpg","https://lh3.googleusercontent.com/-s-AFpvgSeew/URquc6dF-JI/AAAAAAAAAbs/Mt3xNGRUd68/s160-c/Backlit%252520Cloud.jpg",       "https://lh5.googleusercontent.com/-bvmif9a9YOQ/URquea3heHI/AAAAAAAAAbs/rcr6wyeQtAo/s160-c/Bee%252520and%252520Flower.jpg","https://lh5.googleusercontent.com/-n7mdm7I7FGs/URqueT_BT-I/AAAAAAAAAbs/9MYmXlmpSAo/s160-c/Bonzai%252520Rock%252520Sunset.jpg","https://lh6.googleusercontent.com/-4CN4X4t0M1k/URqufPozWzI/AAAAAAAAAbs/8wK41lg1KPs/s160-c/Caterpillar.jpg","https://lh3.googleusercontent.com/-rrFnVC8xQEg/URqufdrLBaI/AAAAAAAAAbs/s69WYy_fl1E/s160-c/Chess.jpg","https://lh5.googleusercontent.com/-WVpRptWH8Yw/URqugh-QmDI/AAAAAAAAAbs/E-MgBgtlUWU/s160-c/Chihuly.jpg","https://lh5.googleusercontent.com/-0BDXkYmckbo/URquhKFW84I/AAAAAAAAAbs/ogQtHCTk2JQ/s160-c/Closed%252520Door.jpg","https://lh3.googleusercontent.com/-PyggXXZRykM/URquh-kVvoI/AAAAAAAAAbs/hFtDwhtrHHQ/s160-c/Colorado%252520River%252520Sunset.jpg","https://lh3.googleusercontent.com/-ZAs4dNZtALc/URquikvOCWI/AAAAAAAAAbs/DXz4h3dll1Y/s160-c/Colors%252520of%252520Autumn.jpg",           "https://lh4.googleusercontent.com/-GztnWEIiMz8/URqukVCU7bI/AAAAAAAAAbs/jo2Hjv6MZ6M/s160-c/Countryside.jpg","https://lh4.googleusercontent.com/-bEg9EZ9QoiM/URquklz3FGI/AAAAAAAAAbs/UUuv8Ac2BaE/s160-c/Death%252520Valley%252520-%252520Dunes.jpg","https://lh6.googleusercontent.com/-ijQJ8W68tEE/URqulGkvFEI/AAAAAAAAAbs/zPXvIwi_rFw/s160-c/Delicate%252520Arch.jpg",         "https://lh5.googleusercontent.com/-Oh8mMy2ieng/URqullDwehI/AAAAAAAAAbs/TbdeEfsaIZY/s160-c/Despair.jpg",    "https://lh5.googleusercontent.com/-gl0y4UiAOlk/URqumC_KjBI/AAAAAAAAAbs/PM1eT7dn4oo/s160-c/Eagle%252520Fall%252520Sunrise.jpg"};

public image(Context c1) {
    Log.i("iamgeclass", "create");
    c = c1;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return pic.length;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return pic[arg0];
}

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

@Override
    public View getView(int pos, View arg1, ViewGroup arg2) {
    // TODO Auto-generated method stub
    ImageView i;
    i = new ImageView(c);
    i.setLayoutParams(new GridView.LayoutParams(85, 85));
    i.setScaleType(ImageView.ScaleType.CENTER_CROP);
    i.setPadding(8, 8, 8, 8);
    i.setImageResource(pic[pos]);

         ImageLoader ps = new ImageLoader(c);
     ps.DisplayImage(imageThumbUrls[pos], i);

    return i;

}
}

这是用于从 url 显示图片的图像加载器类。公共类 ImageLoader {

MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new  WeakHashMap<ImageView, String>());
 ExecutorService executorService; 

public ImageLoader(Context context){
    fileCache=new FileCache(context);
    executorService=Executors.newFixedThreadPool(5);
  }

  int stub_id = R.drawable.ic_launcher;
  public void DisplayImage(String url,ImageView imageView)
  {
    //stub_id = loader;
    imageViews.put(imageView, url);
    Bitmap bitmap=memoryCache.get(url);
    if(bitmap!=null)
        imageView.setImageBitmap(bitmap);
    else
    {
        queuePhoto(url, imageView);
      //  imageView.setImageResource(loader);
    }
  }

  private void queuePhoto(String url, ImageView imageView)
  {
    PhotoToLoad p=new PhotoToLoad(url, imageView);
    executorService.submit(new PhotosLoader(p));
   }

  private Bitmap getBitmap(String url)
  {
    File f=fileCache.getFile(url);

    //from SD cache
    Bitmap b = decodeFile(f);
    if(b!=null)
        return b;

    //from web
    try {
        Bitmap bitmap=null;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is=conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Exception ex){
       ex.printStackTrace();
       return null;
    }
  }

//decodes image and scales it to reduce memory consumption
  private Bitmap decodeFile(File f){
    try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE=70;
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

//Task for the queue
 private class PhotoToLoad
 {
    public String url;
    public ImageView imageView;
    public PhotoToLoad(String u, ImageView i){
        url=u;
        imageView=i;
    }
 }

  class PhotosLoader implements Runnable {
    PhotoToLoad photoToLoad;
    PhotosLoader(PhotoToLoad photoToLoad){
        this.photoToLoad=photoToLoad;
    }

    @Override
    public void run() {
        if(imageViewReused(photoToLoad))
            return;
        Bitmap bmp=getBitmap(photoToLoad.url);
        memoryCache.put(photoToLoad.url, bmp);
        if(imageViewReused(photoToLoad))
            return;
        BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
        Activity a=(Activity)photoToLoad.imageView.getContext();
        a.runOnUiThread(bd);
    }
  }

  boolean imageViewReused(PhotoToLoad photoToLoad){
    String tag=imageViews.get(photoToLoad.imageView);
    if(tag==null || !tag.equals(photoToLoad.url))
        return true;
    return false;
   }

  //Used to display bitmap in the UI thread
  class BitmapDisplayer implements Runnable
   {
    Bitmap bitmap;
    PhotoToLoad photoToLoad;
    public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
    public void run()
    {
        if(imageViewReused(photoToLoad))
            return;
        if(bitmap!=null)
            photoToLoad.imageView.setImageBitmap(bitmap);
        else
            photoToLoad.imageView.setImageResource(stub_id);
    }
}

public void clearCache() {
    memoryCache.clear();
    fileCache.clear();
}

}

谁能告诉我错误在哪里..谢谢

4

2 回答 2

0

我不认为你想要达到的目标是可能的。

您可以尝试以下任何一种方法:

  • 使用单个 GridView。将可绘制文件夹中的图像 URI 以及 URL 中的图像加载到数组列表中,并设置适配器,从而设置 GridView。

  • 如果您需要在单个 Activity 中使用两个 gridView,请在每个布局中使用 gridView 创建两个片段。在“fragment A”的一个gridView和“fragment B”中的Url图像中加载可绘制图像。而在 FragmentActivity 的布局中,只需包含两个片段。

于 2013-06-12T06:24:55.667 回答
0

现在我只能在您的布局中看到一个 GridView,如果您希望它并排执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_weight="0.5" >

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        android:columnWidth="80dp"
        android:descendantFocusability="afterDescendants"
        android:focusable="true"
        android:gravity="bottom"
        android:horizontalSpacing="3dp"
        android:numColumns="5"
        android:stretchMode="columnWidth"
        android:verticalSpacing="3dp" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="true"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_weight="0.5" >

    <GridView
        android:id="@+id/gridview2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        android:columnWidth="80dp"
        android:descendantFocusability="afterDescendants"
        android:focusable="true"
        android:gravity="bottom"
        android:horizontalSpacing="3dp"
        android:numColumns="5"
        android:stretchMode="columnWidth"
        android:verticalSpacing="3dp" />
</LinearLayout>

</LinearLayout>

然后只需为每个 GridView 设置两个不同的适配器或同一适配器的两个实例。如果我遗漏了什么,请进一步解释您的问题。

我这样分配适配器:(您需要两个不同的适配器或检查适配器内每个gridview的ID)

gridview2 = (GridView) findViewById(R.id.gridview2);
gridview2.setAdapter(new ImageAdapter(this));

gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
于 2013-06-12T07:46:19.093 回答