9

I want to create a HorizontalScrollView, but with some "effects" in it, like this example, or this other example. The thing is that I don't know how many items I'll have to use; I get the ImageView from an API so it should be dynamic. How can I make that effect that if it's selected it becomes bigger with the TextView below? The closest example I found on SO is here. That's exactly what I need, but I've tested the answer given and it doesn't work for me ... but the image from the question is exactly what I want. Can anyone guide me to do this?

Let's say I'll test it with 5 ImageView ONLY FOR TESTING, so an example to how to add those ImageView dynamically would be good for me as well.

Another example would be Snapchat APP but the difference is that I would like to add some effect on the selected like make it bigger or something.

EDIT

I would like to get an example to how to do like a custom HorizontalScrollView with a Layout (adapter) and the most important if it's possible add an efect to the clicked one, and I want the adapter because I'll need to get the item clicked of course. The thing that I think I should use a RecycleView as @UncaughtException said to me because I don't know how much images I'll get and I'll have to put on my APP so I think this is the solution.

It would be a mix between Snapchat HoritzontalScrollView and This image from a SO question

Community
  • 1
  • 1
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148

2 Answers2

7

In your case definitely you can use ViewPager, but if i were you i would go for RecyclerView with a LinearLayoutManager with its orientation set to Horizontal, so you will not need an HorizontalScrollView, and using RecyclerView you will also get the adapter thing you are looking for..

Now in order to scale or show some other effect on click to differentiate it from others, you can Animate that particular view,

I have written some demo code for that , posting here the required files , let me know if this is what you want,

Activity

/**
  * Created by Satyen on 10/27/15.
  **/
public class SlidingDrawerActivity extends Activity {

   RecyclerView rcyList;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_scroll_list);

        rcyList = (RecyclerView) findViewById(R.id.rcyList);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rcyList.setLayoutManager(layoutManager);

       /* rcyList.addItemDecoration(
                new DividerItemDecoration(this, null));*/
        MyRecyclerViewAdapter myRecyclerAdapter = new MyRecyclerViewAdapter(this);
        rcyList.setAdapter(myRecyclerAdapter);

    }


}

Activity Layout

<!-- layout_scroll_list.xml -->

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

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_weight="1">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rcyList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@android:color/holo_blue_dark"
        android:paddingLeft="8dp"
        android:paddingRight="8dp" />

</FrameLayout>
</LinearLayout>

Adapter

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.CustomViewHolder> {

private Context mContext;
View animatedView = null;

public MyRecyclerViewAdapter(Context context) {
    this.mContext = context;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
    final View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);


    CustomViewHolder viewHolder = new CustomViewHolder(view);
    /*final Animation a = AnimationUtils.loadAnimation(mContext, R.anim.scale_up);*/
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // You can tweak with the effects here
            if (animatedView == null) {
                animatedView = view;
            } else {
                animatedView.setAnimation(null);
                animatedView = view;
            }
            ScaleAnimation fade_in = new ScaleAnimation(1f, 1.3f, 1f, 1.3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            fade_in.setDuration(100);     // animation duration in milliseconds
            fade_in.setFillAfter(true);    // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
            view.startAnimation(fade_in);
        }
    });
    return viewHolder;
}

@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
    //Setting text view title
    customViewHolder.textView.setText("Data No. " + i);
}

@Override
public int getItemCount() {
    return 10;
}

public class CustomViewHolder extends RecyclerView.ViewHolder {
    protected ImageView imageView;
    protected TextView textView;

    public CustomViewHolder(View view) {
        super(view);
        this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
        this.textView = (TextView) view.findViewById(R.id.title);
    }
}
}

Adapter Row Layout

<!-- list_row.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">

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

    <ImageView
        android:id="@+id/thumbnail"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thumbnail"
        android:layout_centerHorizontal="true"
        android:text="dafdafda"
        android:textColor="#222"
        android:textSize="12sp" />

</RelativeLayout>

</LinearLayout>

Other than you can also use TwoWayView that gives the functionality of implementing HorizontalListView,

Above is just some demo code which may require some tweaks, let me know if this helps or ask further ...

Also adding the screenshots of the output ..

Layout when none of the item is clicked

Layout when second item is clicked

