对于我在平板电脑上的列表视图,我试图让我选择的列表项选择在选中时保持其状态但不幸的是我看到一些奇怪的行为.出于某种原因,每当我滚动列表到所选项目不可见的点,然后滚动回到所选项目可见的点时,背景颜色意外地被重用.我相信有些东西需要放在getView方法中,但我不知道如何处理这个方法.必须采取哪些措施来防止背景颜色被重复使用?
适配器类
public class VictoriaListAdapter extends BaseAdapter {
private List
片段类
public class FragmentVictoriaLine extends ListFragment {
private VictoriaListAdapter mAdapter;
public FragmentVictoriaLine() {
}
/**
* Whether or not the activity is in two-pane mode,i.e. running on a tablet
* device.
*/
public boolean mTwoPane;
public static FragmentVictoriaLine newInstance() {
return new FragmentVictoriaLine();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_victoria_line,container,false);
initialize();
return view;
}
List
数据类
public class Victoria {
public Victoria(){}
private String station;
private String zone;
private boolean selected;
public String getStation(){
return station;
}
public void setStation(String item){
this.station = item;
}
public String getZone(){
return zone;
}
public void setZone(String zone){
this.zone = zone;
}
private int _id;
public void getID(int _id){
this._id = _id;
}
public int setID(){
return _id;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
最佳答案
当您滚动列表时,ListView中的项目/视图将被重用以优化内存.因此,当您将选定状态设置为列表项时,您将在滚动浏览时在多个列表项中看到所选状态.防止这种情况的最佳方法是保留数据模型中的状态,并在适配器的getView函数中设置状态.
这是你能做的 –
@Override
public void onItemClick(AdapterView> parent,long id) {
VictoriaListAdapter adapter = (VictoriaListAdapter) parent.getAdapter();
//reverse the selected state in data model
for (int i = 0; i < adapter.getCount(); i++) {
Victoria victoria = (Victoria)adapter.getItem(i);
victoria.setSelected(i == position ? true : false);
}
Victoria victoria = (Victoria)adapter.getItem(position);
---
---
在适配器 –
@Override
public Object getItem(int position) {
//Return full object,coz we need to access other
//member variables too
return mData.get(position);
}
@Override
public View getView(int position,ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_dualline,false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.item_station);
holder.description = (TextView) convertView.findViewById(R.id.item_zone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Victoria victoria = (Victoria)getItem(position);
holder.title.setText(victoria.getStation());
holder.description.setText(victoria.getZone());
if (victoria.isSelected()) {
setItemSelected(convertView);
} else {
setItemNormal(convertView);
}
return convertView;
}
public void setItemSelected(View view) {
View rowView = view;
view.setBackgroundColor(Color.parseColor("#868F98"));
TextView tv0 = (TextView) rowView.findViewById(R.id.item_station);
tv0.setTextColor(Color.WHITE);
TextView tv1 = (TextView) rowView.findViewById(R.id.item_zone);
tv1.setTextColor(Color.WHITE);
}
public void setItemNormal(View v) {
v.setBackgroundColor(Color.TRANSPARENT);
TextView tv0 = ((TextView) v.findViewById(R.id.item_station));
tv0.setTextColor(Color.WHITE);
TextView tv1 = ((TextView) v.findViewById(R.id.item_zone));
tv1.setTextColor(Color.parseColor("#B5B5B5"));
}
希望能帮助到你!