0

Apologies I am a novice and learning everyday. *Edited to respond to questions.

  • My RecyclerView is placed in the mainActivity layout file and runs on app launch
  • When I try to switch fragments the recyclerview still shows on fragment change. -The fragments are definitely changing as per the navhost controller but the RecyclerView keeps showing.
  • I want the RecyclerView only to show in one fragment but everytime I shift the code to ui.home.HomeFragment or ui.HomeViewModel the app wont launch.

What I am trying to achieve;

  1. 5 fragment app with bottomNavigation tabs using default android studio setup.
  2. one of the fragments to contain the recyclerView
  3. The fragments are setup in the format ie. ui.home.HomeFragment & ui.home.HomeViewModel.

Would anyone be able to advise why when switching fragments it keeps showing the recyclerView from MainActivity? Anytime I move this recyclerView to another fragment.xml and remove it from the MainActivity.xml file it crashes.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:background="#DCDCDC"
android:layout_height="match_parent"
android:paddingTop="0dp">


<fragment
    android:id="@+id/nav_host_fragment_activity_main"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0"
    app:navGraph="@navigation/mobile_navigation" >

</fragment>


<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    android:background="#282D58"
    app:itemIconTint="@color/white"
    app:itemTextColor="@color/white"
    app:labelVisibilityMode="labeled"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_nav_menu" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="60dp"
    android:layout_marginTop="60dp"
    tools:layout_editor_absoluteX="0dp"
    tools:listitem="@layout/note_item" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

 public class MainActivity extends AppCompatActivity {

private NoteViewModel noteViewModel;
private RecyclerView RecyclerView;
private RecyclerView.Adapter Adapter;
private RecyclerView.LayoutManager LayoutManager;
private ActivityMainBinding binding;

ImageView imageView1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    BottomNavigationView navView = findViewById(R.id.nav_view);
    AppBarConfiguration appBarConfiguration = new 
AppBarConfiguration.Builder(
            R.id.navigation_logbook, R.id.navigation_settings, 
R.id.navigation_aircraft, R.id.navigation_pilots, 
R.id.navigation_totals)
            .build();
    NavController navController = Navigation.findNavController(this, 
R.id.nav_host_fragment_activity_main);
    NavigationUI.setupWithNavController(binding.navView, navController);


    RecyclerView recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    NoteAdapter adapter = new NoteAdapter();
    recyclerView.setAdapter(adapter);


    noteViewModel = new 
 ViewModelProvider.AndroidViewModelFactory(getApplication())
            .create(NoteViewModel.class);
    noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() 
 {
        @Override
        public void onChanged(List<Note> notes) {


            //update RecyclerView
            adapter.setNotes(notes);
        }
    });

    imageView1 = findViewById(R.id.flightadd);
    imageView1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, 
 AddNoteActivity.class);
            startActivityForResult(intent, 1);
        }



    });
        new ItemTouchHelper(new 
 ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.LEFT) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, 
 @NonNull RecyclerView.ViewHolder viewHolder, @NonNull 
 RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder 
 viewHolder, int direction) {
                
 
 
noteViewModel.delete(adapter.getNotes(viewHolder.getAdapterPosition()));

            }
        }).attachToRecyclerView(recyclerView);



        adapter.setOnItemClickListener(new 
   NoteAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Note note) {
                Intent intent = new Intent(MainActivity.this, 
   UpdateActivity.class);
                intent.putExtra("id", note.getId());
                intent.putExtra("title", note.getTitle());
                intent.putExtra("description", note.getDescription());
                intent.putExtra("year", note.getYear());
                intent.putExtra("flightnumber", note.getFlightnumber());
                intent.putExtra("reg", note.getReg());
                intent.putExtra("month", note.getMonth());
                intent.putExtra("date", note.getDate());
                intent.putExtra("dep", note.getDep());
                intent.putExtra("arr", note.getArr());
                intent.putExtra("total", note.getTotal());
                intent.putExtra("offb", note.getOffb());
                intent.putExtra("onb", note.getOnb());
                startActivityForResult(intent, 2);

            }
        });

}




