从Firestore改编的Recyclerview没有出现在片段上(带有选项卡) [英] Recyclerview adapted from Firestore not appearing on Fragment (with tabs)

查看:73
本文介绍了从Firestore改编的Recyclerview没有出现在片段上(带有选项卡)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将Recyclerview(从Firestore中加载)到活动中,则可以正常工作.

My Recyclerview (which is loaded from Firestore) works if I load it on the activity.

我刚刚在ConstraintLayout中添加了一个TabLayout和一个ViewPager:

I just added a TabLayout and a ViewPager to my ConstraintLayout:

   <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        app:layout_constraintTop_toBottomOf="@id/storeIDLayout"
        >
        <android.support.design.widget.TabItem
            android:id="@+id/tabItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TEST 1" />
        <android.support.design.widget.TabItem
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test 2" />
    </android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/tabs"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

并将RecyclerView移至fragment.xml:

and moved out the RecyclerView to fragment.xml:

<android.support.constraint.ConstraintLayout
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"
android:id="@+id/ConstraintLayout"
>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_prodlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    app:layout_constraintBottom_toBottomOf="@+id/ConstraintLayout"
    app:layout_constraintLeft_toLeftOf="@+id/ConstraintLayout"
    app:layout_constraintRight_toRightOf="@+id/ConstraintLayout"
    tools:listitem="@layout/item_addorders" />

</android.support.constraint.ConstraintLayout>

问题: 在活动中,我有这个:

Question: In the activity, I have this:

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

    if (mAdapter != null) {
        mAdapter.startListening();
    }
}

@Override
public void onStop() {
    super.onStop();
    if (mAdapter != null) {
        mAdapter.stopListening();
    }
}

我该在哪里插入片段?

Where do I insert this in my fragment?

第二个问题:

我的片段显示为空.如果我取消注释Textview(R.id.section_label)-textview可以正常工作.但是recyclerview没有出现.知道为什么吗?

My fragment is appears empty. If I uncomment the Textview (R.id.section_label) - the textview works fine. But the recyclerview does not show up. Any idea why?

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }
}

public static class PlaceholderFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_add_new_order, container, false);

        FirebaseFirestore mFirestore;
        Query mQuery;
        AddOrderAdapter mAdapter;

        RecyclerView mAddOrderRecycler = rootView.findViewById(R.id.recycler_prodlist);
        mAddOrderRecycler.setLayoutManager(new LinearLayoutManager(this.getActivity()));

        mFirestore = FirebaseFirestore.getInstance();

        mQuery = mFirestore
                .collection("PROD")
                .document("TEST")
                .collection("Items")
                .orderBy("Name", Query.Direction.ASCENDING);
        mAdapter = new AddOrderAdapter(mQuery, getActivity());

        mAddOrderRecycler.setAdapter(mAdapter);

        //TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        //textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));

        return rootView;
    }
}

已编辑以包含模型:

import com.google.firebase.firestore.IgnoreExtraProperties;

@IgnoreExtraProperties
public class AddOrderProductList {
    private String Barcode;
    private String ImageURL;
    private String Name;
    private String Unit;

    public AddOrderProductList() {}

    public String getBarcode() {
        return Barcode;
    }

    public String getImageURL() {
        return ImageURL;
    }

    public String getName() {
        return Name;
    }

    public String getUnit() {
        return Unit;
    }

    public AddOrderProductList(String barcode, String imageURL, String name, String unit) {
        Barcode = barcode;
        ImageURL = imageURL;
        Name = name;
        Unit = unit;
    }
}

第二次

import com.google.firebase.firestore.IgnoreExtraProperties;

@IgnoreExtraProperties
public class AddOrderProductList {
    private String barcode;
    private String imageURL;
    private String name;
    private String unit;

    public AddOrderProductList() {}

    public String getBarcode() {
        return barcode;
    }

    public String getImageURL() {
        return imageURL;
    }

    public String getName() {
        return name;
    }

    public String getUnit() {
        return unit;
    }

    public AddOrderProductList(String barcode, String imageURL, String name, String unit) {
        this.barcode = barcode;
        this.imageURL = imageURL;
        this.name = name;
        this.unit = unit;
    }
}

数据库结构:

PROD -> TEST -> Items -> Document of items with fields:

"Barcode" - String
"Name" - String
"Unit" - String
"ImageURL" - String

推荐答案

第一个问题的答案很简单:

The answer to the first question is very simple:

我该在哪里插入片段?

Where do I insert this in my fragment?

正如您在活动类中完成的工作一样,它也在片段类中也进行了工作.覆盖onStart()onStop()方法,并开始/停止侦听更改.另外,在onStart()方法中,无需检查是否为空,只需直接使用mAdapter.startListening();.

As you did it in your activity class and it worked, do it also in your fragment class. Override both onStart() and onStop() methods and start/stop listening for changes. Also, in the onStart() method, there is no need to check for nullity, just use mAdapter.startListening(); directly.

关于第二个问题,当您使用ID为section_label的视图注释该视图并且可以正常工作时,最有可能是因为您的视图的宽度和高度均设置为match_parent,这意味着它需要全部您的视图总空间.

Regarding your second question, when you are commenting the view with the id section_label and it works, it most likely because your view is set to match_parent for both width and height, which in term means that it take all the total space of your view.

关于模型类,还需要记住一件事.当Firebase Realtime Database SDK对来自数据库的对象进行反序列化时,它正在寻找遵循JavaBeans原则并相应地命名为 Java命名约定.因此,模型类中的字段应命名为barcode而不是Barcode,并带有相应的getter getBarcode().您的模型类应如下所示:

There is one more thing that you need to keep in mind regarding your model class. When the Firebase Realtime Database SDK deserializes objects coming from the database, is looking for fields that follow the principles of the JavaBeans and are named accordingly to Java Naming Conventions. So a field in your model class should be named barcode and not Barcode, with the corresponding getter getBarcode(). You model class should look like this:

public class AddOrderProductList {
    private String barcode;
    private String imageURL;
    private String name;
    private String unit;

    public AddOrderProductList() {}

    public AddOrderProductList(String barcode, String imageURL, String name, String unit) {
        this.barcode = barcode;
        this.imageURL = imageURL;
        this.name = name;
        this.unit = unit;
    }

    public String getBarcode() {return barcode;}
    public String getImageURL() {return imageURL;}
    public String getName() {return name;}
    public String getUnit() {return unit;}
}

这篇关于从Firestore改编的Recyclerview没有出现在片段上(带有选项卡)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