I have 4 fragments, and for an example with one TextView go fine. And if I add a ListView with an adapter ... Everything except this page does not go anywhere.

Code itself:

public class ExampleActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_example); } public void homeClick(View view) { Fragment myfragment; myfragment = new HomeFragment(); Log.d("fragment changed", ""); fragmentCommit(myfragment); } public void favClick(View view) { Fragment myfragment; myfragment = new FavFragment(); Log.d("fragment changed", ""); fragmentCommit(myfragment); } public void ssClick(View view) { Fragment myfragment; myfragment = new SsFragment(); Log.d("fragment changed", ""); fragmentCommit(myfragment); } public void rubClick(View view) { Fragment myfragment; myfragment = new RubFragment(); Log.d("fragment changed", ""); fragmentCommit(myfragment); } public void fragmentCommit(Fragment fragment){ FragmentManager fm = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragmentTransaction.replace(R.id.fragment_switch, fragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } } 

Container:

 <fragment android:id="@+id/fragment_switch" android:name="com.isakovch.Example.HomeFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/linearLayout2" /> 

HomeFragment:

 public class HomeFragment extends Fragment { List<VacancyModel> vacancyModelList; public static List<VacancyModel> vacancyModelList2; public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_home, container, false); ListView listView = (ListView) rootView.findViewById(R.id.lvMain); MainVacancyAdapter adapter; if (getActivity().getIntent().getBooleanExtra(SplashScreen.DATA_FROM_SPLASH_SCREEN, true)) { Bundle bundle = getActivity().getIntent().getExtras(); vacancyModelList = (List<VacancyModel>) bundle.getSerializable(VacancyModel.class.getCanonicalName()); vacancyModelList2 = vacancyModelList; //Creating an adapter and setting it to the list adapter = new MainVacancyAdapter(getActivity().getApplicationContext(), R.layout.row_main_vacancies, vacancyModelList); } else { //Creating an adapter and setting it to the list adapter = new MainVacancyAdapter(getActivity().getApplicationContext(), R.layout.row_main_vacancies, vacancyModelList2); } listView.setAdapter(adapter); return rootView; } } 

And the rest are empty, like this:

 public class FavFragment extends Fragment { public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_fav, container,false); } } 

What is the problem, tell me, rummaged everywhere?

By the way, if the same ListView code with an adapter is transferred to FavFragment or to other fragments, it works fine ...

  • one
    and how do you call favClick, ssClick ... Where is the activation code? - Kirill Stoianov
  • In XML I prescribe where onClick ... Updated code ... - DevOma

1 answer 1

Replace <fragment /> with some kind of ViewGroup container. For example, <FrameLayout />

  • Now does not automatically show the first HomeFragment - DevOma
  • In onCreate () of your activation, add fragmentCommit (new HomeFragment ()) to the end; - Dmitry Guselnikov
  • Thank you so much, earned !!!! - DevOma pm