@Override
protected void onActivityResult(int requestCode, int resultCode, 
 @Nullable Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && resultCode == RESULT_OK)
    {
        String title = data.getStringExtra("noteTitle");
        String description = data.getStringExtra("noteDescription");
        String year = data.getStringExtra("noteYear");
        String reg = data.getStringExtra("noteReg");
        String month = data.getStringExtra("noteMonth");
        String date = data.getStringExtra("noteDate");
        String flightnumber = data.getStringExtra("noteFlightnumber");
        String dep = data.getStringExtra("noteDep");
        String arr = data.getStringExtra("noteArr");
        String offb = data.getStringExtra("noteOffb");
        String onb = data.getStringExtra("noteOnb");
        String total = data.getStringExtra("noteTotal");

        Note note = new Note(title, description, year, reg, month, 
   flightnumber, date, dep, arr, offb, onb, total);
        noteViewModel.insert(note);

    }

    else if (requestCode == 2 && resultCode == RESULT_OK)
    {
        String title = data.getStringExtra("titleLast");
        String description = data.getStringExtra("descriptionLast");
        String year = data.getStringExtra("yearLast");
        String flightnumber = data.getStringExtra("flightnumberLast");
        String reg = data.getStringExtra("titleReg");
        String month = data.getStringExtra("titleMonth");
        String date = data.getStringExtra("titleDate");
        String dep = data.getStringExtra("titleDep");
        String arr = data.getStringExtra("titleArr");
        String offb = data.getStringExtra("titleOffb");
        String onb = data.getStringExtra("titleOnb");
        String total = data.getStringExtra("titleTotal");

        int id = data.getIntExtra("noteId", -1);

        Note note = new Note(title, description, year, month, reg, 
   flightnumber, date, dep, arr, offb, onb, total);
        note.setId(id);
        noteViewModel.update(note);
    }


   }
  • Move your recyclerView to a fragment, if your app is crashing post stack trace to get help. If you'll place `recyclerView` in `activity`, it would always show. – Praveen Nov 09 '21 at 11:11
  • what are u trying to accomplish here in terms of UI? the way you create an activity with a bottom navigation that holds 5 tabs "fragments" is within your XML but you added a useless recyclerview and table layout to the mix – Mahmoud Omara Nov 09 '21 at 11:11
  • you should only have the and in your activity_main, and then inside a layout file for each fragment you should put the specific design for that page – Mahmoud Omara Nov 09 '21 at 11:12
  • Thank you for the replies. I have tried to update the question above to better explain. All the 5 fragments are contained in ui.name.NameFragment & ui.name.NameViewModel. – matthew baxter Nov 10 '21 at 01:06

1 Answers1

0

Found a solution. Had to remove the Nav controller and add the following. Adding repeat copies in each activity class. RecyclerView runs in MainActivity with BottomNavbar home tab and closes on selecting new activities using bottomNavbar.

MainActivity.java

   BottomNavigationView bottomNavigationView = 
 findViewById(R.id.nav_view);
  

  bottomNavigationView.
  setOnNavigationItemSelectedListener(( 
   item) ->  {
        switch (item.getItemId()) {
        case R.id.navigation_logbook:

            break;

        case R.id.navigation_pilots:
            Intent intent2 = new 
 Intent(MainActivity.this, Pilots.class);
            startActivity(intent2);
            break;

        case R.id.navigation_settings:
            Intent intent3 = new 
  Intent(MainActivity.this, Settings.class);
            startActivity(intent3);
            break;

            case R.id.navigation_aircraft:
                Intent intent4 = new 
  Intent(MainActivity.this, Aircraft.class);
                startActivity(intent4);
                break;

            case R.id.navigation_totals:
                Intent intent5 = new 
  Intent(MainActivity.this, Totals.class);
                startActivity(intent5);
                break;

    }
    return false;

});