2017-03-31 55 views
0

在我的android应用程序中,我从http请求中提取数据。如何动态加载滑动图片(Android)?

在我的布局中,我想用Glide库动态显示图像。

我得到的图像网址,但我无法找到一种方式来显示他们在我的布局。

在我的布局文件,我有:

<LinearLayout 
    android:id="@+id/social_media_layout" 
    android:layout_width="match_parent" 
    android:orientation="horizontal" 
    android:layout_height="60dp" 
    android:gravity="center_vertical"> 

    </LinearLayout> 

不知怎的,我想在我的LinearLayout添加imageviews(动态)。

例如:如果我从请求中获得2个图像url,我需要在我的linearlayout中使用两个imageviews。

注意:我想用Glide库加载图像。

任何想法都是有益的。

谢谢。

+0

获取LinearLayout视图实例,然后创建您的ImageView并添加所需的图像,然后使用linearLayout.addView(image);添加视图 –

+1

很可能,您应该使用ListView或RecyclerView而不是LinearLayout。 –

回答

1

你可以做一个新的XML文件item_social_media.xml

例如

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="60dp" > 

    <ImageView 
     android:id="@+id/social_media_icon" 
     /> 

     <!-- More customization as needed --> 

</RelativeLayout> 

你可以说膨胀,并添加到LinearLayout中。

final Context context = MainActivity.this; // TODO: Get a context 

// Inflate and update the view 
View socialMediaItem = LayoutInflater.from(context).inflate(R.layout.item_social_media); 
ImageView icon = (ImageView) socialMediaItem.findViewById(R.id.social_media_icon); 
// TODO: Glide ... into(icon); 

// Add to the LinearLayout 
linearLayout.addView(socialMediaItem); 
1

您应该更好地使用ListView或RecycleView。但是,如果你坚持将它们添加到LinearLayout中动态,则:

LinearLayout layout = (LinearLayout)findViewById(R.id.social_media_layout); 

// assuming that image_urls_list contains the images urls 
for(int i=0;i<image_urls_list.size();i++) 
{ 
    ImageView image = new ImageView(this); 

    // set whatever width and height you want to give 
    image.setLayoutParams(new android.view.ViewGroup.LayoutParams(100,100)); 

    // load image using Glide 
    Glide.with(context).load(image_urls_list.get(i)).into(image); 

    // Adds the view to the layout 
    layout.addView(image); 
} 

你可能要一个Horizo​​ntalScrollView标签作为父标记添加到您的LinearLayout,让您可以水平滚动。