How to insert images in horizonatal scrollview in custom adapter? - android

I am using custom adapter for listview where I put 4 sections.
Now i have photo section in which I put HorizontalScrollView. I got bunch of images from server so put them in string array.
if (postDisplay.getImageUrl().equals("null")) {
holder.photo_layout.setVisibility(View.GONE);
} else {
holder.photo_layout.setVisibility(View.VISIBLE);
String SharePhotos=postDisplay.getImageUrl();
String temp=SharePhotos.replace("small", "big");
String[] SharePicArray=temp.split("~");
for (int i=0; i<SharePicArray.length; i++) {
ImageView imageview = new ImageView(activity);
imageview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
imageLoader.DisplayImage(SharePicArray[i], imageview);
String URL=SharePicArray[i];
holder.image_layout.addView(imageview);
}
}
When I run this code first time it gives me right solution but when I scroll down and then scroll up to see again the images it again create another imageview.
If there is any other solution please let me know.
If there is any other view I can take to show images from server.

Related

How can I use ViewFlipper like a slider

I have a layout for recyclerview adapter that it's like a instagram. That means it has a slider for images and like button and etc.
Now I want to load multiple images in the slider and clients should change images with dragging like instagram exactly.
First I used PosterSlider library, but its problem is that doesn't have scale type so I cannot manage images size.
So now I wanna try ViewFLipper, but my problem is I don't know how can I use ViewFlipper like a slider.
That means how can I change images by dragging in ViewFLipper?
If you have another idea for changing picture, I will be glad to hear it.
This is my code for ViewFlipper that I know it is wrong, because it shows just one Image and it doesn't have any code for changing picture by dragging:
try {
JSONArray jsonArray = new JSONArray(newsLetterList.get(position).Jsonsrc);
for(int i = 0;i < jsonArray.length(); i++){
setImageInFlipper(jsonArray.getString(i),holder);
}
} catch (JSONException e) {
e.printStackTrace();
}
setImageInFLipper method:
private void setImageInFlipper(String imgUrl,NewsLetterViewHolder holder){
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(context).load(imgUrl).into(imageView);
holder.slider.addView(imageView);
}
You can take some reference from here. They have implemented the same viewflipper slide both with next(flip) button and without using any button. Hope that helps!

Picasso: adding multiple images dynamically in flipper for Android.

I'm new to picasso.using it i want to dynamically fetch images and be able to update the images whenever some new link has been updated. currently i'm only able to do this for a single Image. the code that i'm using is :
picasso.with(this).load(url).into(image1)
where url is the url to the image and image1 is an imageview. i want to diaplay 5 images into 5 different imageviews, iteratively. how can i do that ?
also i wan to delete the cached images of picasso, so that i can update it with newer images. any help would be appreciated.
In your xml just add only this,
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ViewFlipper>
lets Say your URL Images Array like this.
String ImgAry[] = {"url1","url2","url3","url4","url5"}
In your onCreate()
viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
for(int i=0;i<ImgAry.length;i++)
{
// create dynamic image view and add them to ViewFlipper
setImageInFlipr(ImgAry[i]);
}
method in Your Activity file
private void setImageInFlipr(String imgUrl) {
ImageView image = new ImageView(getApplicationContext());
picasso.with(this).load(imgUrl).into(image);
viewFlipper.addView(image);
}
private void setImageInFlipr(String imgUrl) {
ImageView image = new ImageView(getApplicationContext());
picasso.get().load(imgUrl).into(image);
viewFlipper.addView(image);
}

Get position of dynamic Image View

