DeviceModelHomeRecyclerViewAdapter.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.Intelligent.FamilyU.adapter;
  2. import android.content.Context;
  3. import android.support.v7.widget.RecyclerView;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.ImageView;
  8. import android.widget.LinearLayout;
  9. import android.widget.TextView;
  10. import com.Intelligent.FamilyU.R;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. public class DeviceModelHomeRecyclerViewAdapter extends RecyclerView.Adapter<DeviceModelHomeRecyclerViewAdapter.ViewHolder> {
  14. private List<HashMap<String, String>> mlist;
  15. private Context context;
  16. private OnItemClickListener mOnItemClickListener;
  17. public static interface OnItemClickListener {
  18. void onItemClick(int position);
  19. void onItemLongClick(int position);
  20. }
  21. public DeviceModelHomeRecyclerViewAdapter(Context context, List<HashMap<String, String>> list, OnItemClickListener onItemClickListener) {
  22. super();
  23. mlist = list;
  24. this.context = context;
  25. mOnItemClickListener = onItemClickListener;
  26. }
  27. public static class ViewHolder extends RecyclerView.ViewHolder {
  28. // each data item is just a string in this case
  29. public TextView name;
  30. public ImageView imv;
  31. public ImageView nameIv;
  32. public LinearLayout ll;
  33. public ViewHolder(View v) {
  34. super(v);
  35. }
  36. }
  37. // Create new views (invoked by the layout manager)
  38. @Override
  39. public DeviceModelHomeRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
  40. int viewType) {
  41. // create a new view
  42. View convertView = LayoutInflater.from(parent.getContext())
  43. .inflate(R.layout.listview_add_device_home_item, parent, false);
  44. ViewHolder viewHolder = new ViewHolder(convertView);
  45. viewHolder.name = (TextView) convertView.findViewById(R.id.name_tv);
  46. viewHolder.imv = (ImageView) convertView.findViewById(R.id.iv);
  47. viewHolder.nameIv = (ImageView) convertView.findViewById(R.id.name_iv);
  48. viewHolder.ll = (LinearLayout) convertView.findViewById(R.id.ll);
  49. return viewHolder;
  50. }
  51. // Replace the contents of a view (invoked by the layout manager)
  52. @Override
  53. public void onBindViewHolder(ViewHolder holder, final int position) {
  54. // - get element from your dataset at this position
  55. // - replace the contents of the view with that element
  56. HashMap<String, String> map = mlist.get(position);
  57. String name = map.get("name");
  58. holder.name.setText(name);
  59. if ("add".equals(name)) {
  60. holder.name.setVisibility(View.GONE);
  61. holder.nameIv.setVisibility(View.GONE);
  62. holder.imv.setImageResource(R.mipmap.dlna_icon_member_add);
  63. } else {
  64. holder.name.setVisibility(View.VISIBLE);
  65. holder.nameIv.setVisibility(View.VISIBLE);
  66. holder.imv.setImageResource(R.mipmap.icon_big_study);
  67. }
  68. holder.ll.setOnClickListener(new View.OnClickListener() {
  69. @Override
  70. public void onClick(View v) {
  71. mOnItemClickListener.onItemClick(position);
  72. }
  73. });
  74. holder.ll.setOnLongClickListener(new View.OnLongClickListener() {
  75. @Override
  76. public boolean onLongClick(View v) {
  77. mOnItemClickListener.onItemLongClick(position);
  78. return true;
  79. }
  80. });
  81. }
  82. // Return the size of your dataset (invoked by the layout manager)
  83. @Override
  84. public int getItemCount() {
  85. return mlist.size();
  86. }
  87. public void refreshData(List<HashMap<String, String>> list) {
  88. if (null != mlist) {
  89. mlist.clear();
  90. }
  91. mlist.addAll(list);
  92. notifyDataSetChanged();
  93. }
  94. public List<HashMap<String, String>> getAllList() {
  95. return mlist;
  96. }
  97. // 添加数据
  98. public void addData(HashMap<String, String> map, int position) {
  99. mlist.add(map);
  100. //添加动画
  101. notifyItemInserted(position);
  102. }
  103. // 删除数据
  104. public void removeData(int position) {
  105. mlist.remove(position);
  106. //删除动画
  107. notifyItemRemoved(position);
  108. notifyDataSetChanged();
  109. }
  110. public void addBtn() {
  111. if (null == mlist || mlist.size() == 0) {
  112. return;
  113. }
  114. // if ("add".equals(mlist.get(mlist.size() - 1).get("name"))) {
  115. // mlist.remove(mlist.size() - 1);
  116. // }
  117. HashMap<String, String> map = new HashMap<String, String>();
  118. map.put("name", "add");
  119. mlist.add(map);
  120. notifyItemInserted(mlist.size()-1);
  121. // List<HashMap<String, String>> list = new ArrayList<>();
  122. // list.addAll(mlist);
  123. // refreshData(list);
  124. }
  125. }