如何在Recyclerview中多次显示相同的firebase用户数据?

发布于 2025-02-08 08:28:56 字数 3924 浏览 2 评论 0原文

我正在开发一个应用程序,其中我在Recyclerview中向用户显示数据。我使用了一个显示URL预览的库。 我的问题 每个用户都有Video1,video2和videet3节点,我想在我想在第二个video2中显示的第一个项目视图中显示每个用户的数据3次IE,以及在第三个Video3中。因此,每个用户都会有3个项目视图。以下是我的代码。请询问您是否需要任何澄清。预先感谢

[![数据库策略] [1]] [1] [1]:https://i.sstatic.net/xjqqt.png

Adapter类

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

模型类

公共类模型clupper clupper 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();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄兵必败 2025-02-15 08:28:56

SIR使用嵌套的回收库,在XML中的主要项目视图内使用另一个Recyclerview。

例如:
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(用于显示视频):

 <?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>

您已经拥有Mainitem的模型和适配器,现在创建模型和模型SubItem的适配器(用于视频),现在在MainiteMadapter的OnBindViewHolder中,SET SUBITEM布局,也在MainiteMadapter的ItemViewholder中初始化。

,例如这样的东西

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

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);
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文