我使用高效适配器来填充 mylistview,如下所示:
问问题
456 次
2 回答
2
您可以做的是使用setTag()
并getTag()
保持滚动位置,并检查您的 ListView 行的 ImageView 是否不会相互更改资源。
在适配器的getView()
类中使用
vi.setTag(R.id.btOnOFF, holder.btOnOFF);
然后onItemClick()
使用getTag()
ImageView imgview = (ImageView) view.getTag(R.id.btOnOFF);
imgview.setBackgroundResource(R.drawable.air_radio_button_rouge);
如需进一步参考,您可以查看我的博客文章
于 2013-07-05T06:06:34.893 回答
0
你可以尝试这样的事情。我清理并简化了您的代码。
public class EfficientAdapter extends BaseAdapter {
private Activity mActivity;
private ArrayList<SearchTracks> mSearchTracks;
private ResultatMultiple mResultatMultiple;
private int mSelectedPosition;
public EfficientAdapter(Activity activity, ArrayList<SearchTracks> searchTracks) {
mActivity = activity;
mSearchTracks = searchTracks;
}
public EfficientAdapter(ResultatMultiple resultatMultiple){
mResultatMultiple = resultatMultiple ;
}
public void setSelectedPosition(int position) {
mSelectedPosition = position;
}
@Override
public int getCount() {
return mSearchTracks.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.ecran_multiple_row, null);
holder = new ViewHolder();
holder.indexTextView = (TextView)view.findViewById(R.id.txIndex);
holder.titleTextView = (TextView)view.findViewById(R.id.txSTitle);
holder.buttonOnOffImageView = (ImageView)view.findViewById(R.id.btOnOFF);
view.setTag(holder);
}else {
holder = (ViewHolder)view.getTag();
}
int index = position + 1;
holder.indexTextView.setText((index <= 9 ? "0" : "") + Integer.toString(index));
holder.titleTextView.setText(mSearchTracks.get(position).getTitle());
if(position % 2 == 0) {
view.setBackgroundResource(R.drawable.listview_selector_odd);
}else {
view.setBackgroundResource(R.drawable.listview_selector_even);
}
if(mSearchTracks.size() == 1 || position == mSelectedPosition) {
holder.buttonOnOffImageView.setBackgroundResource(R.drawable.air_radio_button_rouge);
}else {
holder.buttonOnOffImageView.setBackgroundResource(R.drawable.air_deezer_check);
}
return view;
}
private class ViewHolder {
public TextView indexTextView;
public TextView titleTextView;
public ImageView buttonOnOffImageView;
}
}
现在在您的 onClick 侦听器中,您可以调用listAdapter.setSelectedPosition(position);
然后listAdapter.notifyDataSetChanged();
重新加载您的表。
于 2013-07-11T02:23:59.123 回答