【发布时间】:2015-03-05 18:47:40
【问题描述】:
我正在尝试创建嵌套 cmets 系统,但遇到了问题。我有一个主 ListView 来显示所有顶级 cmets,每个评论 XML 都包含一个带有调整填充的子 ListView。嵌套的 cmets 加载,但每个父评论只加载第一个返回的评论。我已经进行了一些调试,看起来好像所有数据都被发送到 ListView ArrayAdapter,但 getView() 仅针对第一项运行。是否可以强制重新加载 ListView,还是有更好的方法来做我正在尝试的事情?
以下是我当前的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-20dp"
android:layout_marginRight="-20dp"
android:background="#ff2b2b2b"
android:orientation="vertical"
android:padding="20dp"
android:paddingBottom="0dp"
android:paddingEnd="0dp"
android:scrollbars="none">
<TextView
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ccrama"
android:textSize="12dp" />
<TextView
android:id="@+id/commentLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="COMMENT"
android:textSize="14dp" />
<ListView
android:id="@+id/commentsListUnder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-20dp"
android:layout_marginLeft="-5dp"
android:layout_marginRight="-20dp"
android:background="#ff2b2b2b"
android:orientation="vertical"
android:scrollbars="none" />
我的 ArrayAdapter 看起来像这样
public class CommentAdapter extends ArrayAdapter<CommentNode> {
public CommentAdapter(Context context, ArrayList<CommentNode> users) {
super(context, 0, users);
main = context;
this.users = users;
}
ArrayList<CommentNode> users;
Context main;
CommentNode user;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
user = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.comment, parent, false);
}
TextView author = (TextView) convertView.findViewById(R.id.author);
TextView comm = (TextView) convertView.findViewById(R.id.commentLine);
TextView upvote = (TextView) convertView.findViewById(R.id.upvotePost);
author.setText(user.getComment().getAuthor());
comm.setText(user.getComment().getBody());
upvote.setText(user.getComment().getScore() + "");
ArrayList<CommentNode> comments = new ArrayList<CommentNode>();
CommentNode comment = user;
for (CommentNode node : comment.walkTree(TraversalMethod.BREADTH_FIRST)) {
if (node.getParent().getComment().getId() == comment.getComment().getId()) {
comments.add(node);
}
}
//The above method IS returning all the correctly leveled comments
CommentAdapter adapter = new CommentAdapter(main, comments);
ListView listView = (ListView) convertView.findViewById(R.id.commentsListUnder);
listView.setAdapter(adapter);
return convertView;
}
@Override
public int getCount() {
return users == null ? 0 : users.size();
} }
感谢您的帮助!
【问题讨论】:
-
你要找的可能是
ExpandableListView -
谢谢 Blackbelt,但根据我的阅读,ExpandableListView 只允许一个子节点,但在我的情况下,我需要在前一个 ListView 中有一个 ListView。如果不是这样,请告诉我。
-
可能你看错了(或者我误解了你)
-
其实我觉得你是对的。如果它不起作用,我将尝试该方法并在此处报告。在此之前将保持问题开放
-
@ccrama 您可以在
ExpandableListView的每个组中拥有多个项目。您将有一个标题(您的主要评论),然后每个标题中有多个子元素(回复 cmets)
标签: android listview android-listview