我有一个模拟Facebook提要的代码
我想添加评论选项以打开一个新的列表视图,其中每个帖子包含多个评论
主列表视图是由Adapter定义的,我如何以一种方式来做论文,即单击评论按钮将为我真正打开每个帖子中的不同评论的列表,或者如果我希望保留某些帖子,则不显示任何评论未回答?
这是我的适配器
import android.R.layout;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class FacebookWallAdapter extends ArrayAdapter {
private List<FacebookWallPost> posts;
private List<String> comments;
private ListView commentsListView;
private Context activity;
private int postsLayoutId;
public FacebookWallAdapter(@NonNull Context context, int resource, @NonNull List objects) {
super(context, resource, objects);
this.posts = objects;
this.activity = context;
this.postsLayoutId = resource;
}
@SuppressLint("ResourceType")
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(this.activity);
convertView = inflater.inflate(this.postsLayoutId,parent,false);
TextView writerName = convertView.findViewById(R.id.writerName);
TextView date = convertView.findViewById(R.id.date);
TextView postText = convertView.findViewById(R.id.postText);
ImageView profilePic = convertView.findViewById(R.id.profilePicture);
ImageView postImage = convertView.findViewById(R.id.postImage);
ImageView likeButtonStatus = convertView.findViewById(R.id.likeButton);
ImageView commentsButton = convertView.findViewById(R.id.commentButton);
FacebookWallPost thisPost = this.posts.get(position);
writerName.setText(thisPost.getPostWriter());
date.setText(thisPost.getDate());
postText.setText(thisPost.getPostText());
profilePic.setImageResource(thisPost.getProfilePic());
postImage.setImageResource(thisPost.getPostImage());
if(thisPost.getLikeStatus()){
likeButtonStatus.setImageResource(R.drawable.like_blue);
}
else
{ likeButtonStatus.setImageResource(R.drawable.like_black);
}
likeButtonStatus.setOnClickListener((View v) -> {
if (thisPost.getLikeStatus())
{
thisPost.setLikeStatus(false);
}
else {
thisPost.setLikeStatus(true);
}
this.notifyDataSetChanged();
});
return convertView;
}
}
我试图添加另一个名为listName的列表视图
和评论列表:列出评论
但是我无法正确定义适配器,该怎么办?
最佳答案
您可以看一下可展开的列表视图:
https://developer.android.com/reference/android/widget/ExpandableListView
这使您可以将列表视图包含在其他列表视图中。
关于java - ListView适配器内的ListView Android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62367359/