如何将ImageView添加为ListView标头?

时间:2012-07-18 06:56:07

标签: android listview header

有人可以帮我找出如何将ImageView作为标题添加到ListView吗?

我的代码在这里:

LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snapshot = inflater.inflate(R.layout.map_snapshot, null);

ListView channelsList = (ListView) findViewById(R.id.channelsList);
channelsList.addHeaderView(snapshot);

到现在它什么都没有显示。

4 个答案:

答案 0 :(得分:7)

尝试通过父(ListView)作为参数膨胀标题视图:

ListView channelsList = (ListView) findViewById(R.id.channelsList);
View snapshot = inflater.inflate(R.layout.map_snapshot, channelList, false);

然后,只需将其添加到ListView:

channelsList.addHeaderView(snapshot);

答案 1 :(得分:0)

您可以使用CommonsWare MergeAdapterSectionedListAdapter。使用此库,您可以在View中使用任何Listview作为标题。有关详细信息,请参阅this SO question和接受的答案。

答案 2 :(得分:0)

尝试:

ListView channelsList = (ListView) findViewById(R.id.channelsList);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.map_snapshot, channelsList , false);
channelsList .addHeaderView(header, null, false);

答案 3 :(得分:0)

在布局map_snapshot中,您必须具有父布局,因为我已采用线性布局同样采用它。

 ListView channelsList = (ListView) findViewById(R.id.channelsList);
    LinearLayout headerView = (LinearLayout) getLayoutInflater().inflate(R.layout.map_snapshot, null);
    listView.addHeaderView(headerView);

如果要在布局中添加视图:

TextView mUsername=(TextView) headerView.findViewById(R.id.user_name);
相关问题