简体   繁体   English

RecyclerView 只显示一项

[英]RecyclerView shows only one item

Im trying to display info from firebase firestore, but I get only one record in Recycleview.我试图显示来自 firebase firestore 的信息,但我在 Recycleview 中只得到一条记录。 I do not know the reason of this.我不知道这是什么原因。 I have multiple records in my firestore collection, but only first one in list is returned/displayed.我的 Firestore 集合中有多个记录,但仅返回/显示列表中的第一个。

public class fragment_history extends Fragment {
private final String TAG = "err";
private FirestoreRecyclerAdapter adapter;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_history, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    
    RecyclerView firestoreList = view.findViewById(R.id.recycler_view);
    
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    String userId = Objects.requireNonNull(user).getUid();
    
    Query query = firebaseFirestore.collection("user").document(userId).collection("prod1").limit(10);
    FirestoreRecyclerOptions<ProductModel> options = new FirestoreRecyclerOptions.Builder<ProductModel>()
            .setQuery(query, ProductModel.class)
            .build();
    
    adapter = new FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
        @NonNull
        @Override
        public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.display_item, parent, false);
            return new ProductViewHolder(view);
        }

        @SuppressLint("SetTextI18n")
        @Override
        protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull ProductModel model) {
            holder.product_name.setText(model.getName());
            holder.product_price.setText(model.getPrice() + "");
        }
    };

    firestoreList.setHasFixedSize(true);
    firestoreList.setAdapter(adapter);

}
private static class ProductViewHolder extends RecyclerView.ViewHolder {
    private final TextView product_name;
    private final TextView product_price;

    public ProductViewHolder(@NonNull View itemView) {
        super(itemView);

        product_name = itemView.findViewById(R.id.product_name);
        product_price = itemView.findViewById(R.id.product_price);
    }

}

@Override
public void onStop() {
    super.onStop();
    adapter.stopListening();
}

@Override
public void onStart() {
    super.onStart();
    adapter.startListening();
}
}

LIST XML清单 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:orientation="vertical">


    <androidx.recyclerview.widget.RecyclerView
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view" />

</RelativeLayout>

ITEM XML项目 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/product_name"
    android:text="item1name"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/product_price"
    android:text="item1price"/>

I though this was because of list xml layout_height , but if I set it to wrap_content , then list is empty.我虽然这是因为列表 xml layout_height ,但如果我将它设置为wrap_content ,那么列表是空的。 Can you please help?你能帮忙吗?

The item.xml has LinearLayout height like match_parent android:layout_height="match_parent" which is wrong. item.xml具有LinearLayout高度,例如match_parent android:layout_height="match_parent"这是错误的。 Change it to wrap_content and it will solve the problem.将其更改为wrap_content即可解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM