ListView header 而不是在滚动视图中

ListView header instead of being in scrollview

我有一个显示我的项目的图像和名称的片段,我想在下面显示对该项目的评论列表。但是,我似乎无法在滚动视图中使用列表视图。人们建议对我的列表视图使用 header。但是我很困惑,因为我的 header 不是静态的,并且每个项目都不同(我必须通过 http 调用获取并填充 imageview 和 textview)。有人可以通过提供一个简单的例子或 link?

来帮助我解决这个问题

我读了这个Using a ListAdapter to fill a LinearLayout inside a ScrollView layout,但我想不通。

使用 ViewPager 作为 listView 的 header View。

然后自己操作 ViewPager 状态 :(load update show)..

如果您想要一个随评论列表滚动的 header,请查看 RecyclerView. You can find many examples and tutorials online. It allows you to create a list of items, but how an item is displayed can change. The RecyclerView.Adapter 确定如何显示内容,这就是您要关注的地方。

那里有很多教程,所以我不会在这里打扰 copy/pasting 代码。

我是这样修复的: 我为我的列表视图 (list_layout.xml) 创建了一个 xml 布局,id 为 "list" 和我的页眉的另一个 xml 布局 (header_layout.xml)。然后在我的片段中 在 onCreateView 中这样做:

 View v = inflater.inflate(R.layout.list_layout, container, false);

    ListView listView= (ListView) v.findViewById(R.id.list);
    View header = inflater.inflate(R.layout.header_layout,listView, false);
    listView.addHeaderView(header, null, false);
    adapter = new Adapter...
    listView.setAdapter(adapter);

    //lets say you have a text view in your header. This is how you set it
    //pay attention how used **header** NOT v as the view
    Name_textview=(TextView) header.findViewById(R.id.photo_number);