Outline
My application has a main Image View and below main view, it has dynamic horizontal imageview .
Code for dynamic image view
ImageView imageall = new ImageView(FullImage.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(175, 175);
params.setMargins(10, 0, 0, 0);
imageall.setLayoutParams(params);
imageall.setBackgroundResource(R.drawable.imageview_border);
Picasso.with(context_tab1).load("http://example.com//upload/image/" + stringList.get(in)).into(imageall);
// Adds the view to the layout
layout.addView(imageall);
imageall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = (Integer) v.getTag();
image = "http://example.com/x/upload/image/" + stringList.get(position);
Picasso.with(context_tab1).load(image).into(fullimage);
}
});
}
I populate the dynamic imageview from array list. Everything works fine till this part.
Problem
Now, On Click of the dynmic imageview must get that image view position and change as the main imageview .
This is not working for me.
For better understanding please find the image attached.
Thanks.
No need of getting position of dynamic ImageView..
While creating the dynamic ImageView you can set Ids to them and also set the onClickListener on them. Override the Onclick method, check the Id with your Ids you gave to dynamic ImageView and replace the Main view's as per the ImageView clicked..
Happy coding.. :)

android: one click listener on many imageviews in layout

I have the following scenario:
In a layout, I have imageviews aligned horizontally and vertically as shown in image.
First, which layout should be used for this purpose? RelativeLayout or FrameLayout? ListView inside the layout?
Also, instead of writing setOnClickListener for every imageview, how can I write just one click listener to get the clicked imageview?
A GridView is perfect for this. For more information on GridView, look here.
As for your onClickListener question: there is no easy way to do this, but what you can do is something like this:
Have an array containing every ImageView id like so:
public static final int[] IMAGE_VIEWS = {R.id.imageView1, R.id.imageView2, R.id.imageView3 /*etc*/}; //Make sure to list from the first imageview to the last image view in correct order
Define an onClickListener
private View.OnClickListener imageViewListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < IMAGE_VIEWS.length; i++) {
if (v.getId() == IMAGE_VIEWS[i]) {
doSomethingWithImageViewClick(i); //if ImageView 4 was clicked, variable 'i' will be 3 (because arrays start at index = 0).
}
}
}
}
Set the onClickListeners for all your imageViews:
final ImageView view1 = (ImageView) view.findViewById(R.id.imageView1);
view1.setOnClickListener(imageViewListener);
//etc
Use 'GridView' in the layout and use 'setOnItemClickListener' to handle click events,
You can find more details in below link
GridView.
GridView can achieve the effect. I think you can look at this Grid View.

Accessing array of imageviews with for loop

I am developing an app which has 9 image views in a 3x3 matrix.
I want to change their image if a user clicks them.
So tat requires linking 9 ids and 9 set on click listeners
Is it possible to access them using a for loop like this
public int[] imv= {R.id.im0, R.id.im1, R.id.im2, R.id.im3,R.id.im4, R.id.im5, R.id.im6, R.id.im7,R.id.im8};//Loading ids into array imv
for(int i=0;i<imv.length;i++)
{
ImageView im[i] = (ImageView) findViewById(imv[i]);//Attaching ids
}
for(int i=0;i<imv.length;i++)
{
im[i].setOnClickListener
}
I am getting an error with im[i]. But if I remove i I get an object im which is a collection of imageviews. How can I access the individual imageviews and set onclicklisteners?
I think it will be better to go for GridView refer to this example
http://developer.android.com/resources/tutorials/views/hello-gridview.html
you can write your logic in getView method using imageView.setOnClickListener
//scope of the im[i] will end in the first for loop
for(int i=0;i<imv.length;i++)
{
ImageView im[i] = (ImageView) findViewById(imv[i]);//Attaching ids
im[i].setOnClickListener(this);
}
I think the problem is that "im" is declared inside the for loop (making it local to the for loop only).. Declare "im" outside the loop, then just define it inside.
try:
ImageView im[] = new ImageView im[imv.length];
for(int i=0;i<imv.length;i++)
{
im[i] = (ImageView) findViewById(imv[i]);//Attaching ids
}
Note: I've forgotten how to properly declare an array. Just confirm it elsewhere.. :D

Categories

Resources