第二阶段冲刺10
留言点赞功能实现
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Header部分,显示用户信息、头像等 -->
<LinearLayout
android:id="@+id/userInfoLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="@+id/avatarImageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/default_avatar" />
<TextView
android:id="@+id/usernameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="18sp"
android:textColor="@android:color/black"
android:layout_marginStart="16dp"
android:gravity="center_vertical" />
</LinearLayout>
<!-- 内容部分,显示朋友圈文字内容 -->
<TextView
android:id="@+id/contentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/userInfoLayout"
android:padding="16dp"
android:text="这里是朋友圈内容"
android:textSize="16sp" />
<!-- 操作区域,包括点赞和留言 -->
<LinearLayout
android:id="@+id/actionLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/contentTextView"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="16dp">
<Button
android:id="@+id/likeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点赞"
android:textSize="14sp"
android:drawableStart="@drawable/ic_like"
android:drawablePadding="8dp"
android:paddingStart="8dp"
android:paddingEnd="8dp" />
<Button
android:id="@+id/commentButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="评论"
android:textSize="14sp"
android:drawableStart="@drawable/ic_comment"
android:drawablePadding="8dp"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:layout_marginStart="16dp" />
</LinearLayout>
activity
<!-- 留言列表 -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/commentsRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/actionLayout"
android:padding="16dp"/>
</RelativeLayout>
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class PostDetailActivity extends AppCompatActivity {
private ImageView avatarImageView;
private TextView usernameTextView;
private TextView contentTextView;
private Button likeButton;
private Button commentButton;
private RecyclerView commentsRecyclerView;
private CommentAdapter commentAdapter;
private List<Comment> commentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_detail);
// 初始化界面组件
avatarImageView = findViewById(R.id.avatarImageView);
usernameTextView = findViewById(R.id.usernameTextView);
contentTextView = findViewById(R.id.contentTextView);
likeButton = findViewById(R.id.likeButton);
commentButton = findViewById(R.id.commentButton);
commentsRecyclerView = findViewById(R.id.commentsRecyclerView);
// 模拟数据
User user = new User("用户名", R.drawable.default_avatar);
String content = "这里是朋友圈内容";
List<Comment> comments = generateDummyComments();
// 设置用户信息和内容
avatarImageView.setImageResource(user.getAvatar());
usernameTextView.setText(user.getUsername());
contentTextView.setText(content);
// 设置点赞按钮点击事件
likeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 处理点赞逻辑,可以在这里改变点赞状态或者更新点赞数等
}
});
// 设置评论按钮点击事件
commentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 进入评论编辑界面或者弹出评论框等
}
});
// 设置留言列表
commentAdapter = new CommentAdapter(comments);
commentsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
commentsRecyclerView.setAdapter(commentAdapter);
}
// 模拟生成留言数据
private List<Comment> generateDummyComments() {
List<Comment> comments = new ArrayList<>();
comments.add(new Comment("用户A", "评论内容A"));
comments.add(new Comment("用户B", "评论内容B"));
// 添加更多的评论数据
return comments;
}
}