第二阶段冲刺3
查看自己的朋友圈
部分代码
// 获取当前用户的朋友圈数据
ArrayList<FriendCircleItem> myFriendCircle = getUserFriendCircle(currentUserId);
// 在Activity或Fragment中展示朋友圈数据
RecyclerView recyclerView = findViewById(R.id.friend_circle_recycler_view);
FriendCircleAdapter adapter = new FriendCircleAdapter(myFriendCircle);
recyclerView.setAdapter(adapter);
// FriendCircleAdapter
public class FriendCircleAdapter extends RecyclerView.Adapter<FriendCircleViewHolder> {
private ArrayList<FriendCircleItem> friendCircleItems;
public FriendCircleAdapter(ArrayList<FriendCircleItem> items) {
this.friendCircleItems = items;
}
@NonNull
@Override
public FriendCircleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_friend_circle, parent, false);
return new FriendCircleViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull FriendCircleViewHolder holder, int position) {
FriendCircleItem item = friendCircleItems.get(position);
holder.bind(item);
}
@Override
public int getItemCount() {
return friendCircleItems.size();
}
}
// FriendCircleViewHolder
public class FriendCircleViewHolder extends RecyclerView.ViewHolder {
private TextView userNameTextView;
private ImageView userAvatarImageView;
private TextView postContentTextView;
private TextView likeCountTextView;
private TextView commentCountTextView;
public FriendCircleViewHolder(@NonNull View itemView) {
super(itemView);
userNameTextView = itemView.findViewById(R.id.user_name);
userAvatarImageView = itemView.findViewById(R.id.user_avatar);
postContentTextView = itemView.findViewById(R.id.post_content);
likeCountTextView = itemView.findViewById(R.id.like_count);
commentCountTextView = itemView.findViewById(R.id.comment_count);
}
public void bind(FriendCircleItem item) {
userNameTextView.setText(item.getUserName());
Glide.with(itemView.getContext()).load(item.getUserAvatarUrl()).into(userAvatarImageView);
postContentTextView.setText(item.getPostContent());
likeCountTextView.setText(String.valueOf(item.getLikeCount()));
commentCountTextView.setText(String.valueOf(item.getCommentCount()));
}
}