简体   繁体   中英

Views with same id in multiple fragments

I have a custom view "ClearableTextView", there's an EditText and a button inside a relative layout. The view is created by inflating a layout file, so the two views have IDs.

I also have two fragments and each inflates it's own layout. Both this layouts include my custom ClearableTextView, inside other views.

I know that reusing the same ID is bad and the result is unpredictable, however a guy from google who's name sounds french once said that as long as the child views have unique IDs within a parent there shouldn't be a problem finding the right one. My searched Views don't even have a common root View, the views they are in are owned by different Fragments.

I look for the reference of EditText in fragment A like so:

v= inflater.inflate(layoutA);
TextView tvA=v.findViewById("someLayoutUniqueID").findById(ClearableTextView_uniqueID_A).findViewById(editTextID);

And for the EditText in fragment B:

v= inflater.inflate(layoutB);
TextView tvA=v.findViewById("anotherLayoutUniqueId").findById(ClearableTextView_uniqueID_B).findViewById(editTextID);

And when I say an ID is unique, it means it is unique within all layouts in my project.

Now the weird stuff: I create Fragment A. I don't know if it matters, but the Fragment is attached to a viewPager using a FragmentStatePagerAdapter. I type something in the EditText. Then I press a button that creates Fragment B, removes A from the viewPager, and attaches Fragment B to the viewPager. Now in Fragment B the EditText contains the text from fragment A!!! Why?

I know that viewpager combines the Views of each fragment, but in my case it shouldn't, since there is only one fragment attached at all times. And I'm searching for EditText views within their parents that do have unique ids. Any ideas?

If I remember correctly, the ViewPager usually keeps any adjacent fragments in memory so that it can improve the swiping performance. It also keeps the state of the fragment so it can be reloaded quickly, which include the value of the views. When you add your fragments to the view pager it will cause your fragments to be loaded and attached to the activity's view tree.

I ran into a similar problem in the past and I kept putting my text into the wrong TextView . I never tried nesting findById calls like you did, but when I simply searched for a TextView by id I'd usually get the one that was first declared. You can solve this problem in 2 ways, the first is using different ids, which will require some cut-and-paste action (manageable to some extent). The second and optimal choice would be to create a custom view that would encapsulate this layout and it's logic. Pre-define the clicking of the button to clear the text and create an interface to expose que required/necessary bits for it to function properly with the rest of your code.

I know this is late but I've been having the same problem and I think I've found an easy solution. Instead of just looking for the EditText by ID you can get the parent of that EditText and then look for the child which is the EditText like this:

LinearLayout layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.layout_with_editText);
EditText et = (EditText) layout.getChildAt(index); //whatever index your EditText is at

This will actually allow you to assign a text or do whatever work on the EditText in question instead of all of them in memory with the same id. I was also having the same problem with Spinners and this fix also applies. Hope this will help anyone with this problem

I have ViewPager and some MyFragment.newInstance(uniqueId) with parent RelativeLayout. When after creation I try dynamically add any view to any other that first page, all new views was added to first page. Finally me help to set uniqueId for every RelativeLayout in onViewCreated.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    vBackground.setId(mPresenter.getVehicle().getId());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM