1

I have a base navigation activity.This is extended by a homepage activity that has a recycler view in it.I'm attempting to add the recycler view to a constraint layout that is defined in the base navigation activity. The data for the recycler view is from a firebase database. I'm however not able to figure out how to add the recycler view to the constraint layout.

The recycler view works properly for a activity without the nav bar.So I guess the problem isn't with retrieving the data.But when the Homepage activity extends the BaseNav activity, no result is displayed.

BaseNavActivity:

public class BaseNavActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

ConstraintLayout constraintLayout;


protected void onCreateDrawer(Bundle savedInstanceState) {
    setContentView(R.layout.activity_base_nav);
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    constraintLayout = findViewById(R.id.constraint_layout_nav_bar);
}

Homepage Activity:

public class Homepage extends BaseNavActivity {
RecyclerView allEventsRecyclerView;
EventListAdapter eventListAdapter;
View contentView;
LayoutInflater inflater;
ArrayList<EventDetails> returnArrayList;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
ChildEventListener childEventListener;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.onCreateDrawer(savedInstanceState);
    inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    contentView = inflater.inflate(R.layout.activity_homepage, allEventsRecyclerView,false);
    constraintLayout.addView(contentView,0);
    allEventsRecyclerView = findViewById(R.id.homepageRecyclerView);
    allEventsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    returnArrayList = new ArrayList<>();
    eventListAdapter = new EventListAdapter(this,returnArrayList);
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference().child("AE").child("someplace");
    childEventListener = new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            returnArrayList.add(dataSnapshot.getValue(EventDetails.class));
            eventListAdapter.notifyDataSetChanged();
            allEventsRecyclerView.setAdapter(eventListAdapter);
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };
    databaseReference.addChildEventListener(childEventListener);

}
}

activity_homepage.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Homepage">

<android.support.v7.widget.RecyclerView
    android:id="@+id/homepageRecyclerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

activity_base_nav.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_base_nav"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_base_nav"
    app:menu="@menu/activity_base_nav_drawer" />

</android.support.v4.widget.DrawerLayout>

I need the recycler view to appear in the screen along with the nav bar. How exactly can I achieve this ?

twothreezarsix
  • 375
  • 3
  • 13
  • Please add the code for activity_base_nav.xml and activity_homepage.xml. – Bö macht Blau Feb 17 '19 at 16:57
  • Added the resource files – twothreezarsix Feb 17 '19 at 17:09
  • I could ask for app_bar_base_nav.xml but I think in the end the solution will be to add just the RecyclerView (which you don't need to *inflate*, just use `new RecyclerView(HomepageActivity.this)`) to the parent ViewGroup of the nav bar (which I suppose is a ConstraintLayout). If you add aView programmatically, you always have to provide something like LayoutParams as well. [This post](https://stackoverflow.com/a/42846520/5015207) sketches how to do it for ConstraintLayout – Bö macht Blau Feb 17 '19 at 17:27

0 Answers0