Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. public class FlatListAdapter extends RecyclerView.Adapter<FlatListAdapter.ViewHolder> {
  2. private ArrayList<FlatItemModel> flats;
  3. private Context context;
  4.  
  5. private SparseBooleanArray likedItems;
  6.  
  7. private int DP_FRIEND_AVATAR_SIZE;
  8. private int DP_FRIEND_AVATAR_MARGIN;
  9.  
  10. public FlatListAdapter(Context context)
  11. {
  12. this.flats = new ArrayList<>();
  13. this.likedItems = new SparseBooleanArray();
  14. this.context = context;
  15.  
  16. DP_FRIEND_AVATAR_SIZE = (int)context.getResources().getDimension(R.dimen.friendsAvatarDiameter);
  17. DP_FRIEND_AVATAR_MARGIN = (int)context.getResources().getDimension(R.dimen.indicatorMargin);
  18. }
  19.  
  20. @Override
  21. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  22. View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.flat, parent, false);
  23. itemView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  24.  
  25. ViewHolder holder = new ViewHolder(itemView);
  26.  
  27. return holder;
  28. }
  29.  
  30. @Override
  31. public void onBindViewHolder(ViewHolder holder, int position) {
  32. FlatItemModel flat = flats.get(position);
  33. if(likedItems.get(position))
  34. {
  35. holder.likeButton.setBackground(context.getResources().getDrawable(R.drawable.star_pressed));
  36. }
  37. else
  38. {
  39. holder.likeButton.setBackground(context.getResources().getDrawable(R.drawable.star_unpressed));
  40. }
  41. Glide.with(context).load(flat.getImageUrl()).into(holder.image);
  42. holder.address.setText(flat.getAddress());
  43. holder.metres.setText(flat.getMetres() + context.getResources().getString(R.string.m_square));
  44. holder.stage.setText(flat.getStage());
  45. holder.rooms.setText(flat.getRooms());
  46. holder.subwayStation.setText(flat.getSubwayStation());
  47. holder.timeToSubwayStation.setText(flat.getTimeToSubwayStation());
  48. holder.price.setText(flat.getPrice());
  49. holder.friendsAvatarHost.removeAllViewsInLayout();
  50. if(flat.getFriendPhotoUrls() != null) {
  51. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int)convertDpToPixel(DP_FRIEND_AVATAR_SIZE,
  52. context), (int)convertDpToPixel(DP_FRIEND_AVATAR_SIZE, context));
  53. params.setMarginEnd((int) convertDpToPixel(DP_FRIEND_AVATAR_MARGIN, context));
  54. if(holder.friendsAvatarHost.getChildAt(0) == null) {
  55. for (int i = 0; i < 3 & i < flat.getFriendPhotoUrls().size(); i++) {
  56. CircleImageView image = new CircleImageView(context);
  57. Glide.with(context).load(flat.getFriendPhotoUrls().get(i)).into(image);
  58. image.setLayoutParams(params);
  59. holder.friendsAvatarHost.addView(image);
  60. }
  61. if (flat.getFriendPhotoUrls().size() > 3) {
  62. TextView extraFriends = new TextView(context);
  63. extraFriends.setLayoutParams(params);
  64. extraFriends.setText("+" + (flat.getFriendPhotoUrls().size() - 3));
  65. extraFriends.setBackground(context.getResources().getDrawable(R.drawable.round_friends_indicator));
  66. extraFriends.setTextColor(holder.address.getTextColors());
  67. extraFriends.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
  68. extraFriends.setPadding(0, (int) convertDpToPixel(6, context), 0, 0);
  69. holder.friendsAvatarHost.addView(extraFriends);
  70. }
  71. }
  72. }
  73.  
  74. }
  75.  
  76. @Override
  77. public long getItemId(int position)
  78. {
  79. return flats.get(position).getId();
  80. }
  81.  
  82. public static float convertDpToPixel(float dp, Context context){
  83. Resources resources = context.getResources();
  84. DisplayMetrics metrics = resources.getDisplayMetrics();
  85. float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
  86. return px;
  87. }
  88.  
  89. @Override
  90. public int getItemCount() {
  91. return flats.size();
  92. }
  93.  
  94. public void addItem(FlatItemModel flat)
  95. {
  96. this.flats.add(flat);
  97. Log.d("addItem", "position: " + (getItemCount() - 1));
  98. likedItems.append(this.getItemCount() - 1, flat.isFavorite());
  99. }
  100.  
  101. class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
  102. {
  103. ImageView image;
  104. Button likeButton;
  105. TextView address, metres, stage, rooms, subwayStation, timeToSubwayStation, price;
  106. LinearLayout friendsAvatarHost;
  107. public ViewHolder(View itemView) {
  108. super(itemView);
  109. image = (ImageView) itemView.findViewById(R.id.flat_image);
  110.  
  111. address = (TextView) itemView.findViewById(R.id.flat_address);
  112. metres = (TextView) itemView.findViewById(R.id.flat_metres);
  113. stage = (TextView) itemView.findViewById(R.id.flat_stage);
  114. rooms = (TextView) itemView.findViewById(R.id.flat_rooms);
  115. subwayStation = (TextView) itemView.findViewById(R.id.flat_subway_station);
  116. timeToSubwayStation = (TextView) itemView.findViewById(R.id.flat_time_to_subway_station);
  117. price = (TextView) itemView.findViewById(R.id.flat_price);
  118.  
  119. friendsAvatarHost = (LinearLayout) itemView.findViewById(R.id.flat_friends_images_host);
  120.  
  121. likeButton = (Button) itemView.findViewById(R.id.flat_like_button);
  122. likeButton.setOnClickListener(this);
  123. itemView.setOnClickListener(this);
  124. }
  125.  
  126. @Override
  127. public void onClick(View v) {
  128. if(v instanceof Button)
  129. {
  130. if(!likedItems.get(getLayoutPosition()))
  131. {
  132. likeButton.setBackground(context.getResources().getDrawable(R.drawable.star_pressed));
  133. flats.get(getAdapterPosition()).setIsFavorite(true);
  134. likedItems.delete(getLayoutPosition());
  135. likedItems.append(getLayoutPosition(), true);
  136. }
  137. else
  138. {
  139. likeButton.setBackground(context.getResources().getDrawable(R.drawable.star_unpressed));
  140. flats.get(getAdapterPosition()).setIsFavorite(false);
  141. likedItems.delete(getLayoutPosition());
  142. likedItems.append(getLayoutPosition(), false);
  143. }
  144. }
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement