0

I have a layout like so.

HorizontalScrollView
    LinearLayout (Horizontal)
        ImageView (A number of times)

Is there any event/way I can tell which ImageView is visible? I'd like an event if possible so I can store the index of the current Image that's visible.

Something like

class MyViewClass
    private int index;
    ....


    imageView.onEventViewListener(...
        index = currentImageIndex;
    );

EDIT: My current code loading the ImageViews

    OnClickListener click = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Object i = v.getTag();
            smoothScrollTo((width*((Integer) i+1)), 0);
        }
    };

    for(int i = 0;i < mImages.size();i++) {
        Gallery.Image image = mImages.get(i);

        ImageView iv = new ImageView(getContext());
        ViewGroup.LayoutParams par = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        iv.setLayoutParams(par);
        iv.setTag(i);
        iv.setOnClickListener(click);


        Picasso.with(getContext())
                .load("http://www.example.com/files/galleries/" + image.getGalleryId() + "/" + image.getSrc() + "_full.jpg")
                .into(iv);
        layout.addView(iv);
    }
TMH
  • 6,096
  • 7
  • 51
  • 88

2 Answers2

0

You can use viewpager; it will provide all the events that's required by you

gandharva
  • 691
  • 5
  • 16
  • your questions doesn't show any viewpager. I doubt you can nest them and I am not sure why you would want to – jiduvah Apr 02 '14 at 12:06
  • It's not suggested, but you can have a look here- http://stackoverflow.com/a/12856349/637755 – gandharva Apr 02 '14 at 12:06
  • I should have clarified a bit, this is for a Gallery Fragment I'm building, where you'll be able to swipe left right between images, and have (not sure what yet) some UI to skip backwards and forward, and this Gallery Fragment is inside a ViewPager contaning other Fragments like a ContactDetails one, and a OpeningTimes one etc. – TMH Apr 02 '14 at 12:10
  • 1
    Well with a viewpager you swipe from side to side. So how can another element work within that, when it also wants to swipe from side to side? – jiduvah Apr 02 '14 at 12:13
  • This will be a subview of Gallery, by default it's a vertical list, so no side to side scrolling, when you click an Image you'll get this detailed view popping up, but maybe I need to think over the flow of this Activity, see if there is a neater way of doing it. – TMH Apr 02 '14 at 12:16
  • Ah ok, so the viewpager has nothing to do with this view? Then you could probably register an onScrollListener. Then compare the value when it scrolls to the position of your views. – jiduvah Apr 02 '14 at 12:21
0

You can try this widget:HorizontalListView. Its attribute mLeftViewAdapterIndex means first Visiable item's index. Here is the source code in github. https://github.com/MeetMe/Android-HorizontalListView/blob/master/AndroidHorizontalListView/src/com/meetme/android/horizontallistview/HorizontalListView.java

banking
  • 480
  • 2
  • 9