Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FlatListAdapter extends RecyclerView.Adapter<FlatListAdapter.ViewHolder> {
- private ArrayList<FlatItemModel> flats;
- private Context context;
- private SparseBooleanArray likedItems;
- private int DP_FRIEND_AVATAR_SIZE;
- private int DP_FRIEND_AVATAR_MARGIN;
- public FlatListAdapter(Context context)
- {
- this.flats = new ArrayList<>();
- this.likedItems = new SparseBooleanArray();
- this.context = context;
- DP_FRIEND_AVATAR_SIZE = (int)context.getResources().getDimension(R.dimen.friendsAvatarDiameter);
- DP_FRIEND_AVATAR_MARGIN = (int)context.getResources().getDimension(R.dimen.indicatorMargin);
- }
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.flat, parent, false);
- itemView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
- ViewHolder holder = new ViewHolder(itemView);
- return holder;
- }
- @Override
- public void onBindViewHolder(ViewHolder holder, int position) {
- FlatItemModel flat = flats.get(position);
- if(likedItems.get(position))
- {
- holder.likeButton.setBackground(context.getResources().getDrawable(R.drawable.star_pressed));
- }
- else
- {
- holder.likeButton.setBackground(context.getResources().getDrawable(R.drawable.star_unpressed));
- }
- Glide.with(context).load(flat.getImageUrl()).into(holder.image);
- holder.address.setText(flat.getAddress());
- holder.metres.setText(flat.getMetres() + context.getResources().getString(R.string.m_square));
- holder.stage.setText(flat.getStage());
- holder.rooms.setText(flat.getRooms());
- holder.subwayStation.setText(flat.getSubwayStation());
- holder.timeToSubwayStation.setText(flat.getTimeToSubwayStation());
- holder.price.setText(flat.getPrice());
- holder.friendsAvatarHost.removeAllViewsInLayout();
- if(flat.getFriendPhotoUrls() != null) {
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int)convertDpToPixel(DP_FRIEND_AVATAR_SIZE,
- context), (int)convertDpToPixel(DP_FRIEND_AVATAR_SIZE, context));
- params.setMarginEnd((int) convertDpToPixel(DP_FRIEND_AVATAR_MARGIN, context));
- if(holder.friendsAvatarHost.getChildAt(0) == null) {
- for (int i = 0; i < 3 & i < flat.getFriendPhotoUrls().size(); i++) {
- CircleImageView image = new CircleImageView(context);
- Glide.with(context).load(flat.getFriendPhotoUrls().get(i)).into(image);
- image.setLayoutParams(params);
- holder.friendsAvatarHost.addView(image);
- }
- if (flat.getFriendPhotoUrls().size() > 3) {
- TextView extraFriends = new TextView(context);
- extraFriends.setLayoutParams(params);
- extraFriends.setText("+" + (flat.getFriendPhotoUrls().size() - 3));
- extraFriends.setBackground(context.getResources().getDrawable(R.drawable.round_friends_indicator));
- extraFriends.setTextColor(holder.address.getTextColors());
- extraFriends.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
- extraFriends.setPadding(0, (int) convertDpToPixel(6, context), 0, 0);
- holder.friendsAvatarHost.addView(extraFriends);
- }
- }
- }
- }
- @Override
- public long getItemId(int position)
- {
- return flats.get(position).getId();
- }
- public static float convertDpToPixel(float dp, Context context){
- Resources resources = context.getResources();
- DisplayMetrics metrics = resources.getDisplayMetrics();
- float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
- return px;
- }
- @Override
- public int getItemCount() {
- return flats.size();
- }
- public void addItem(FlatItemModel flat)
- {
- this.flats.add(flat);
- Log.d("addItem", "position: " + (getItemCount() - 1));
- likedItems.append(this.getItemCount() - 1, flat.isFavorite());
- }
- class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
- {
- ImageView image;
- Button likeButton;
- TextView address, metres, stage, rooms, subwayStation, timeToSubwayStation, price;
- LinearLayout friendsAvatarHost;
- public ViewHolder(View itemView) {
- super(itemView);
- image = (ImageView) itemView.findViewById(R.id.flat_image);
- address = (TextView) itemView.findViewById(R.id.flat_address);
- metres = (TextView) itemView.findViewById(R.id.flat_metres);
- stage = (TextView) itemView.findViewById(R.id.flat_stage);
- rooms = (TextView) itemView.findViewById(R.id.flat_rooms);
- subwayStation = (TextView) itemView.findViewById(R.id.flat_subway_station);
- timeToSubwayStation = (TextView) itemView.findViewById(R.id.flat_time_to_subway_station);
- price = (TextView) itemView.findViewById(R.id.flat_price);
- friendsAvatarHost = (LinearLayout) itemView.findViewById(R.id.flat_friends_images_host);
- likeButton = (Button) itemView.findViewById(R.id.flat_like_button);
- likeButton.setOnClickListener(this);
- itemView.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- if(v instanceof Button)
- {
- if(!likedItems.get(getLayoutPosition()))
- {
- likeButton.setBackground(context.getResources().getDrawable(R.drawable.star_pressed));
- flats.get(getAdapterPosition()).setIsFavorite(true);
- likedItems.delete(getLayoutPosition());
- likedItems.append(getLayoutPosition(), true);
- }
- else
- {
- likeButton.setBackground(context.getResources().getDrawable(R.drawable.star_unpressed));
- flats.get(getAdapterPosition()).setIsFavorite(false);
- likedItems.delete(getLayoutPosition());
- likedItems.append(getLayoutPosition(), false);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement