Advertisement
Guest User

Untitled

a guest
May 4th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.06 KB | None | 0 0
  1. package ru.myapp.test.brands;
  2.  
  3. import android.content.Context;
  4. import android.support.v7.widget.RecyclerView;
  5. import android.text.TextUtils;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9.  
  10. import com.tonicartos.superslim.GridSLM;
  11. import com.tonicartos.superslim.LayoutManager;
  12. import com.tonicartos.superslim.LinearSLM;
  13.  
  14. import ru.myapp.test.R;
  15.  
  16. /**
  17.  * Created by Lucifer on 05.05.2015.
  18.  */
  19. public class RecyclerBrandsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  20.  
  21.  
  22.     private static final int VIEW_TYPE_HEADER = 0x01;
  23.     private static final int VIEW_TYPE_CONTENT = 0x00;
  24.     private static final int LINEAR = 0;
  25.     private BrandModel data;
  26.     private int mHeaderDisplay;
  27.     private boolean mMarginsFixed;
  28.     private Context mContext;
  29.  
  30.     public RecyclerBrandsAdapter(Context context, int headerMode,BrandModel data){
  31.         mContext = context;
  32.         mHeaderDisplay = headerMode;
  33.         this.data=data;
  34.         String lastHeader = "";
  35.         int sectionManager = -1;
  36.         int headerCount = 0;
  37.         int sectionFirstPosition = 0;
  38.         for (int i = 0; i < data.brands.size(); i++) {
  39.             String header = data.brands.get(i).brandswelove_name.substring(0, 1);
  40.             if (!TextUtils.equals(lastHeader, header)) {
  41.                 sectionManager = (sectionManager + 1) % 2;
  42.                 sectionFirstPosition = i + headerCount;
  43.                 lastHeader = header;
  44.                 headerCount += 1;
  45.  
  46.                 data.brands.add(i,new BrandModel.Brand(header,
  47.                         data.brands.get(i).brandswelove_id,
  48.                         data.brands.get(i).loveCurrentCustomer,
  49.                         true,
  50.                         sectionManager,
  51.                         sectionFirstPosition));
  52.  
  53.                 data.brands.get(i+1).isHeader=false;
  54.                 data.brands.get(i+1).sectionManager=sectionManager;
  55.                 data.brands.get(i+1).sectionFirstPosition=sectionFirstPosition;
  56.  
  57.             }else{
  58.                 data.brands.get(i).isHeader=false;
  59.                 data.brands.get(i).sectionManager=sectionManager;
  60.                 data.brands.get(i).sectionFirstPosition=sectionFirstPosition;
  61.             }
  62.  
  63.  
  64.         }
  65.  
  66.     }
  67.  
  68.     @Override
  69.     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  70.         View view;
  71.         if (viewType == VIEW_TYPE_HEADER) {
  72.             view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
  73.             return new BrandsViewHolderHeader(view);
  74.         } else {
  75.             view = LayoutInflater.from(parent.getContext()).inflate(R.layout.brands_item, parent, false);
  76.             return new BrandViewHolder(view);
  77.         }
  78.  
  79.     }
  80.  
  81.     @Override
  82.     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  83.  
  84.         final BrandModel.Brand item = data.brands.get(position);
  85.         final View itemView = holder.itemView;
  86.  
  87.         if (holder instanceof BrandViewHolder) {
  88.             BrandViewHolder viewHolder=(BrandViewHolder) holder;
  89.             if(item.loveCurrentCustomer==null){
  90.  
  91.                 viewHolder.bindItem(item.brandswelove_name, R.drawable.dislike);
  92.  
  93.             }else{
  94.                 viewHolder.bindItem(item.brandswelove_name,R.drawable.like);
  95.             }
  96.         }else if(holder instanceof BrandsViewHolderHeader){
  97.             BrandsViewHolderHeader viewHolder=(BrandsViewHolderHeader) holder;
  98.             viewHolder.bindItem(item.brandswelove_name);
  99.         }
  100.  
  101.  
  102.         GridSLM.LayoutParams lp = GridSLM.LayoutParams.from(itemView.getLayoutParams());
  103.         // Overrides xml attrs, could use different layouts too.
  104.         if (item.isHeader) {
  105.             lp.headerDisplay = mHeaderDisplay;
  106.             if (lp.isHeaderInline() || (mMarginsFixed && !lp.isHeaderOverlay())) {
  107.                 lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
  108.             } else {
  109.                 lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
  110.             }
  111.  
  112.             lp.headerEndMarginIsAuto = !mMarginsFixed;
  113.             lp.headerStartMarginIsAuto = !mMarginsFixed;
  114.         }
  115.         lp.setSlm(LinearSLM.ID);
  116.         lp.setFirstPosition(item.sectionFirstPosition);
  117.         itemView.setLayoutParams(lp);
  118.     }
  119.  
  120.     @Override
  121.     public int getItemCount() {
  122.         return data.brands.size();
  123.     }
  124.  
  125. /*
  126.     public void setHeaderDisplay(int headerDisplay) {
  127.         mHeaderDisplay = headerDisplay;
  128.   //      notifyHeaderChanges();
  129.     }
  130.  
  131.     public void setMarginsFixed(boolean marginsFixed) {
  132.         mMarginsFixed = marginsFixed;
  133.         notifyHeaderChanges();
  134.     }
  135.  
  136.     private void notifyHeaderChanges() {
  137.         for (int i = 0; i < data.brands.size(); i++) {
  138.             BrandModel.Brand item = data.brands.get(i);
  139.             if (item.isHeader) {
  140.                 notifyItemChanged(i);
  141.             }
  142.         }
  143.     }
  144. */
  145.     @Override
  146.     public int getItemViewType(int position) {
  147.         return data.brands.get(position).isHeader ? VIEW_TYPE_HEADER : VIEW_TYPE_CONTENT;
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement