当用户单击列表视图中的项目时,我将手动更改所选listView项目的背景颜色。我计划改变的不仅仅是背景,(最终是图标等),因此我需要listview来重绘自己。
问题是当我notifyDataChanged没有任何反应时。由于我在函数中放置了Log,当单击该项时,我可以验证是否触发了onItemClick,并且还调用了对适配器的notifyDataSetChanged调用。
我希望应该为每个位置再次调用“getView”,其中数据链接到视图。但这并没有发生。没有任何迹象表明发生了什么。我在这里缺少什么?
class MyOnItemClickListener implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(final AdapterView<?> parent, View view,
final int position, long id) {
Log.i(TAG, "listview on click " + position);
MyListAdapter adapter = (MyListAdapter) parent.getAdapter();
adapter.mSelected = position;
adapter.notifyDataSetChanged();
}
}
class MyListAdapter extends BaseAdapter{
public List<String> mTxtContents;
public int mGroupCount;
public int mSelected;
public void setData(List<String> txtContents){
mTxtContents = txtContents;
mGroupCount = txtContents.size();
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void notifyDataSetChanged() {
Log.i(TAG, "notify data set changed");
// TODO Auto-generated method stub
super.notifyDataSetChanged();
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public int getCount() {
Log.i(TAG, "nva panel group count " + mGroupCount);
return mGroupCount;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i(TAG, "get view, " + position);
if(convertView == null){
LayoutInflater li = LayoutInflater.from(getActivity());
convertView = li.inflate(R.layout.lvitem_navpanel, parent, false);
}
TextView tv = (TextView) convertView.findViewById(R.id.tv_txtData);
tv.setText(mTxtContents.get(position));
if(mSelected == position){
convertView.setBackgroundColor(Color.parseColor("#00FF00"));
}else{
convertView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
return convertView;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return true;
}
}
答案 0 :(得分:1)
修改后再次尝试设置适配器。像这样:
MyListAdapter adapter = (MyListAdapter) parent.getAdapter();
adapter.mSelected = position;
parent.setAdapter(adapter);
对setAdapter的新调用将清除回收器。
修改强>
我不确定你为什么要使用上面发布的模式(对我来说看起来像是不必要的样板和处理,但也许你有特殊需要),以下内容对你有所帮助
在OnClick侦听器中,您可以执行类似
的操作TextView txtView = (TextView)listView.getChildAt(position);
txtView.setBackgroundColor(Color.RED);
txtView.setText("New Text");
这会自动将更改应用于您的列表项。
如果您需要刷新整行,您可以
View view = list.getChildAt(position);
list.getAdapter().getView(position, view, list);