我有一个文章列表,每10篇文章就有一篇广告。问题是--而不是将广告添加到列表中,而是实际上取代了一篇文章。
正在发生的事情的例子:
- ad //taking the place of article 1
- article 2
- article 3
- article 4
...
- article 9
- article 10
- ad //taking the place of article 11
- article 12我让它展示每一篇X文章的方式是在我的ArrayAdapter中
@Override
//determines which view type I want to use
public int getItemViewType(int position) {
this.currentLayout = position % 11 == 0 ? 1 : 0;
return this.currentLayout;
}
@Override
//says there are 2 different view types I want to use
public int getViewTypeCount() {
return 2;
}所以-我想现在我看到了它的发生,这是有道理的,但是.怎样才能让广告出现,然后再写下一篇文章?
以前的尝试:
以前,我的XML中的文章就在文章本身下面,然后根据#打开/关闭它,但是被告知这是过度的XML (并且无论如何都有一些问题)。
每当文章显示广告时,我也尝试将文章作为下一项添加,但是--如果我不是这样一个菜鸟,我会立即意识到这是行不通的(它会复制这篇文章--很多):
public int getItemViewType(int position) {
//...
if(this.currentLayout == 1) this.insert(this.getItem(position), position);
//...发布于 2013-01-16 05:04:48
怎样才能让广告出现,然后再写下一篇文章?
在适配器中,需要覆盖getCount()以说明广告,然后调整getView()中的position以避免加载错误的数据。
在你的例子中:
getCount()应该返回12篇文章+2个ads = 14行。getView()应该在你给getItem()打电话时调整position,可能是:getItem(position - (position / 11) + 1) (但老实说,我的数学可能还差得很远)。发布于 2013-01-16 06:00:51
看看MergeAdapter。它允许您将适配器和视图混合到一个统一的适配器中,并负责为您纠正位置。您可能需要创建多个ArrayAdapter实例来创建节,然后放置广告,就像有节标题一样。
https://stackoverflow.com/questions/14351667
复制相似问题