如何在Android Firebase中创建异构RecyclerView

时间:2018-07-25 09:41:36

标签: firebase android-studio firebase-realtime-database android-recyclerview firebaseui

我的mainApp活动中包含一个recyclerView。我从firebase动态地将卡添加到此recyclerView。这是我的firebase数据库结构。

enter image description here

我想在Google Playstore中显示我的数据。那是标题:F&B,Vendors,Venue等节目显示为长水平卡,图像中标记有红色区域,我想从firebase数据库的lond卡视图中生成图像。

enter image description here

我使用recyclerview实现此目的。我在主要活动中添加了一个recyclerView。我动态地创建了长牌。另外,因为我想在长卡内生成卡。我在生成的动态卡的布局中添加了RecycerView,如图所示。

但是我无法在包含textView和RecyclerView的动态生成的长卡中插入数据。如果我能够在生成的日志中填充recyclerView。我可能会达到上述目的。

这是我用来实现上述功能的代码。

我有3个文件,即-  -MainActivity(一个片段文件,因为IAM使用制表符)。  -模型文件以获取数据。  -RecyclerView适配器。

MainActivity.java是我的主要片段文件

public class fragment_home extends Fragment {

RecyclerView recyclerViewTopLevel;
RecyclerView recyclerViewSecondLevel;
RecyclerView recyclerViewInCard;

SnapHelper snapHelper;
public String key;
public String keyMajorCard;
public String keySmallCard;

List<ModelCard> list = new ArrayList<>();
List<MainCardModel> mainCardModelList = new ArrayList<>();
List<SmallCardModel> smallCardModelList = new ArrayList<>();

ModelCard modelCard;
MainCardModel mainCardModel;
SmallCardModel smallCardModel;

public static final String Database_Path = "Featured";
public static final String Database_MajorCards = "MajorCards";

public DatabaseReference databaseReference;

RecyclerAdapter recyclerAdapter;
AdapterSecondView recyclerAdapterSecondLevel;
RecyclerAdapterInCard recyclerAdapterInCard;

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

    recyclerViewTopLevel = view.findViewById(R.id.recyclerTopLevel);
    recyclerViewTopLevel.setHasFixedSize(true);
    recyclerViewTopLevel.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));

    recyclerViewSecondLevel = view.findViewById(R.id.recyclerSecondLevel);
    recyclerViewSecondLevel.setHasFixedSize(true);
    recyclerViewSecondLevel.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
    recyclerViewSecondLevel.setNestedScrollingEnabled(false);
    recyclerViewSecondLevel.setVerticalScrollBarEnabled(false);

    final LayoutInflater layoutInflater = getLayoutInflater();
    final View mmView = layoutInflater.inflate(R.layout.main_card_layout,null);

    recyclerViewInCard = mmView.findViewById(R.id.recyclerInCard);
    recyclerViewInCard.setHasFixedSize(true);
    recyclerViewInCard.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));

    snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(recyclerViewTopLevel);

    databaseReference = FirebaseDatabase.getInstance().getReference();
    databaseReference.child(Database_Path).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            modelCard = dataSnapshot.getValue(ModelCard.class);

            key = dataSnapshot.getKey();
            list.add(modelCard);

            recyclerAdapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    databaseReference.child(Database_MajorCards).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            mainCardModel = dataSnapshot.getValue(MainCardModel.class);

            keyMajorCard = dataSnapshot.getKey();
            mainCardModelList.add(mainCardModel);

            recyclerAdapterSecondLevel.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    databaseReference.child(Database_MajorCards).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            smallCardModel = dataSnapshot.getValue(SmallCardModel.class);

            keySmallCard = dataSnapshot.getKey();
            smallCardModelList.add(smallCardModel);

            recyclerAdapterInCard.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return view;
}

@Override
public void onStart() {
    super.onStart();

    MainCardModel mainCardModel1 = new MainCardModel();

    recyclerAdapter = new RecyclerAdapter(getContext(),list);
    recyclerViewTopLevel.setAdapter(recyclerAdapter);

    recyclerAdapterSecondLevel = new AdapterSecondView(getContext(),mainCardModelList);
    recyclerViewSecondLevel.setAdapter(recyclerAdapterSecondLevel);

    final LayoutInflater layoutInflater = getLayoutInflater();
    final View mmView = layoutInflater.inflate(R.layout.main_card_layout,null);
}
}

这是我的Mainactivity.xml文件,我在上面的片段里面充气

<LinearLayout 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="com.example.shiva.eventninjaz.fragments.fragment_home"
android:orientation="vertical"
android:padding="10dp">

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

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

<android.support.v7.widget.RecyclerView

    android:id="@+id/recyclerSecondLevel"
    android:layout_marginTop="@dimen/ten"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

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

这是我的模型文件MainCardModel.java

public class MainCardModel {
private transient String key;

private String Image;
private String Title;
private RecyclerAdapterInCard recyclerAdapter;

public MainCardModel(){

}

public MainCardModel(String image, String title){
    this.Image = image;
    this.Title = title;
}

public String getKey() {
    return key;
}

public void setKey(String key) {
    this.key = key;
}

public String getImage() {
    return Image;
}

public void setImage(String image) {
    Image = image;
}

public String getTitle() {
    return Title;
}

public void setTitle(String title) {
    Title = title;
}

public RecyclerAdapterInCard getRecyclerAdapter() {
    return recyclerAdapter;
}

public void setRecyclerAdapter(RecyclerAdapterInCard recyclerAdapter) {
    this.recyclerAdapter = recyclerAdapter;
}
}

这是我的RecyclerViewAdapter.java

public class AdapterSecondView extends RecyclerView.Adapter<AdapterSecondView.TaskViewHolder> {

Context context;
List<MainCardModel> dataList;

public AdapterSecondView(Context context, List<MainCardModel> tempList) {
    this.context = context;
    this.dataList = tempList;
}

@NonNull
@Override
public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_card_layout,parent,false);
    return new TaskViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
    final MainCardModel mainCardModel = dataList.get(position);

    holder.titleText.setText(mainCardModel.getTitle());
    holder.recyclerViewInCard.setAdapter(mainCardModel.getRecyclerAdapter());
}

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

public static class TaskViewHolder extends RecyclerView.ViewHolder{
    View mView;
    public RecyclerView recyclerViewInCard;
    public TextView titleText;

    public TaskViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        recyclerViewInCard = mView.findViewById(R.id.recyclerInCard);
        titleText = mView.findViewById(R.id.title);
    }
}
}

现在,我想在上面生成的长牌中添加动态牌。此卡包含图像视图。我使用picasso库将firebase图片url转换为Image并将其适合我的imageView。但是我无法在longCard内添加卡片。

这是我要在长卡smallCard.xml中生成的布局

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="170dp"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">





<android.support.v7.widget.CardView
    android:layout_width="160dp"
    android:layout_height="200dp"
    android:id="@+id/smallCard"
    android:layout_marginTop="50dp"
    android:layout_marginEnd="20dp"
    app:cardCornerRadius="20sp"
    app:cardElevation="@dimen/ten"
    app:cardPreventCornerOverlap="false"
    android:layout_centerHorizontal="true">


    <ImageView
        android:id="@+id/imageFrame_smallCard"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/test1"
        android:layout_centerHorizontal="true">
    </ImageView>


</android.support.v7.widget.CardView>

是否有其他方法可以在playstore中完成。通过某种方式,我可以在动态生成的LongCard中填充recyclerView。

请让我知道我的数据库是否正确构建。

0 个答案:

没有答案