2015-06-20 42 views
0

我需要在scrollview中顯示像gridview這樣的圖像。 正如我讀到它是不正確的在scrollview中使用gridview。但我需要大圖像然後文本,然後列出ao所有圖像和所有在一個片段,所以我需要scrollview,像第一個,但只是圖像沒有任何文字 enter image description here 所以有一種方法來以編程方式顯示圖像後另一個在RelativeLayout(比如在gridview中)?如何使用像gridview的圖像做RelativeLayout?

這是我得到的,但它不工作,圖像只是相互覆蓋。

for (String pic : mFlat.getAdv_pics()){ 
      i++; 
      ImageView imageView = new ImageView(mActivity); 
      imageView.setId(i); 

      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
      params.addRule(RelativeLayout.RIGHT_OF, i-1); 

      imageView.setLayoutParams(params); 

      Log.d("!!!!", "" + i + "|" + imageView.getId()); 

      Log.d("!!!!", "http://****" + pic + "_small.jpg"); 
      relativeLayout.addView(imageView); 
      Picasso.with(mActivity) 
        .load("http://****" + pic + "_small.jpg") 
        .resize(width,width) 
        .centerCrop() 
        .into(imageView); 
     } 
    } 

感謝

+0

但爲什麼你不想使用GridView? GridView默認提供滾動機制。 – dhuma1981

+0

使用網格視圖它被定義爲專門處理這些複雜性,你代表 –

+0

因爲我知道在scrollview中使用gridview - 是一個大問題 – SERG

回答

1

是有原因的gridview的存在,創建這樣的佈局是更爲複雜和困難的方式處理也使用其它視圖組合(事件,位置..)。您基本上需要在realtivelayout內顯示一個listview,而listview的項目將是您的自定義網格的一行。 比如你想在每行顯示3張圖片:

你的主要XML看起來就像這樣:

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

    <ListView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/listView"/> 
</RelativeLayout> 

,然後你的ListView適配器的項目將是這樣的:

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

    <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imageView1"/> 
    <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imageView2"/> 

    <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imageView3"/> 

</RelativeLayout> 

現在,您需要將嵌套數組傳遞給適配器,每個數組項都包含3個圖像等等。但主要問題是處理事件以及需要重新考慮的地方。

相關問題