content: package com.cy.cyflowlayoutlibrary;
  
  
  import android.graphics.Bitmap;
  import android.util.SparseArray;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.widget.EditText;
  import android.widget.ImageView;
  import android.widget.ProgressBar;
  import android.widget.TextView;
  
  import java.util.Collection;
  import java.util.List;
  
  /**
   * Created by cy on 2018/2/8.
   */
  
  public abstract class FlowLayoutAdapter<T> {
      private OnDataSetChangedListener onDataSetChangedListener;
      private List<T> list_bean;
  
      public FlowLayoutAdapter( List<T> list_bean) {
          this.list_bean = list_bean;
      }
  
      public int getCount() {
          return list_bean.size();
      }
  
      public  View getView(FlowLayout parent, final int position) {
  
          View view = LayoutInflater.from(parent.getContext()).inflate(getItemLayoutID(position, list_bean.get(position)), parent, false);
  
          view.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  onItemClick(position, list_bean.get(position));
              }
          });
          view.setOnLongClickListener(new View.OnLongClickListener() {
              @Override
              public boolean onLongClick(View v) {
                  onItemLongClick(position,list_bean.get(position));
                  return true;
              }
          });
          bindDataToView(new ViewHolder(view), position, list_bean.get(position));
          return view;
  
      }
  
  
      //填充数据
      public abstract void bindDataToView(ViewHolder holder, int position, T bean);
       /*
        ItemView的单击事件
  
        @param position
       */
  
      public abstract void onItemClick(int position, T bean);
      public  void onItemLongClick(int position, T bean){}
  
      /*
          取得ItemView的布局文件
          @return
         */
      public abstract int getItemLayoutID(int position, T bean);
  
  
      public void remove(int position) {
          list_bean.remove(position);
          notifyDataSetChanged();
      }
  
      public void add(T bean) {
          list_bean.add(bean);
  
          notifyDataSetChanged();
      }
      public void notifyDataSetChanged(){
          if(onDataSetChangedListener!=null){
  
              onDataSetChangedListener.onDataSetChanged();
          }
      }
  
      public void addNoNotify(T bean) {
          list_bean.add(bean);
      }
  
      public void addToHead(T bean) {
          list_bean.add(0, bean);
      }
  
      public int addAll(List<T> beans) {
          list_bean.addAll(beans);
  
          notifyDataSetChanged();
  
          return beans.size();
      }
  
      public void addAll(Collection<T> c) {
          list_bean.addAll(c);
          notifyDataSetChanged();
  
  
      }
  
      public int clearAddAll(List<T> beans) {
          list_bean.clear();
          list_bean.addAll(beans);
  
          notifyDataSetChanged();
  
          return beans.size();
      }
  
      public void addAllToHead(List<T> beans) {
          list_bean.addAll(0, beans);
          notifyDataSetChanged();
  
      }
  
      public void clear() {
          list_bean.clear();
          notifyDataSetChanged();
  
      }
  
      public void setOnDataSetChangedListener(OnDataSetChangedListener onDataSetChangedListener) {
          this.onDataSetChangedListener = onDataSetChangedListener;
      }
  
      public interface OnDataSetChangedListener{
          public void onDataSetChanged();
      }
      public static class ViewHolder {
          private View itemView;
          private SparseArray<View> array_view;
  
          public ViewHolder(View itemView) {
              this.itemView = itemView;
  
              array_view = new SparseArray<View>();
  
          }
  
  
          public <T extends View> T getView(int viewId) {
  
              View view = array_view.get(viewId);
              if (view == null) {
                  view = itemView.findViewById(viewId);
                  array_view.put(viewId, view);
              }
              return (T) view;
          }
  
  
  
  
  
          public ViewHolder setVisible(int res_id) {
              getView(res_id).setVisibility(View.VISIBLE);
              return this;
          }
  
          public ViewHolder setInVisible(int res_id) {
              getView(res_id).setVisibility(View.INVISIBLE);
              return this;
          }
  
          public void setViewGone(int res_id) {
              getView(res_id).setVisibility(View.GONE);
          }
  
          public void setViewVisible(int res_id) {
              getView(res_id).setVisibility(View.VISIBLE);
          }
  
  
          public void setText(int tv_id, String text) {
              TextView tv = getView(tv_id);
  
  
              tv.setText(nullToString(text));
          }
  
          public String nullToString(Object object) {
              return object == null ? "" : object.toString();
          }
  
          public void setPriceText(int tv_id, String text) {
              TextView tv = getView(tv_id);
  
              tv.setText("¥" + text);
          }
  
          public void setCountText(int tv_id, String text) {
              TextView tv = getView(tv_id);
  
              tv.setText("x" + text);
          }
  
          public void setCountText(int tv_id, int text) {
              TextView tv = getView(tv_id);
  
              tv.setText("x" + text);
          }
  
          public void setPriceText(int tv_id, int text) {
              TextView tv = getView(tv_id);
  
              tv.setText("¥" + text);
          }
  
          public void setPriceText(int tv_id, float text) {
              TextView tv = getView(tv_id);
  
              tv.setText("¥" + text);
          }
  
          public void setText(int tv_id, int text) {
              TextView tv = getView(tv_id);
              tv.setText(String.valueOf(nullToString(text)));
          }
  
          public void setTextColor(int tv_id, int color) {
              TextView tv = getView(tv_id);
              tv.setTextColor(color);
          }
  
          public String getTVText(int tv_id) {
              TextView tv = getView(tv_id);
              return tv.getText().toString().trim();
          }
  
          public String getETText(int tv_id) {
              EditText tv = getView(tv_id);
              return tv.getText().toString().trim();
          }
  
          public void setBackgroundResource(int v_id, int resid) {
              View view = getView(v_id);
              view.setBackgroundResource(resid);
          }
  
          public void setImageBitmap(int v_id, Bitmap bitmap) {
              ImageView view = getView(v_id);
              view.setImageBitmap(bitmap);
          }
  
          public void setImageResource(int v_id, int resID) {
              ImageView view = getView(v_id);
              view.setImageResource(resID);
          }
  
          public void setProgress(int progress_id, int progress) {
              ProgressBar progressBar = getView(progress_id);
              progressBar.setProgress(progress);
  
          }
  
          public void setOnClickListener(int res_id, View.OnClickListener onClickListener) {
              getView(res_id).setOnClickListener(onClickListener);
          }
  
          public void setOnLongClickListener(int res_id, View.OnLongClickListener onLongClickListener) {
              getView(res_id).setOnLongClickListener(onLongClickListener);
          }
      }
  
  
  }
  