我在我的应用中使用AndroidImageSlider库作为图片滑块。我正在使用Hashmap
加载图片,如下所示:
HashMap<String,String> url_maps = new HashMap<String, String>();
url_maps.put("Image 1", "http://example.com/./project_image/example.jpg");
url_maps.put("Image 2", "http://example.com/./project_image/example.jpg");
url_maps.put("Image 3", "http://example.com/./project_image/example.jpg");
for(String name : url_maps.keySet()){
TextSliderView textSliderView = new TextSliderView(this);
// initialize a SliderLayout
textSliderView
.description(name)
.image(url_maps.get(name))
.setScaleType(BaseSliderView.ScaleType.Fit);
//add your extra information
textSliderView.bundle(new Bundle());
textSliderView.getBundle()
.putString("extra",name);
mDemoSlider.addSlider(textSliderView);
}
注意:我正在设置HashMap
的键作为每个图像的描述。
如何使此滑块动态化?
我想从json feed动态加载图像,如下所示:
{
"Images": [
{
"title": "title",
"Image": "http://example.com/./project_image/example.jpg"
},
{
"title": "Distribution",
"Image": "http://example.com/./project_image/example.jpg"
},
{
"title": "Distribution",
"Image": "http://example.com/./project_image/example.jpg"
}
]
}
我可以使用HashMap
执行此操作,还是需要使用其他选项?如果是,请建议。或者我使用不同的库?如果是,请参考。
谢谢。
答案 0 :(得分:1)
使用Gson
Gson gson = new Gson();
Type type= new TypeToken<List<ImageData>>(){}.getType();
List<ImageData> imageDataList = gson.fromJson(jsonString, type);
其中jsonString
是表示JSON数组的字符串
ImageData
类看起来像这样
public class ImageData {
String title;
String Image;
public ImageData(){};
}
然后按如下方式遍历列表
for(ImageData data : imageDataList){
TextSliderView textSliderView = new TextSliderView(this);
// initialize a SliderLayout
textSliderView
.description(data.title)
.image(data.Image)
.setScaleType(BaseSliderView.ScaleType.Fit);
//add your extra information
textSliderView.bundle(new Bundle());
textSliderView.getBundle()
.putString("extra",data.title);
mDemoSlider.addSlider(textSliderView);
}
答案 1 :(得分:0)
创建一个类作为数据的结构
class ImageUrls{
String imageTitle,imageUrl;
ImageUrls(String imageTitle, String imageUrl){
this.imageTitle = imageTitle;
this.imageUrl = imageUrl;
}
}
现在制作ArrayList<ImageUrls> url_maps
在ArrayList中添加数据
url_maps.add("Image 1", "http://example.com/./project_image/example.jpg");
url_maps.add("Image 2", "http://example.com/./project_image/example.jpg");
url_maps.add("Image 3", "http://example.com/./project_image/example.jpg");
并将此数据添加到ImageSlider
For(int count : ArrayList.size()){
//here you can get your data from arraylist using
arraylist.get(postion).imageTitle
arraylist.get(postion).imageUrl
}