Gum Naam
Gum Naam

Reputation: 155

How to show same firebase user data multiple time in recyclerview?

I am developing an app in which I am showing users data in a recyclerview. I have used a library which shows preview of a url. My Problem Each user has video1, video2, and video3 nodes and I want to display the data of each user 3 times i.e. in the first itemView I want to display video1, in the 2nd video2, and in the 3rd video3. So each user will have 3 itemViews. Below is my code. Please do ask if you need any clarification. Thanks in advance

[![Database Struction][1]][1] [1]: https://i.sstatic.net/XJQqt.png

Adapter Class

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;
    ArrayList<ModelClass> modelClass = new ArrayList<>();

    public MyAdapter(Context context, ArrayList<ModelClass> modelClass) {
        this.context = context;
        this.modelClass = modelClass;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.gig_display_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

                holder.urlEmbeddedView.setURL(modelClass.get(position).getVideo1(), new URLEmbeddedView.OnLoadURLListener() {
                    @Override
                    public void onLoadURLCompleted(URLEmbeddedData data) {
                        holder.urlEmbeddedView.title(data.getTitle());
                        holder.urlEmbeddedView.description(data.getDescription());
                        holder.urlEmbeddedView.host(data.getHost());
                        holder.urlEmbeddedView.thumbnail(data.getThumbnailURL());
                        holder.urlEmbeddedView.favor(data.getFavorURL());
                    }
                });

    }

    @Override
    public int getItemCount() {
        return modelClass.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        URLEmbeddedView urlEmbeddedView;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            urlEmbeddedView = itemView.findViewById(R.id.urlView);
        }
    }
}

Model Class

public class ModelClass {

    public String  name, video1, video2, video3, videos;
    int showVideoCount = 1;
    int lifeTimeClicks = 0;
    int lifeTimeClicksOnProfile = 0;
    int dailyClicksOnProfile = 0;
    int dailyClicksByYou = 0;

    public ModelClass(String name, String video1, String video2, String video3) {
        this.name = name;
        this.video1 = video1;
        this.video2 = video2;
        this.video3 = video3;
    }
 public ModelClass() {
    }
  public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVideo1() {
        return video1;
    }

    public void setVideo1(String video1) {
        this.video1 = video1;
    }

    public String getVideo2() {
        return video2;
    }

    public void setVideo2(String video2) {
        this.video2 = video2;
    }

    public String getVideo3() {
        return video3;
    }

    public void setVideo3(String video3) {
        this.video3 = video3;
    }

HomeFragment

  dRef.addListenerForSingleValueEvent(new ValueEventListener() { // singleValueEvent will only call the firebase database once
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                list = new ArrayList<ModelClass>();
                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                    ModelClass modelClass = dataSnapshot.getValue(ModelClass.class);
                    list.add(modelClass);
                    Collections.shuffle(list); // This will shuffle the links
                }
                MyAdapter adapter  = new MyAdapter(getActivity(), list);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();

Upvotes: 0

Views: 43

Answers (1)

Noman Uddin
Noman Uddin

Reputation: 307

Sir Use Nested Recyclerview, Inside your main Item view in xml use another recyclerview.

e.g: MainAcitivty.xml

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</android.support.constraint.ConstraintLayout>

FirstItem.xml:

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            
                <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:cardBackgroundColor="#f3f3f3"
                    app:cardElevation="8dp"
                    android:layout_margin="12dp">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="12dp"
                        android:orientation="vertical">
                        <TextView
                            android:id="@+id/tv_item_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="12sp"
                            android:textSize="18sp"
                            android:text="Item Title"/>
//recycler for video
                        <android.support.v7.widget.RecyclerView
                            android:id="@+id/rv_sub_item"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"/>
                    </LinearLayout>
                </android.support.v7.widget.CardView>
            
            </LinearLayout>
        

SubItem.xml (for displaying videos) :

 <?xml version="1.0" encoding="utf-8"?>
           <FrameLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            
                <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="12dp">
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        
                            <TextView
                                android:id="@+id/tv_sub_item_title"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:textStyle="bold"
                                android:text="Sub item title"/> //Video
                      
                    </RelativeLayout>
                </android.support.v7.widget.CardView>
            
            </FrameLayout>

you have model and adapter for MainItem already, Now create model and adapter for subitem(for videos), now in onBindViewHolder of the MainItemAdapter, set subItem layout, also initialize in ItemViewHolder in MainItemAdapter.

e.g. something like this

  @Override
    public void onBindViewHolder(@NonNull ItemViewHolder itemViewHolder, int i) {
   
     LinearLayoutManager layoutManager = new LinearLayoutManager(
                    itemViewHolder.rvSubItem.getContext(),
                    LinearLayoutManager.VERTICAL,
                    false
            );
     // Create sub item view adapter
      SubItemAdapter subItemAdapter = new SubItemAdapter(item.getSubItemList());
    
            itemViewHolder.rvSubItem.setLayoutManager(layoutManager);
            itemViewHolder.rvSubItem.setAdapter(subItemAdapter);
            itemViewHolder.rvSubItem.setRecycledViewPool(viewPool);

 }

    class ItemViewHolder extends RecyclerView.ViewHolder {
            private TextView tvItemTitle;
            private RecyclerView rvSubItem;
    
            ItemViewHolder(View itemView) {
                super(itemView);
                tvItemTitle = itemView.findViewById(R.id.tv_item_title);
                rvSubItem = itemView.findViewById(R.id.rv_sub_item);
            }
        }

Upvotes: 1

Related Questions