I'm trying to use a nested recycler view to show some products from Firestore in a home fragment that is similar to play store.
the screen doesn't have an action bar. I have used Search View inside a layout and following that the Nested Recycler View comes. I have a Search View i.e iconifiedByDefault="true"
.
My Recycler View is not loading until I click on the Search View.
The same thing happens when I use iconifiedByDefault="false"
.
XML code for home fragment:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/search_layout"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingEnd="20dp"
android:paddingRight="20dp"
android:paddingBottom="12dp">
<androidx.appcompat.widget.SearchView
android:id="@+id/srchView"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/searchview_background"
app:iconifiedByDefault="true"
app:queryBackground="@android:color/transparent"
app:queryHint="Search by product name.."
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingStart="20dp"
android:paddingTop="0dp"
android:paddingEnd="20dp"
android:paddingBottom="0dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
HomeFragment.class
public class HomeFragment extends Fragment implements Serializable{
public HomeFragment() {
// Required empty public constructor
}
ProgressDialog progressDialog;
ImageView cartIcon;
SearchView srchView;
RecyclerView parentView;
RecyclerView.Adapter parentAdapter;
ArrayList<ParentModel> parentModelArrayList = new ArrayList<>();
RecyclerView.LayoutManager parentLaoutManager;
@SuppressLint("NotifyDataSetChanged")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
parentView = view.findViewById(R.id.parentView);
addData();
parentLaoutManager = new LinearLayoutManager(getActivity());
parentView.setLayoutManager(parentLaoutManager);
parentAdapter = new ParentAdapter(parentModelArrayList, getActivity());
parentModelArrayList = new ArrayList<>();
parentView.setAdapter(parentAdapter);
parentAdapter.notifyDataSetChanged();
return view;
}
private void addData() {
parentModelArrayList.clear();
parentModelArrayList.add(new ParentModel("Tasty Snacks"));
parentModelArrayList.add(new ParentModel("Personal Care"));
}
}
ParentAdapter.class
public class ParentAdapter extends RecyclerView.Adapter<ParentAdapter.ViewHolder> {
List<ParentModel> parentModelList;
Activity activity;
public ParentAdapter(List<ParentModel> parentModelList, Activity activity) {
this.parentModelList = parentModelList;
this.activity = activity;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.parent_view_layout, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
ParentModel currentItem = parentModelList.get(position);
RecyclerView.LayoutManager lm = new LinearLayoutManager(activity,LinearLayoutManager.HORIZONTAL,false);
holder.prntRView.setLayoutManager(lm);
ArrayList<ProductDataModel> prdModellist = new ArrayList<>();
ProductAdapter productAdapter = new ProductAdapter(holder.prntRView.getContext(),prdModellist);
holder.prntRView.setAdapter(productAdapter);
holder.title.setText(currentItem.categoryText());
holder.viewAllBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("btn id", currentItem.categoryText());
showLanding(currentItem.categoryText());
}
});
FirebaseFirestore firebaseFirestore;
if (parentModelList.get(position).categoryText().equals("Tasty Snacks")) {
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection(COLLECTION_NAME).whereEqualTo(CONDITION).get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot queryDocumentSnapshot: task.getResult()) {
prdModellist.add(new ProductDataModel(
(String) queryDocumentSnapshot.get("productimage"),
(String) queryDocumentSnapshot.get("productname"),
(String) queryDocumentSnapshot.get("description"),
(Long) queryDocumentSnapshot.get("price"),
(Long) queryDocumentSnapshot.get("discount")
));
}
} else {
String error = task.getException().getMessage();
System.out.println(error);
}
}
}
);
}
if (parentModelList.get(position).categoryText().equals("Personal Care")) {
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection(COLLECTION).whereEqualTo(CONDITION2).get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (QueryDocumentSnapshot queryDocumentSnapshot: Task.getResult()) {
prdModellist.add(new ProductDataModel(
(String) queryDocumentSnapshot.get("productimage"),
(String) queryDocumentSnapshot.get("productname"),
(String) queryDocumentSnapshot.get("description"),
(Long) queryDocumentSnapshot.get("price"),
(Long) queryDocumentSnapshot.get("discount")
));
}
} else {
String error = task.getException().getMessage();
System.out.println(error);
}
}
}
);
}
}
private void showLanding(String categoryText) {
Intent intent;
switch (categoryText){
case "Tasty Snacks":
case "Personal Care":
intent = new Intent(activity,ProductsLandingActivity.class);
intent.putExtra("title",categoryText);
activity.startActivity(intent);
break;
}
}
@Override
public int getItemCount() {
return parentModelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
MaterialButton viewAllBtn;
RecyclerView prntRView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.prntTitle);
viewAllBtn = itemView.findViewById(R.id.viewAllBtn);
prntRView = itemView.findViewById(R.id.prntRView);
}
}
}