簡體   English   中英

RecyclerView 只顯示一項

[英]RecyclerView shows only one item

我試圖顯示來自 firebase firestore 的信息,但我在 Recycleview 中只得到一條記錄。 我不知道這是什么原因。 我的 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();
}
}

清單 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>

項目 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"/>

我雖然這是因為列表 xml layout_height ,但如果我將它設置為wrap_content ,那么列表是空的。 你能幫忙嗎?

item.xml具有LinearLayout高度,例如match_parent android:layout_height="match_parent"這是錯誤的。 將其更改為wrap_content即可解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM