【发布时间】:2015-05-08 16:11:11
【问题描述】:
我有一个嵌套的 ExpandableListViews 系统,它可以持续几代(树型布局)用于 cmets。每个顶级评论都可以在这些 cmets 上有带有子级的 cmets。为此,我有一个主 ExpandableListView 来存储所有顶级 cmets,并且每个“评论”XML 都包含一个可用于任何子 cmets 的 ExpandableListView。我的 ExpandableListView 的适配器如下
public class ExpandableCommentAdapter extends BaseExpandableListAdapter {
private static final class ViewHolder {
TextView textLabel;
}
private final List<CommentNode> itemList;
private final LayoutInflater inflater;
private Context main;
public ExpandableCommentAdapter(Context context, List<CommentNode> itemList) {
this.inflater = LayoutInflater.from(context);
this.itemList = itemList;
main = context;
}
@Override
public CommentNode getChild(int groupPosition, int childPosition) {
CommentNode n = itemList.get(groupPosition);
ArrayList<CommentNode> comments = new ArrayList<CommentNode>();
for (CommentNode node : n.walkTree(TraversalMethod.BREADTH_FIRST)) {
if (node.getParent().getComment().getId() == n.getComment().getId()) {
comments.add(node);
}
}
Log.v("RedditSlide", "COMMENT CHILD SIZE:" + comments.size());
return comments.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
CommentNode n = itemList.get(groupPosition);
ArrayList<CommentNode> comments = new ArrayList<CommentNode>();
CommentNode comment = n;
for (CommentNode node : comment.walkTree(TraversalMethod.BREADTH_FIRST)) {
if (node.getParent().getComment().getId() == comment.getComment().getId()) {
comments.add(node);
}
}
return comments.size();
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
final ViewGroup parent) {
final CommentNode user = getChild(groupPosition, childPosition);
if (convertView == null) {
convertView = LayoutInflater.from(parent.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);
}
}
ExpandableCommentAdapter adapter = new ExpandableCommentAdapter(convertView.getContext(), comments);
ExpandableListView listView = (ExpandableListView) convertView.findViewById(R.id.commentsListUnder);
listView.setAdapter(adapter);
return convertView;
}
@Override
public CommentNode getGroup(int groupPosition) {
return itemList.get(groupPosition);
}
@Override
public int getGroupCount() {
return itemList.size();
}
@Override
public long getGroupId(final int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final CommentNode user = itemList.get(groupPosition);
if (convertView == null) {
convertView = LayoutInflater.from(parent.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() + "");
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
我的评论XML如下
<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" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/upvote"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:background="@drawable/roundbutton"
android:onClick="upvote"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="@drawable/iconupvote"
/>
<ImageView
android:id="@+id/downvote"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="-7dp"
android:background="@drawable/roundbutton"
android:onClick="downvote"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="@drawable/icondownvote"
/>
<TextView
android:id="@+id/upvotePost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="7dp"
android:text="30"
android:textSize="14dp" />
</LinearLayout>
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
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" />
</LinearLayout>
我遇到的问题是子 cmets 的“可扩展性”似乎跳过了一代。 Level 1, Level 3 等都有展开,他们的孩子可见,但是Level 2 不能展开,只有一个孩子可见。其他孩子(4、5、6...)不可见,第 3 级有一个展开图标,但点击时不显示任何孩子。
我的布局/系统有什么问题? 谢谢!
【问题讨论】:
标签: android expandablelistview