Created
July 19, 2016 13:40
-
-
Save motornyimaksym/b7c437bfd005da524fd01291eb4b8657 to your computer and use it in GitHub Desktop.
RecyclerViewAdapter with header and footer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.uryuk.uryukandroid.ui.adapters; | |
import android.content.Context; | |
import android.graphics.Typeface; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.EditText; | |
import android.widget.RadioButton; | |
import android.widget.RadioGroup; | |
import android.widget.TextView; | |
import java.util.List; | |
import me.uryuk.uryukandroid.R; | |
import me.uryuk.uryukandroid.entities.Product; | |
import me.uryuk.uryukandroid.entities.UserInformation; | |
import me.uryuk.uryukandroid.preferences.SharedPreferencesManager_; | |
import me.uryuk.uryukandroid.ui.callbacks.CartCallbacks; | |
import me.uryuk.uryukandroid.ui.callbacks.ProductCallbacks; | |
import me.uryuk.uryukandroid.utils.FontManager; | |
public class CartRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
private static final int TYPE_HEADER = Integer.MIN_VALUE; | |
private static final int TYPE_FOOTER = Integer.MIN_VALUE + 1; | |
private List<Product> productList; | |
private Context context; | |
private ProductCallbacks productCallbacks; | |
private CartCallbacks cartCallbacks; | |
private int normalLayoutId; | |
private int footerLayoutId; | |
private int headerLayoutId; | |
private UserInformation userInformation; | |
private int orderSum; | |
private int percentDiscount = 0; | |
private int absoluteDiscount = 0; | |
public CartRecyclerViewAdapter(Context context, List<Product> productList, UserInformation userInformation, ProductCallbacks productCallbacks, CartCallbacks cartCallbacks, int orderSum, int normalLayoutId, int footerLayoutId, int headerLayoutId) { | |
this.context = context; | |
this.productList = productList; | |
this.userInformation = userInformation; | |
this.productCallbacks = productCallbacks; | |
this.cartCallbacks = cartCallbacks; | |
this.orderSum = orderSum; | |
this.normalLayoutId = normalLayoutId; | |
this.footerLayoutId = footerLayoutId; | |
this.headerLayoutId = headerLayoutId; | |
} | |
public void setOrderSum(int orderSum) { | |
this.orderSum = orderSum; | |
notifyDataSetChanged(); | |
} | |
public void setDiscount(int percentDiscount, int absoluteDiscount) { | |
this.percentDiscount = percentDiscount; | |
this.absoluteDiscount = absoluteDiscount; | |
notifyDataSetChanged(); | |
} | |
public void update(List<Product> productList) { | |
this.productList.clear(); | |
this.productList.addAll(productList); | |
notifyDataSetChanged(); | |
} | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view; | |
if (viewType == TYPE_FOOTER) { | |
view = LayoutInflater.from(parent.getContext()).inflate(footerLayoutId, parent, false); | |
return new FooterViewHolder(view); | |
} | |
if (viewType == TYPE_HEADER) { | |
view = LayoutInflater.from(parent.getContext()).inflate(headerLayoutId, parent, false); | |
return new HeaderViewHolder(view); | |
} | |
view = LayoutInflater.from(parent.getContext()).inflate(normalLayoutId, parent, false); | |
return new NormalViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | |
try { | |
if (holder instanceof NormalViewHolder) { | |
NormalViewHolder normalViewHolder = (NormalViewHolder) holder; | |
normalViewHolder.bindView(position - 1); | |
} else if (holder instanceof FooterViewHolder) { | |
FooterViewHolder vh = (FooterViewHolder) holder; | |
vh.bindView(position); | |
} else if (holder instanceof HeaderViewHolder) { | |
HeaderViewHolder vh = (HeaderViewHolder) holder; | |
vh.bindView(position); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public int getItemCount() { | |
if (productList == null) { | |
return 0; | |
} | |
if (productList.size() == 0) { | |
//Return 1 here to show nothing | |
return 1; | |
} | |
// Add extra view to show the footer and header view | |
return productList.size() + 2; | |
} | |
@Override | |
public int getItemViewType(int position) { | |
if (position == 0) { | |
// This is where we'll add footer. | |
return TYPE_HEADER; | |
} | |
if (position == productList.size() + 1) { | |
// This is where we'll add footer. | |
return TYPE_FOOTER; | |
} | |
return super.getItemViewType(position) + 1; | |
} | |
public class NormalViewHolder extends RecyclerView.ViewHolder { | |
public TextView description_textView; | |
public TextView price_textView; | |
public TextView weight_textView; | |
public TextView title_textView; | |
public TextView addToOrder_textView; | |
public TextView itemsInOrder_textView; | |
public TextView deleteFromOrder_textView; | |
public View itemDescription_view; | |
public NormalViewHolder(View itemView) { | |
super(itemView); | |
description_textView = (TextView) itemView.findViewById(R.id.description_textView); | |
price_textView = (TextView) itemView.findViewById(R.id.price_textView); | |
weight_textView = (TextView) itemView.findViewById(R.id.weight_textView); | |
title_textView = (TextView) itemView.findViewById(R.id.title_textView); | |
addToOrder_textView = (TextView) itemView.findViewById(R.id.addToOrder_textView); | |
deleteFromOrder_textView = (TextView) itemView.findViewById(R.id.deleteFromOrder_textView); | |
itemsInOrder_textView = (TextView) itemView.findViewById(R.id.itemsInOrder_textView); | |
itemDescription_view = itemView.findViewById(R.id.itemDescription_view); | |
} | |
public void bindView(int position) { | |
Product product = productList.get(position); | |
title_textView.setText(product.getTitle()); | |
description_textView.setText(product.getShot_description()); | |
price_textView.setText(context.getString(R.string.price, product.getPrice())); | |
weight_textView.setText(product.getWeight()); | |
deleteFromOrder_textView.setVisibility(View.VISIBLE); | |
Integer count = SharedPreferencesManager_.getInstance_(context).getCountOfProductInOrder(product); | |
itemsInOrder_textView.setText(String.valueOf(count)); | |
itemDescription_view.setOnClickListener(v -> { | |
if (productCallbacks != null) { | |
productCallbacks.seeDetails(product); | |
} | |
}); | |
addToOrder_textView.setOnClickListener(v -> { | |
if (productCallbacks != null) { | |
productCallbacks.addProductToOrder(product); | |
notifyDataSetChanged(); | |
} | |
}); | |
deleteFromOrder_textView.setOnClickListener(v -> { | |
if (productCallbacks != null) { | |
productCallbacks.deleteProductFromOrder(product); | |
notifyDataSetChanged(); | |
} | |
}); | |
} | |
} | |
public class FooterViewHolder extends RecyclerView.ViewHolder { | |
public TextView sum_textView; | |
public TextView makeOrder_textView; | |
public RadioButton courier_delivery; | |
public RadioButton self_delivery; | |
public RadioGroup category_radioGroup; | |
public View buy_textView; | |
public View confirm_imageView; | |
public View couponCredentials_view; | |
public View yourDiscount_view; | |
public EditText coupon_editText; | |
public TextView discount_textView; | |
public FooterViewHolder(View itemView) { | |
super(itemView); | |
sum_textView = (TextView) itemView.findViewById(R.id.sum_textView); | |
makeOrder_textView = (TextView) itemView.findViewById(R.id.makeOrder_textView); | |
courier_delivery = (RadioButton) itemView.findViewById(R.id.courier_delivery); | |
self_delivery = (RadioButton) itemView.findViewById(R.id.self_delivery); | |
category_radioGroup = (RadioGroup) itemView.findViewById(R.id.category_radioGroup); | |
confirm_imageView = itemView.findViewById(R.id.confirm_imageView); | |
couponCredentials_view = itemView.findViewById(R.id.couponCredentials_view); | |
yourDiscount_view = itemView.findViewById(R.id.yourDiscount_view); | |
buy_textView = itemView.findViewById(R.id.buy_textView); | |
coupon_editText = (EditText) itemView.findViewById(R.id.coupon_editText); | |
discount_textView = (TextView) itemView.findViewById(R.id.discount_textView); | |
} | |
public void bindView(int position) { | |
sum_textView.setText(context.getString(R.string.price_sum, orderSum)); | |
if (percentDiscount > 0 && absoluteDiscount > 0) { | |
discount_textView.setText(context.getString(R.string.discount_formatted, 0, 0).replaceAll("_", "%")); | |
} else { | |
discount_textView.setText(context.getString(R.string.discount_formatted, percentDiscount, absoluteDiscount).replaceAll("_", "%")); | |
} | |
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/" + FontManager.FONT_TTF); | |
courier_delivery.setTypeface(font); | |
self_delivery.setTypeface(font); | |
buy_textView.setOnClickListener(v -> { | |
buy_textView.setVisibility(View.GONE); | |
couponCredentials_view.setVisibility(View.VISIBLE); | |
}); | |
confirm_imageView.setOnClickListener(v -> { | |
if (cartCallbacks != null) { | |
try { | |
Integer code = Integer.getInteger(coupon_editText.getText().toString()); | |
cartCallbacks.useDiscount(code); | |
} catch (Exception e) { | |
cartCallbacks.cantUseDiscount(); | |
} | |
} | |
couponCredentials_view.setVisibility(View.GONE); | |
yourDiscount_view.setVisibility(View.VISIBLE); | |
}); | |
makeOrder_textView.setOnClickListener(v -> { | |
if (cartCallbacks != null) { | |
cartCallbacks.makeOrderClick(); | |
} | |
}); | |
if (category_radioGroup != null) { | |
category_radioGroup.setOnCheckedChangeListener((group, checkedId) -> { | |
switch (checkedId) { | |
case R.id.courier_delivery: | |
if (cartCallbacks != null) { | |
cartCallbacks.checkOrder(1); | |
courier_delivery.setTextColor(ContextCompat.getColor(context, R.color.red)); | |
self_delivery.setTextColor(ContextCompat.getColor(context, R.color.white)); | |
} | |
break; | |
case R.id.self_delivery: | |
if (cartCallbacks != null) { | |
cartCallbacks.checkOrder(2); | |
self_delivery.setTextColor(ContextCompat.getColor(context, R.color.red)); | |
courier_delivery.setTextColor(ContextCompat.getColor(context, R.color.white)); | |
} | |
break; | |
} | |
}); | |
} | |
} | |
} | |
public class HeaderViewHolder extends RecyclerView.ViewHolder { | |
public TextView name_textView; | |
public TextView phone_textView; | |
public HeaderViewHolder(View itemView) { | |
super(itemView); | |
name_textView = (TextView) itemView.findViewById(R.id.name_textView); | |
phone_textView = (TextView) itemView.findViewById(R.id.phone_textView); | |
} | |
public void bindView(int position) { | |
name_textView.setText(userInformation.getUsername()); | |
phone_textView.setText(userInformation.getPhone()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment