嵌套的ExpandableListView仅适用于其他所有子项

时间:2015-03-06 23:12:07

标签: android expandablelistview

我有一个嵌套的ExpandableListViews系统,可以继续几代(树型布局)进行注释。每个顶级评论都可以对这些评论中的孩子发表评论。为此,我有一个主要的ExpandableListView来存储所有顶级注释,每个“注释”XML包含一个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>

我遇到的问题是儿童评论的“可扩展性”似乎跳过了一代人。 1级,3级等都有扩展,他们的孩子是可见的,但2级不能扩展,只有一个孩子可见。其他孩子(4,5,6 ......)不可见,3级 一个扩展图标,但点击时没有显示孩子。

我的布局/系统出了什么问题? 谢谢!

1 个答案:

答案 0 :(得分:0)

你的问题是两件事。您试图嵌套以相同方向滚动的可滚动视图。 Android无法正确处理这种情况,您将获得各种疯狂​​行为。试图“破解”这些问题只会引起各种各样的麻烦。

第二个问题是您在适配器中嵌入适配器。虽然这“可以”起作用,但实际上并不推荐。无法保证为同一个位置号码调用getChildViewgetParentView的次数。有时只有一次。有时它是两次。哎它可能是4次。每次重新创建一个新的适配器时,每个位置都会多次调用getChildViewgetParentView个方法。简而言之,这是一个主要的性能问题。

您最好的解决方案是重新设计一个不需要嵌入ExpandableListViews和适配器的UX设计。