如何在父活动的约束布局中添加RecyclerView?

时间:2019-02-17 16:27:57

标签: java android android-recyclerview

我有一个基础导航活动。此活动由其中包含回收者视图的主页活动扩展。我试图将回收者视图添加到基础导航活动中定义的约束布局中。回收者视图的数据来自Firebase数据库。但是,我无法弄清楚如何将回收者视图添加到约束布局中。

对于没有导航栏的活动,回收站视图正常工作。因此,我想问题不在于检索数据。但是,当Homepage活动扩展了BaseNav活动时,不会显示任何结果。

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);
}

首页活动:

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>

我需要将回收站视图与导航栏一起显示在屏幕上。 我到底该如何实现?

0 个答案:

没有答案