1

I have a ViewPager that slides through multiple full screen photos. Each photo has a caption which is an ImageView merged with TextView. I'm trying to hide/ show the TextView caption on the photo based on click action.

Problem: currently I'm using findViewById() to find the TextView within each page of the ViewPager, since there are multiple photos/ captions sharing the same layout, it only returned the 1st TextView ID, thus the hide/ show function only works on the 1st photo caption but not the rest.

Question: How do I make it work for all TextView's?

Thanks!

tkanzakic
  • 5,499
  • 16
  • 34
  • 41
jerrytouille
  • 1,238
  • 1
  • 13
  • 28
  • Post some would make for us to understand your issue nicely. – GrIsHu Feb 26 '13 at 07:27
  • @Grishu To be honest and not trying to mean or anything, this is not a debugging issue. This is more of a 'how-to' question if you read the OP. I can post the code but it's only a few typical lines of `findViewById` within an `onClick()` function. Thanks. – jerrytouille Feb 26 '13 at 07:40
  • do one thing make a seperate layout for your image and textview (for its caption) and include them into your different viewpager layout. that will help you in making single id . check this and let me know if it solves your problem – Abhinav Singh Maurya Feb 26 '13 at 10:48
  • @AbhinavSinghMaurya even when I made separate layout, all TextView layouts would have the same id, which will result in the same issue. ViewPager is one layout, there is no 'different viewpager layout' for each photo, which makes no sense. Bottom line: it does not make sense to have separate layouts for each photo either TextView or ViewPager. The number of photos can be large, not just one photo. – jerrytouille Feb 28 '13 at 00:19

3 Answers3

0

If you add a PageListener to your ViewPager, you could get the current view being selected. This would enable you to fetch the TextView in the current page.

Check this question and the accepted answer to see how to implement the PageListener.

Community
  • 1
  • 1
  • i tried this but the problem was although I effectively managed the position of each view in the ViewPager, the ID of the view itself doesn't change, they all have the same ID. Thus when I selected the view by id and hid it, only the first one worked. – jerrytouille Mar 01 '13 at 01:41
0

I did it! Here's how:

  1. Within each ViewPager there is a fragment, often contains an ImageView which is its main View component.
  2. Since I've already been tracking the ImageView OnClickListener in each fragment and passed back to the Activity, I have an OnClick(View v) method in the Activity.
  3. In the OnClick(View v), I get the parent View of the current ImageView using (View) getParent() to get the ViewPager parent container (of the current page).
  4. Once you get the parent View, you can find its child TextView id using (TextView) parent.findViewById()

Bonus: the code:

    public void onClick(View v) {
            // since each pager includes an ImageView and a TextView
            // get the current pager which is the parent of the current ImageView
            View currentPage = (View) v.getParent();
            // find the TextView within the current page
            mCaption = (TextView) currentPage.findViewById(R.id.caption);
    ...

Done!

jerrytouille
  • 1,238
  • 1
  • 13
  • 28
0

You could set tags to your views in this way:

textView.setContentDescription("MyTag");

And then in order to find all views with that tag you need to call:

ArrayList<View> outputViews = new ArrayList<>();
mViewPager.findViewsWithText(outputViews, "MyTag", FIND_VIEWS_WITH_CONTENT_DESCRIPTION); 
CookieMonster
  • 1,723
  • 1
  • 15
  • 15