Skip to content

Instantly share code, notes, and snippets.

@senamit2708
Created October 21, 2018 07:00
Show Gist options
  • Save senamit2708/795a5c20fc84aa8479768c68b7a4ab98 to your computer and use it in GitHub Desktop.
Save senamit2708/795a5c20fc84aa8479768c68b7a4ab98 to your computer and use it in GitHub Desktop.
Inteface for sending data from adapter to the class..here i am sending data from recyclerview adapter to its fragment.
package com.example.senamit.stationaryhutpro.fragments;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.senamit.stationaryhutpro.R;
import com.example.senamit.stationaryhutpro.adapters.FilterCategoryAdapter;
import com.example.senamit.stationaryhutpro.interfaces.FilterCategoryInterface;
import com.example.senamit.stationaryhutpro.models.FilterCategoryModel;
import com.example.senamit.stationaryhutpro.viewModels.ProductCategoryViewModel;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class FilterCategory extends Fragment implements FilterCategoryInterface {
private static final String TAG = FilterCategory.class.getSimpleName();
private Context context;
private String productCategory;
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private FilterCategoryAdapter mAdapter;
private ProductCategoryViewModel mViewModel;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//keep it in mind to get the context, might be i m wrong here
context= getActivity();
Log.i(TAG, "the context is "+context.toString());
mViewModel = ViewModelProviders.of(getActivity()).get(ProductCategoryViewModel.class);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// context = container.getContext();
View view = inflater.inflate(R.layout.activity_filter_category, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecyclerView = view.findViewById(R.id.reycler_category);
mLayoutManager = new LinearLayoutManager(context);
mAdapter = new FilterCategoryAdapter(context, this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
productCategory = mViewModel.getProductCategoryName();
mViewModel.getFilterCategory(productCategory).observe(this, new Observer<List<FilterCategoryModel>>() {
@Override
public void onChanged(List<FilterCategoryModel> filterCategoryModels) {
if (filterCategoryModels!= null){
Log.i(TAG, "the size of list is "+filterCategoryModels.size());
mAdapter.setCategory(filterCategoryModels);
}else {
Log.i(TAG, "the size of list is null");
}
}
});
}
@Override
public void funFilterCategoty(String filterCategorySelection) {
mViewModel.setFilterCategorySelected(filterCategorySelection);
}
}
package com.example.senamit.stationaryhutpro.adapters;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.senamit.stationaryhutpro.R;
import com.example.senamit.stationaryhutpro.interfaces.FilterCategoryInterface;
import com.example.senamit.stationaryhutpro.models.FilterCategoryModel;
import java.util.List;
import java.util.zip.Inflater;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class FilterCategoryAdapter extends RecyclerView.Adapter<FilterCategoryAdapter.ViewHolder> {
private static final String TAG = FilterCategoryAdapter.class.getSimpleName();
private Context context;
private List<FilterCategoryModel> productCategoryList;
private FilterCategoryInterface filterCategoryInterface;
public FilterCategoryAdapter(Context context, FilterCategoryInterface filterCategoryInterface) {
this.context = context;
this.filterCategoryInterface = filterCategoryInterface;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_filter_category, parent, false);
return new FilterCategoryAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if (productCategoryList!=null){
Log.i(TAG, "the category items are "+productCategoryList.get(position).getType());
holder.txtCategory.setText(productCategoryList.get(position).getType());
}
}
@Override
public int getItemCount() {
if (productCategoryList!= null){
Log.i(TAG,"the size of productCategoryList is "+productCategoryList.size());
return productCategoryList.size();
}else {
return 0;
}
}
public void setCategory(List<FilterCategoryModel> filterCategoryModels) {
if (filterCategoryModels!= null){
productCategoryList = filterCategoryModels;
}else {
Log.w(TAG,"filterCategorymodels is empty");
}
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView txtCategory;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtCategory = itemView.findViewById(R.id.txtCategory);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int clickedItemIndex = getAdapterPosition();
String filterCategorySelection = productCategoryList.get(clickedItemIndex).getType();
filterCategoryInterface.funFilterCategoty(filterCategorySelection);
Log.i(TAG, "the selected product category is "+filterCategorySelection);
}
}
}
package com.example.senamit.stationaryhutpro.interfaces;
public interface FilterCategoryInterface {
void funFilterCategoty(String filterCategorySelection);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment