Skip to content

Instantly share code, notes, and snippets.

@rhlmshr
Created November 11, 2017 21:22
Show Gist options
  • Save rhlmshr/6210946f13d77e736ecf33a4a9e85d66 to your computer and use it in GitHub Desktop.
Save rhlmshr/6210946f13d77e736ecf33a4a9e85d66 to your computer and use it in GitHub Desktop.
package com.rahulmishra.sarasmelaapp.Fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.mancj.materialsearchbar.MaterialSearchBar;
import com.rahulmishra.sarasmelaapp.R;
import com.rahulmishra.sarasmelaapp.Utils.GridAutoFitLayoutManager;
import com.rahulmishra.sarasmelaapp.activites.CategoryActivity;
import com.rahulmishra.sarasmelaapp.adapters.CategoriesRecyclerAdapter;
import com.rahulmishra.sarasmelaapp.adapters.ProductsRecyclerAdapter;
import com.rahulmishra.sarasmelaapp.models.CategoryEntity;
import com.rahulmishra.sarasmelaapp.models.ProductEntity;
import java.util.ArrayList;
import java.util.List;
public class Products extends Fragment {
private MaterialSearchBar searchBar;
private RecyclerView categories ;
private List<CategoryEntity> categoryList ;
private List<ProductEntity> searchList ;
private ProductsRecyclerAdapter searchListRecyclerAdapter ;
private CategoriesRecyclerAdapter categoriesRecyclerAdapter ;
private DatabaseReference productsRef;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_products, container, false) ;
}
@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
categories = view.findViewById(R.id.categories) ;
categoryList = feed_data() ;
categoriesRecyclerAdapter = new CategoriesRecyclerAdapter(categoryList, view.getContext());
final LinearLayoutManager verticalLayoutManager = new LinearLayoutManager
(view.getContext(), LinearLayoutManager.VERTICAL, false);
categories.setLayoutManager(verticalLayoutManager);
categories.setNestedScrollingEnabled(false);
categories.setAdapter(categoriesRecyclerAdapter);
searchList = new ArrayList<>() ;
searchBar = view.findViewById(R.id.searchBar);
searchBar.setCardViewElevation(10);
searchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
@Override
public void onSearchStateChanged(boolean enabled) {
if(enabled) {
GridAutoFitLayoutManager layoutManager = new GridAutoFitLayoutManager(view.getContext(), 300);
categories.setLayoutManager(layoutManager);
categories.setNestedScrollingEnabled(false);
searchListRecyclerAdapter = new ProductsRecyclerAdapter(searchList, view.getContext()) ;
categories.setAdapter(searchListRecyclerAdapter);
} else {
onButtonClicked(MaterialSearchBar.BUTTON_BACK) ;
}
}
@Override
public void onSearchConfirmed(CharSequence text) {
productsRef = (DatabaseReference) FirebaseDatabase.getInstance().getReference("products")
.orderByValue()
.startAt((String) text)
.endAt(text+"\uf8ff").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
searchList = CategoryActivity.feed_data() ;
searchListRecyclerAdapter = new ProductsRecyclerAdapter(searchList, view.getContext()) ;
// searchListRecyclerAdapter.notifyDataSetChanged();
categories.setAdapter(searchListRecyclerAdapter);
}
@Override
public void onButtonClicked(int buttonCode) {
if(buttonCode == MaterialSearchBar.BUTTON_BACK) {
categoriesRecyclerAdapter = new CategoriesRecyclerAdapter(categoryList, view.getContext());
LinearLayoutManager verticalLayoutManager = new LinearLayoutManager
(view.getContext(), LinearLayoutManager.VERTICAL, false);
categories.setLayoutManager(verticalLayoutManager);
categories.setNestedScrollingEnabled(false);
categories.setAdapter(categoriesRecyclerAdapter);
}
}
});
}
List<CategoryEntity> feed_data() {
List<CategoryEntity> categoryList = new ArrayList<>();
categoryList.add(new CategoryEntity("handicrafts","handicrafts","Buy Handicrafts!", "Collection from\nacross India", R.drawable.handicrafts)) ;
categoryList.add(new CategoryEntity("clothes","clothes","Buy Clothes!", "Collection from\nacross India", R.drawable.clothes)) ;
categoryList.add(new CategoryEntity("homedecor","home decor", "Buy Home\nDecor!", "Collection from\nacross India", R.drawable.decor)) ;
categoryList.add(new CategoryEntity("toys","toys","Buy Toys", "Collection from\nacross India", R.drawable.toys)) ;
categoryList.add(new CategoryEntity("edibles","edibles","Buy Packed\nEdibles", "Collection from\nacross India", R.drawable.edibles)) ;
categoryList.add(new CategoryEntity( "misc","misc","Miscellaneous", "", R.drawable.more)) ;
return categoryList ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment