我正在尝试(约4天)来更改特定列表视图项目的视图。我尝试了很多可用的解决方案,虽然这个问题看起来可能是“重复的”,但我已尽最大努力实施所有解决方案,但仍然没有运气。如何更改/更新列表视图的单个列表项?
所以这里是我的问题,我有一个列表视图,它是通过使用延伸BaseAdapter
的定制适配器创建的。
现在需要发生的是,根据状态,特定列表项目中的图像将被改变。
下面是一些代码:根据状态操纵
nowPlayingListView = (ListView) findViewById(R.id.nowPlayingListView);
nowPlayingListViewAdapter = new NowPlayingListViewAdapter(this, songsList);
nowPlayingListView.setAdapter(nowPlayingListViewAdapter);
这里:
public void handleSelectedState(Drawable dr) {
int currentPosition=getPosition();
View singLeListItem = getViewByPosition(currentPosition, nowPlayingListView);
ImageButton imgButton = (ImageButton) findViewById(R.id.npListViewSingleImageButton);
int count = ((ViewGroup) singLeListItem).getChildCount();
for (int i = 0; i < count; i++) {
View nextChild = ((ViewGroup) singLeListItem).getChildAt(i);
try {
//Log.d("Loop", count + "" + nextChild.getTag() + nextChild.getTag().equals("linearLayoutImageButton"));
if (nextChild.getTag().equals(getResources().getString(R.string.NPSingleItemLinearLayoutImgButton))) {
imgButton = (ImageButton) ((ViewGroup) nextChild).getChildAt(0);
imgButton.setImageDrawable(null);
imgButton.setImageDrawable(dr);
break;
}
} catch (Exception e) {
}
}
}
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
我试图做adpater.notifydatasetchanged
方法图像绘制被设定后
列表视图初始化但没有任何反应。
此外,代码仅适用于可见的列表项,但这不是我所需要的。我想更新不可见的列表项目的更改。
感谢。
这是最好的改变你的适配器来处理这些状态 – Pooya
你究竟是什么意思? –