0

I have a fragment in which i have already implemented a recyclerview at the top part off the screen which holds cardview, now i want to create another recyclerview which also adds cardview to the bottom part of the screen in the same fragment.

I'm new to android studio, so knowing where to place what is a problem for me.

This is the fragment code

    public HomeFragment(){
        //Required empty public constructor
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);

        view = inflater.inflate(R.layout.fragment_home, container, false);
        mBlogList = (RecyclerView)view.findViewById (R.id.myRecycleView);
        mBlogList.setHasFixedSize(true);
        mBlogList.setLayoutManager(new LinearLayoutManager(getContext()));
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Global");

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>
                (Blog.class,R.layout.blog_row, BlogViewHolder.class, mDatabase) {
            @Override
            protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
                viewHolder.setTitle(model.getTitle());
                viewHolder.setDesc(model.getDesc());
                viewHolder.setImage(getContext(), model.getImage());
            }
        };
        mBlogList.setAdapter(firebaseRecyclerAdapter);
    }
    public static class BlogViewHolder extends RecyclerView.ViewHolder{

        View mView;
        public BlogViewHolder(View itemView){
            super(itemView);
            mView = itemView;
        }
        public void setTitle(String title){
            TextView post_title = (TextView) mView.findViewById(R.id.post_title);
            post_title.setText(title);
        }
        public void setDesc(String desc){
            TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
            post_desc.setText(desc);
        }
        public void setImage(Context ctx, String image){
            ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
            Picasso.with(ctx).load(image).into(post_image);
        }
    }
}

Now how to add another recycler view?

This is the layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.bahdape.archangels.MainActivity"
    xmlns:tools="http://schemas.android.com/tools">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="285dp"
    android:layout_marginTop="2dp"
    android:layout_margin="5dp"
    android:background="@drawable/rounded_corner">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="285dp"
        android:id="@+id/myRecycleView"
        android:orientation="horizontal">

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>
    </RelativeLayout>

Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57
maykhid
  • 129
  • 1
  • 10
  • Can you please share your layout file? do you want that recycler view can be seen at same time on layout? – PJain Sep 13 '19 at 06:09
  • Hi! First of all, why do you have the " view = inflater.inflate(R.layout...." row two times? And if you managed to add a recyclerview, what does block you in adding a second one? – barotia Sep 13 '19 at 06:09
  • @PJain I just added the layout. And yes i want the recycler view to be on the same layout. Thanks. – maykhid Sep 13 '19 at 08:57
  • @PJain I just added the layout. And yes i want the recycler view to be on the same layout. Thanks. – maykhid Sep 13 '19 at 08:58
  • than may be you can solve the problem by putting recyclerview inside LinearLayout with define specific weight – PJain Sep 13 '19 at 09:01
  • @barotia the second "view = inflater.inflate(R.layout.." was added by mistake sorry bout that. What's blocking me is the fact that i dont know how to go about adding another recyclerview to the fragment class (in my case HomeFragment). Thanks – maykhid Sep 13 '19 at 09:03
  • @PJain ok, noted. But how do i add this new recycler view to the Fragment i posted above? – maykhid Sep 13 '19 at 09:12
  • simply as you have added one recycler view with help of holder, but only you need is create two object of holder with list and bind that with the adapters of your recyclerViews – PJain Sep 13 '19 at 09:25

1 Answers1

0

I suggest you use a ConstraintLayout instead.

For example:

<?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"
    android:id="@+id/rootContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_dark"
        app:layout_constraintBottom_toTopOf="@id/rv_2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_green_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/rv_1" />
</androidx.constraintlayout.widget.ConstraintLayout>

Will produce this:

preview

Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
  • OK thanks, but the problem now is how to add it to the fragment class i posted above? Any ideas? – maykhid Sep 13 '19 at 09:18
  • I'm not sure I follow... use this layout as your Fragment's layout. When you inflate your fragment's View... is that what you mean? – Martin Marconcini Sep 13 '19 at 09:20
  • From the code for the Fragment i posted above you can see that i added an adapter to it, which fits into the top of the fragment layout. So now, for the bottom part, i'd still like to add another adapter for it. How do i go about it? Does this make sense to you? – maykhid Sep 13 '19 at 09:33
  • Not sure why you're doing it this way: but you asked how to add another RV to the same fragment. You have it. Now if you want to populate RV #2 with data, you need another adapter for it, so ` `mBlogList.setAdapter(firebaseRecyclerAdapter);` -> this sets the adapter on RV #1. Now have `mBlogListNumberTwo.setAdapter(firebaseRecyclerAdapterNumberTwo);` to set it on your RV #2... you need to create the adapter and populate it. If you want to use the _same_ Adapter for both, I think that's not a great idea (why?) – Martin Marconcini Sep 13 '19 at 10:49
  • Sorry i'm replying a bit late.I have a fragment layout which i would like to add two recyclerviews to. The recyclerview at the top is meant to add cardviews of certain images from firebasedatabase, the one at the bottom is also supposed to be a recyclerview also to add cardviews but for different things entirely. So i just thought to add both recyclerviews to the fragment. From your comment above it seems this is not a correct approach, i'm kinda like a newbie, so if you could explain to me how i'm doing it wrong i'd really appreciate it. – maykhid Sep 15 '19 at 04:39