【发布时间】:2020-10-28 05:13:56
【问题描述】:
我在 Fragment 的nestedscrollview 内创建了firebase recyclerview。它运行良好。但是当我在firebase recyclerview 中使用updateoptions 时,recyclerview 不起作用。 我不知道如何解决这个问题。
最初创建的片段加载了来自 firebase 的项目。当我单击按钮更新选项时,recyclerview 不起作用
这是我的 firebaserecyclerview
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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:background="@drawable/fragment_bg"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:layout_marginBottom="50dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/montserrat"
android:text="Hi, Aashik"
android:textColor="@color/colorPrimary"
android:layout_marginHorizontal="20dp"
android:textSize="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/montserrat_bold"
android:layout_marginHorizontal="20dp"
android:text="Welcome Back"
android:textColor="@color/colorPrimary"
android:textSize="30dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:fontFamily="@font/montserrat_bold"
android:text="Category"
android:textColor="@color/colorPrimaryDark"
android:textSize="20sp" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/montserrat_bold"
android:text="Products"
android:layout_marginVertical="10dp"
android:layout_marginHorizontal="20dp"
android:textColor="@color/colorPrimaryDark"
android:textSize="20sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/productsRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="100dp"
android:layoutAnimation="@anim/layoutanimation"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
代码是
productsRef = FirebaseDatabase.getInstance().getReference().child("products");
recyclerView = (RecyclerView) root.findViewById(R.id.productsRecyclerView);
btn= (Button) root.findViewById(R.id.btn);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
FirebaseRecyclerOptions<Products> options=
new FirebaseRecyclerOptions.Builder<Products>()
.setQuery(productsRef, Products.class)
.build();
adapter = new FirebaseRecyclerAdapter<Products, ProductViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final ProductViewHolder holder, final int position, @NonNull final Products model) {
holder.productNameTv.setText(model.getName());
Picasso.get().load(model.getImageurl()).fit().into(holder.productImageIv);
holder.cardcontainer.setAnimation(AnimationUtils.loadAnimation(getActivity(),R.anim.cardviewanimation));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ProductDetailsActivity.class);
intent.putExtra("imageurl",model.getImageurl());
intent.putExtra("name",model.getName());
intent.putExtra("desc",model.getDesc());
intent.putExtra("mrp",model.getMrp());
intent.putExtra("availability",model.getAvailability());
intent.putExtra("pid",model.getPid());
startActivity(intent);
}
});
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items_layout, parent, false);
ProductViewHolder holder = new ProductViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Query query = productsRef.orderByChild("category").equalTo(catName);
FirebaseRecyclerOptions<Products> newOptions=
new FirebaseRecyclerOptions.Builder<Products>()
.setQuery(query, Products.class)
.build();
adapter.updateOptions(newOptions);
);
recylerview 布局是
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginVertical="10dp"
app:cardCornerRadius="2dp"
app:cardElevation="5dp"
android:layout_marginHorizontal="20dp"
android:id="@+id/cardcontainer"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/productImageIv"
android:layout_width="100dp"
android:layout_height="match_parent"
android:src="@drawable/ic_topsym"
android:layout_margin="10dp"
>
</ImageView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/productNameTv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:text="@string/welcome"
android:textColor="@color/colorBlack"
android:fontFamily="@font/arimo_bold"
android:padding="20dp">
</TextView>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
提前致谢。
【问题讨论】:
标签: android firebase android-recyclerview android-scrollview