Satyen Udeshi
  • 3,223
  • 1
  • 19
  • 26
  • How should I use those icons, I mean this is a call from an API so I'll need to fill it as it needs, you got me? – Skizo-ozᴉʞS ツ Oct 27 '15 at 13:02
  • Another thing, do you know why if I select one Data no. X and I scroll horizontally and I get back to it it's without zoom? – Skizo-ozᴉʞS ツ Oct 27 '15 at 13:03
  • you mean showing them from url right ? If i am not wrong then you can use `ImageLoading` libraries like `UniversalImageLoader` and `Picasso` and in the `onBindViewHolder()` method you can update the images accordinly – Satyen Udeshi Oct 27 '15 at 13:04
  • Nono, I mean how to add it on the RecyclerView I used to do this on a ListView witch I had to do something like mListview.add(Image,Text); – Skizo-ozᴉʞS ツ Oct 27 '15 at 13:05
  • Yes it is because of the view recycling, each time we scroll and come back the `onCreateViewHolder()` will be called so it loses the zoom let me see what i can do about that – Satyen Udeshi Oct 27 '15 at 13:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93496/discussion-between-satyen-udeshi-and-skizo). – Satyen Udeshi Oct 27 '15 at 13:06
  • Hello mate, I'm facing with a similar problem let me know if you can help me out, I guess it's almost the same problem on here, but data is taken from `Firebase`, but I guess it doesn't matter, if you have any moment to help me I'll be glad this is the [question](https://stackoverflow.com/questions/48010917/custom-adapter-for-firebaseadapter), The thing i'm trying is to create something like this : [Image](https://raw.githubusercontent.com/GoodieBag/CarouselPicker/master/gif/gif_image_480.gif), but it's giving me problems with FirebaseAdapter... if it'd be a Recyclerview it'd work I guess... – Skizo-ozᴉʞS ツ Dec 29 '17 at 11:38
4

Step 1:

Display the images horizontally in a ViewPager.

Step 2:

Apply the ScaleAnimation class to the clicked item to zoom in on it. This can be done in the instantiateItem() method of the ViewPager's PagerAdapter.

Also, there are some ready-to-use open source widgets available like CoverFlow and FancyCoverFlow. You might want to check out the source code to see how they work.

EDIT:

Firstly, regarding your question of how to handle an unknown number of images, you should realize that in all such widgets (ListView, GridView, ViewPager etc.), the number of objects coming from the API is always unknown at first, i.e. it becomes known when then API response is received. So if you first implement a ViewPager in the normal way, you will see how this is handled. Basically you have to use an Adapter and a model object to populate the ViewPager or ListView. The API response will be either JSON or XML, and after parsing it, you will know the exact number of items.

So I think you should start with first implementing a ViewPager in the normal way. There are any number of examples available for this. Two interesting ones are this one and this one. They are interesting for your case because they also contain example code of how to enlarge the image.

Now coming to the second problem: how do we enlarge the image. To do this, one way is to use the ScaleAnimation class. For example, say you want to enlarge an image by 100% around its center:

ScaleAnimation scaleAnimation =  new ScaleAnimation(1.0f, 1.5f,
                                                    1.0f, 1.5f,
                                                        Animation.RELATIVE_TO_SELF, 0.5f,
                                                        Animation.RELATIVE_TO_SELF, 0.5f);

scaleAnimation.setDuration(200);
scaleAnimation.setInterpolator(new AccelerateDecelerateInterpolator());

imageView.startAnimation(scaleAnimation);

I would use this code on the image in the instantiateItem() method of the ViewPager's PagerAdapter. This should work. Or you can try the zoom animation approach in one of the two previous examples.

I'm afraid you'll have to try to create a working project using these examples as a guide. Then we can further discuss any other problems you face. I'm sure that this can be done quite easily, and I know you can do it. Best ...

EDIT 2:

As per the two examples that you have given, have you seen this and this? Is that what you're looking for ? And does it bring you any closer to solving the problem ?

Community
  • 1
  • 1
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • I saw the [CoverFlow](https://github.com/moondroid/CoverFlow) allready, but do you know if I could do this using something like this [image](http://i.stack.imgur.com/snB84.png)? – Skizo-ozᴉʞS ツ Oct 19 '15 at 18:07
  • I have given the approach above ... if you apply it correctly then I'm sure it will work ... :) – Yash Sampat Oct 19 '15 at 18:08
  • And do you know if I could use [FancyCoverFlow](https://github.com/davidschreiber/FancyCoverFlow) with those images not rotated? I mean those images are like turned I want it without rotation – Skizo-ozᴉʞS ツ Oct 19 '15 at 18:10
  • You can study the source code of those libraries and modify it to suit your own needs ... :) – Yash Sampat Oct 19 '15 at 18:11
  • So what shall I use to display the imageviews RecycleView, GridView, ListView, ViewPager...? – Skizo-ozᴉʞS ツ Oct 26 '15 at 14:58
  • Isn't `ViewPager` what you need to use here ?? – Yash Sampat Oct 27 '15 at 04:42
  • Thats what I don't know whats the best solution there's people told to me that the best solution is use recycleview and then put in on a Horitzontalsxrollview and for example you saying that use a viewpager I really don't care about what to use I only want it to look like those 2 examples on my edit – Skizo-ozᴉʞS ツ Oct 27 '15 at 08:03
  • My guess is you can use either `HorizontalScrollView` or `ViewPager`, and then add the zoom effect. Please also see the edit, might be helpful ... :) – Yash Sampat Oct 27 '15 at 08:18