【问题标题】:ListView inside another ListView另一个 ListView 中的 ListView
【发布时间】:2025-12-24 08:45:11
【问题描述】:

我有一个自定义列表视图,可以在我的屏幕上显示帖子。有些帖子有 cmets,这就是为什么我需要另一个列表视图以便 cmets 可以与帖子一起显示。我正在使用 volley 并制作了一个 post 类,其中包含不同属性的 getter 和 setter 方法。我正在使用自定义列表视图适配器来显示帖子详细信息,但我也不明白如何显示 cmets。有人可以帮助我解释实现这一目标的最佳方法吗?

这是将使用的 JSON 示例:

{ 
  id: "10",
  author_id: "1",
  name: "John",
  picture: "uploads/image.jpg",
  text: "hello this is my first post:)"
  comments: [
      {
            comment: "Hello",
            comment_id: "1",
            comment_author_id: "2",
            post_id: "10"
      },
      {
            comment: "Hi",
            comment_id: "2",
            comment_author_id: "1",
            post_id: "10"
      },
   ]
},
{ 
  id: "11",
  author_id: "3",
  name: "Jane",
  picture: "uploads/image_2.jpg",
  text: "hello World ! "
  comments: []
},

我希望它看起来像下图,因此 cmets 会与帖子一起显示: http://i.stack.imgur.com/RfzNw.jpg

任何帮助都会很棒:)

【问题讨论】:

标签: android json listview android-listview custom-lists


【解决方案1】:

您不能将列表视图放在列表视图中。你应该添加像布局这样的 cmets。

LinearLayout commentsLayout = (LinearLayout)findViewById(R.id.commentsLayout);
View comment = getLayoutInflater().inflate(R.layout.comment);
commentsLayout.addView(comment);

【讨论】:

  • 如果我使用线性布局,我将如何在帖子下显示多个 cmets
【解决方案2】:

你应该为列表视图创建一个自定义适配器,这意味着你为每一行设置一个布局,然后你解析 json ..我认为这个链接会帮助你:https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

【讨论】: