DeviceModelRecyclerViewAdapter.java 3.4 KB

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