how to put Firebase RecyclerView inside another RecyclerView

寵の児 提交于 2019-12-08 09:54:27

问题


basically i am having an application like a homework reminder

the user can insert a list of subjects and in each subject a list of homeworks

i know its the worst practice and i cant put recyclerview inside another one

but i need that in my application as subjects cannot be counted+other things

i inserted it in firebase database successfuly like that

that is subject_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/simple_round"
android:padding="10dp"
android:layout_margin="10dp">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Subject: "/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="2dp"
    android:weightSum="6">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Teacher:"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerview_illness_item_list">
</android.support.v7.widget.RecyclerView>
</LinearLayout>

and that is homework_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/simple_round"
android:padding="10dp"
android:layout_margin="10dp">
<LinearLayout
    android:layout_width="match_parent"
    android:padding="2dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="title:"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:orientation="horizontal">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="description:"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="deadline:"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

and for only one recyclerview i do it like that

adapter = new FirebaseRecyclerAdapter<Test, TestViewHolder>(Test.class, R.layout.test_main_body_item, TestViewHolder.class, reference.child("subjects/98794656")) {
        @Override
        protected void populateViewHolder(TestViewHolder viewHolder, Test model, int position) {
            viewHolder.title.setText(model.getTitle());
            viewHolder.description.setText(model.getDescription());
        }
    };

so how can i do for a recycler view insider another recyclerview ??

thanks


回答1:


What you want to do is detect what is clicked and navigate to another activity to show the list of data that also contains right?

But before that you need an EXTRA_KEY bundle to tell your new activity that it needs to receive something from his parent. e.g: "Hey, i am sending you a package, extract it and use it wisely in your environment."

Now to create that EXTRA BUNDLE is simple. Just write this in your local variable .

The activity you want navigate to, put this there:

public static final String EXTRA_NAME = "cheese_name";
public static final String EXTRA_POST_KEY = ""; `

Intent intent = getIntent();
final String cheeseName = intent.getStringExtra(EXTRA_NAME);`

Get the database reference key for the post also:

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Launch PostDetailActivity
                    Intent intent = new Intent(getActivity(),   CheeseDetailActivity.class);

  //How you are sending something to another activity


intent.putExtra(CheeseDetailActivity.EXTRA_POST_KEY, postKey);
                    startActivity(intent);
                }
            });


    }
};

You referenced to a child ("test").

adapter = new FirebaseRecyclerAdapter<Test, TestViewHolder>(Test.class, R.layout.test_main_body_item, TestViewHolder.class, reference.child("test")) {
    @Override
    protected void populateViewHolder(TestViewHolder viewHolder, Test model, int position) {

    final DatabaseReference postRef = getRef(position);


            // Set click listener for the whole post view
            final String postKey = postRef.getKey();

        viewHolder.title.setText(model.getTitle());
        viewHolder.description.setText(model.getDescription());
    }
};


来源:https://stackoverflow.com/questions/38987278/how-to-put-firebase-recyclerview-inside-another-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!