【问题标题】:Nested ListView Only Running First getView()嵌套的 ListView 只运行第一个 getView()
【发布时间】: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


【解决方案1】:

这可以在不使用 ExpandableListView 的情况下完成。 正在发生的事情是,当您将列表视图嵌入到另一个列表视图中时,它正在缩小,您需要做的是根据项目数动态调整内部列表视图的高度。在您的外部适配器中执行此操作:

OrderDetailAdapter adapter = new OrderDetailAdapter(context,0,orderDetails);
        int numberOfItems = orderDetails.size();
        ViewGroup.LayoutParams params = holder.listView.getLayoutParams();
        int pixels = (int) (80 * scale);
        int dpPixels = (int) (0.1 * scale);
        params.height =numberOfItems * pixels + (numberOfItems - 1) * dpPixels;
        holder.listView.setLayoutParams(params);
        holder.listView.setAdapter(adapter);

OrderDetailAdapter 是内部列表的适配器。 尺度是内部获取的:

scale = context.getResources().getDisplayMetrics().density;

这是一个浮点变量;

【讨论】:

    【解决方案2】:

    我通过使用ExpandableListView 而不是ListView 解决了这个问题。

    【讨论】:

    • 您是否在ExpandableListView 中将您的热门评论显示为组视图,并将所有 cmets 显示为子视图?
    • 如果您使用代码提交解决方案,我认为您的回答会更有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2018-07-24
    • 2011-05-10
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    相关资源
    最近更新 更多