DeviceListRecyclerViewAdapter.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 DeviceListRecyclerViewAdapter extends RecyclerView.Adapter<DeviceListRecyclerViewAdapter.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 DeviceListRecyclerViewAdapter(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 imv1;
  31. public TextView model;
  32. public ImageView imv2;
  33. public LinearLayout ll;
  34. public ViewHolder(View v) {
  35. super(v);
  36. }
  37. }
  38. // Create new views (invoked by the layout manager)
  39. @Override
  40. public DeviceListRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
  41. int viewType) {
  42. // create a new view
  43. View convertView = LayoutInflater.from(parent.getContext())
  44. .inflate(R.layout.listview_add_device_list_item, parent, false);
  45. ViewHolder viewHolder = new ViewHolder(convertView);
  46. viewHolder.name = (TextView) convertView.findViewById(R.id.name);
  47. viewHolder.imv1 = (ImageView) convertView.findViewById(R.id.iv1);
  48. viewHolder.model = (TextView) convertView.findViewById(R.id.model);
  49. viewHolder.imv2 = (ImageView) convertView.findViewById(R.id.iv2);
  50. viewHolder.ll = (LinearLayout) convertView.findViewById(R.id.ll);
  51. return viewHolder;
  52. }
  53. // Replace the contents of a view (invoked by the layout manager)
  54. @Override
  55. public void onBindViewHolder(ViewHolder holder, final int position) {
  56. // - get element from your dataset at this position
  57. // - replace the contents of the view with that element
  58. HashMap<String, String> map = mlist.get(position);
  59. holder.name.setText(map.get("name"));
  60. holder.model.setText(map.get("model"));
  61. holder.ll.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View v) {
  64. mOnItemClickListener.onItemClick(position);
  65. }
  66. });
  67. holder.ll.setOnLongClickListener(new View.OnLongClickListener() {
  68. @Override
  69. public boolean onLongClick(View v) {
  70. mOnItemClickListener.onItemLongClick(position);
  71. return true;
  72. }
  73. });
  74. }
  75. // Return the size of your dataset (invoked by the layout manager)
  76. @Override
  77. public int getItemCount() {
  78. return mlist.size();
  79. }
  80. public void refreshData(List<HashMap<String, String>> list) {
  81. if (null != mlist) {
  82. mlist.clear();
  83. }
  84. mlist.addAll(list);
  85. notifyDataSetChanged();
  86. }
  87. public List<HashMap<String, String>> getAllList() {
  88. return mlist;
  89. }
  90. }