2015-06-20 48 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个图像等等。但主要问题是处理事件以及需要重新考虑的地方。

相关问题