2013-06-12 74 views
0

我們可以用兩種GridView控件在一個單獨的XML file.it顯示onle第一GridView的我..如何使用兩個GridView控件together.i想從第二gridview的網址在第一GridView控件&顯示從繪製文件夾中的照片。 PLS引導我..嵌套的GridView

這是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的顯示PIC 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(); 
} 

} 

誰能告訴我哪裏是mistake..thanks

回答

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)); 
+0

好的..讓我試試這個.. – Coder

+0

不工作..只顯示那些引用第一個gridview的圖片..因爲第一個gridview覆蓋整個屏幕..所以正確,所以如何在同一活動中顯示第二個gridview。 。 任何想法?? – Coder

+0

gridview將擴展以滿足任何需要(因爲它的父級具有fill_parent屬性,將每個gridview嵌入到不同的線性佈局中併爲每個佈局設置0.5個權重。 –

0

我不認爲你正在努力實現是可能的。

你可以嘗試以下任何方式:

  • 使用單個GridView控件。從可繪製文件夾加載圖像URI,並將URL中的圖像加載到數組列表中,然後設置適配器和GridView。

  • 如果您在單個活動中需要兩個gridView,請在每個佈局中創建兩個帶有gridView的片段。將可繪製圖像加載到「片段A」的一個gridView和「片段B」的Url圖像中。而在FragmentActivity的佈局中,只需包含這兩個片段即可。

+0

@Santosh:我想UR拳頭的建議,但它仍然不是working..it只顯示來自一個位置的圖片,不論是來自drawable還是網址。 – Coder

+0

我編輯了我的代碼..請告訴我這裏有什麼問題? – Coder