我的Recyclerview(从Firestore加载)可以在我加载活动时使用。
我刚刚将一个TabLayout和一个ViewPager添加到我的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:
<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>
问题: 在活动中,我有这个:
@Override
public void onStart() {
super.onStart();
if (mAdapter != null) {
mAdapter.startListening();
}
}
@Override
public void onStop() {
super.onStop();
if (mAdapter != null) {
mAdapter.stopListening();
}
}
我在哪里插入片段?
第二个问题:
我的片段显示为空。如果我取消注释Textview(R.id.section_label) - textview工作正常。但是,Recyclerview没有显示出来。知道为什么吗?
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
答案 0 :(得分:1)
第一个问题的答案很简单:
我在哪里插入片段?
正如您在活动类中所做的那样,它也可以在您的片段类中执行。覆盖onStart()
和onStop()
方法并开始/停止侦听更改。此外,在onStart()
方法中,无需检查无效,只需直接使用mAdapter.startListening();
。
关于您的第二个问题,当您使用ID section_label
对视图进行评论并且它有效时,很可能是因为您的视图的宽度和高度都设置为match_parent
,这在术语上意味着它占用了你视图的所有空间。
关于模型类,还需要记住一件事。当Firebase实时数据库SDK反序列化来自数据库的对象时,正在查找遵循JavaBeans原则的字段,并相应地命名为Java Naming Conventions。因此,模型类中的字段应命名为barcode
而不是Barcode
,并使用相应的getter getBarcode()
。你的模型类应该是这样的:
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;}
}