There is a side menu Expandable Navigation Drawer. To click something on an element, I used switch/case.

 case 1: switch (childPosition) { case 0: break; case 1: break; case 2: break; case 3: break; default: break; } break; default: break; 

Where case 1 is the menu item is open, and case 0; case 1; case 2; case 3 case 0; case 1; case 2; case 3 case 0; case 1; case 2; case 3 - the subitem of the menu is open.

There is a fragment with markup in which the textview is located.

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/selected_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/frag1_text"> </TextView> </LinearLayout> 

In this textview , the text should be removed from the string, depending on the clicked sub-element ( case ). Since I don’t have one fragment, I call it first in the case.

  case 1: switch (childPosition) { case 0: fTrans.replace(R.id.frgmCont, frag1); break; default: break; } break; default: break; 

I prescribe in the fragment class:

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment1, null); mSelectedItemView = (TextView) v.findViewById(R.id.selected_item); mSelectedItemView.setText(Html.fromHtml(getResources().getString(R.string.selected_item), null, new MyHtmlTagHandler())); return v; } } 

Now, when calling a fragment in case 0 — its markup with a TextView is called, which takes text from string.selected_item , calling the same fragment in case 1 (the second sub-element) string.selected_item same thing happens. The screen will display the same text from string.selected_item . But I need the text from string.txt_1 taken from the same TextView for example. Do not create the same for each case fragment. But also to write a line in the case:

 mSelectedItemView = (TextView) v.findViewById(R.id.selected_item); mSelectedItemView.setText(Html.fromHtml(getResources().getString(R.string.selected_item), null, new MyHtmlTagHandler())); 

changing the address of the resource in each case: R.string.selected_item to R.string.txt_0 for case 1 and R.string.txt_1 for case 2 , etc. will not work. Then how to do to solve my problem. Thanks for the help!

    1 answer 1

    You can send data about which line should be displayed in the fragment itself.

      frag1 = new YourFragment();// создаете фрагмент Bundle args = new Bundle(); int stringId = 0; switch (childPosition) { case 0: stringId = R.string.txt_1 break; case 1: stringId = R.string.txt_2; break; case 2: stringId = R.string.txt_3; break; case 3: stringId = R.string.txt_4; break; default: break; } args.putInt("stringId", stringId); frag1.setArguments(args); fTrans.replace(R.id.frgmCont, frag1); 

    And now in the fragment itself:

     mSelectedItemView = (TextView) v.findViewById(R.id.selected_item); mSelectedItemView.setText(Html.fromHtml(getResources().getString(getArguments().getInt("stringId")), null, new MyHtmlTagHandler())); 
    • In case 0 I should have another fragment, and in 1,2,3 frag1. But even if you forget about it, it still gives an error when you start the application - NullPointer. - Alexey
    • @ Alexey, then you are doing something wrong. As soon as you show the error of the error, I will immediately say what is wrong. By the code that you provided, my answer will work. - Vladyslav Matviienko
    • problem solved, thanks - Alexey