Saturday, February 1, 2020

Aleksandr Koscheev


Right now I am in Cancún, Mexico.

Phone (+521) 998 169 2213

Russian phone (SMS only) (+7) 910 480 7885

Skype: ru.alxr


LinkedIn profile:

Looking for long-time job in Android development

@Deprecated Skype:  tech_support_jetpc

Thursday, February 11, 2016

RecyclerView: Some trick with Fragment which was instantiated inside XML

Fragment inside xml.
Use of view is to use with RecyclerView viewholder.
Sometimes strange RuntimeException occurs: android.content.res.Resources$NotFoundException: Unable to find resource ID ...
XML looks like this:


 <someviewgroup android:layout_height="wrap_content" android:layout_width="wrap_content">

  <fragment android:id="+@id/someId" android:layout_height="100dp" android:layout_width="100dp" android:name="bla.bla.YourFragment">



If you inflate this XML you can face with android.content.res.Resources$NotFoundException.

And stacktrace guides to nowhere:

E/AndroidRuntime(1013): FATAL EXCEPTION: main
E/AndroidRuntime(1013): android.content.res.Resources$NotFoundException: Unable to find resource ID #0xffffffff
E/AndroidRuntime(1013):     at android.content.res.Resources.getResourceName(
E/AndroidRuntime(1013):     at ......  and etc.

The only way (I could find) to fix crash is to remove id.

But how to get access to instance of Fragment bla.bla.YourFragment from ViewHolder?
You cannot find fragment by id or tag.

Solution is:

In your fragment add callback as tag to rootview:

 public void onViewCreated(View view, Bundle savedInstanceState) {
  view .setTag(new SomeHandler() {
    public void someMethod() {
     //Some code

In ViewHolder find that View and get tag (there is no need to explain how to do it).
Voila, you can pass almost any data into your Fragment!