CodePrimate
CodePrimate

Reputation: 6666

Three fragments with views that have the same id causing odd behavior

I've created a viewpager which shows 3 similar fragments. These fragments have identical layout and the views in those layouts have the same id(eg: The RatingBar in DetailedPowerFragment has the id detailedRatingBar and so does the RatingBar in the DetailedWaterFragment)

However, when I attempt to set the value of the RatingBar with findViewById individually in each of the fragments it seems that a different RatingBar is found.

So

        final RatingBar ratingBar = (RatingBar)getSherlockActivity().findViewById(R.id.ratingBar);
    ratingBar.setEnabled(false);

in DetailedWaterFragment, would set the RatingBar in DetailedPowerFragment. Below you will see the code creating the viewpager if that is relevant - But what is causing this issue? Do I need to give a new id to all of my views to ensure that this doesnt happen?

    detailedFragments = new ArrayList<SherlockFragment>();

    detailedFragments.add(new DetailedPowerFragment());
    detailedFragments.add(new DetailedHeatFragment());
    detailedFragments.add(new DetailedWaterFragment());

    mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), detailedFragments);
    mViewPager.setAdapter(mMyFragmentPagerAdapter);

Upvotes: 0

Views: 206

Answers (1)

Evos
Evos

Reputation: 3915

When you use getSherlockActivity().findViewById() it returns you first view wit provided id from whole view hierarchy, so it can be not those rating bar that you currently see on the screen.

Yeap you could create 3 different fragments with a bit different view ids, this will helps. Also you could modify your logic and invoke method to change rating bar of currently showed fragment.

Good luck!

Upvotes: 1

Related Questions