Per la visualizzazione elenco su tablet, sto cercando di ottenere il mio stato selezionato per mantenere il suo stato selezionato, ma sfortunatamente sto osservando un comportamento strano. Per qualche motivo ogni volta che si scorre l'elenco fino al punto in cui l'elemento selezionato non è visibile e quindi si scorre indietro fino al punto in cui l'elemento selezionato è visibile, il colore dello sfondo viene inaspettatamente riutilizzato. Credo che qualcosa debba andare nel metodo getView ma non sono sicuro di cosa fare con questo metodo. Cosa si deve fare per impedire il riutilizzo del colore di sfondo?Colore di sfondo dell'elemento selezionato in modo inaspettato dopo l'elenco scorrere su tavolette
classe adattatore
public class VictoriaListAdapter extends BaseAdapter {
private List<Victoria> mData;
private LayoutInflater mInflater;
public VictoriaListAdapter (List<Victoria> data, Context context) {
mData = data;
mData = new ArrayList(mData);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position).getStation();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_dualline, parent, 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();
}
holder.title.setText(mData.get(position).getStation());
holder.description.setText(mData.get(position).getZone());
return convertView;
}
/**
* View holder
*/
static class ViewHolder {
private TextView title;
private TextView description;
}
}
classe Frammento
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<Victoria> list = new ArrayList<>();
private void initialize() {
String[] items = getActivity().getResources().getStringArray(R.array.victoria_stations);
String[] itemDescriptions = getActivity().getResources().getStringArray(R.array.victoria_zones);
for (int n = 0; n < items.length; n++){
Victoria victoria = new Victoria();
victoria.setID();
victoria.setStation(items[n]);
victoria.setZone(itemDescriptions[n]);
list.add(victoria);
}
mAdapter = new VictoriaListAdapter(list, getActivity());
setListAdapter(mAdapter);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
View v = getView();
mTwoPane = getActivity().findViewById(R.id.detail_container) != null;
assert v != null;
ListView lv = (ListView)v.findViewById(android.R.id.list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
private Victoria selectedMain;
private View selectedView;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
VictoriaListAdapter adapter = (VictoriaListAdapter) parent.getAdapter();
String station = adapter.getItem(position);
if (mTwoPane) {
setItemNormal();
View rowView = view;
setItemSelected(rowView);
Fragment newFragment;
if (station.equals(view.getResources().getString(R.string.bho))) {
newFragment = new FragmentVictoriaBHO();
} else if (station.equals(view.getResources().getString(R.string.brx))) {
newFragment = new FragmentVictoriaBRX();
} else if (station.equals(view.getResources().getString(R.string.eus))) {
newFragment = new FragmentVictoriaEUS();
} else if (station.equals(view.getResources().getString(R.string.fpk))) {
newFragment = new FragmentVictoriaFPK();
} else if (station.equals(view.getResources().getString(R.string.green_park))) {
newFragment = new FragmentVictoriaGreenPark();
} else if (station.equals(view.getResources().getString(R.string.hhy))) {
newFragment = new FragmentVictoriaHHY();
} else if (station.equals(view.getResources().getString(R.string.kxsp))) {
newFragment = new FragmentVictoriaKXSP();
} else {
newFragment = new FragmentVictoriaBHO();
}
VictoriaLineActivity activity = (VictoriaLineActivity) view.getContext();
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.fade_out, R.anim.fade_in);
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
} else {
Intent intent;
if (station.equals(view.getResources().getString(R.string.bho))) {
intent = new Intent(getActivity(), VictoriaBHOActivity.class);
} else if (station.equals(view.getResources().getString(R.string.brx))) {
intent = new Intent(getActivity(), VictoriaBRXActivity.class);
} else if (station.equals(view.getResources().getString(R.string.eus))) {
intent = new Intent(getActivity(), VictoriaEUSActivity.class);
} else if (station.equals(view.getResources().getString(R.string.fpk))) {
intent = new Intent(getActivity(), VictoriaFPKActivity.class);
} else if (station.equals(view.getResources().getString(R.string.green_park))) {
intent = new Intent(getActivity(), VictoriaGreenParkActivity.class);
} else if (station.equals(view.getResources().getString(R.string.hhy))) {
intent = new Intent(getActivity(), VictoriaHHYActivity.class);
} else if (station.equals(view.getResources().getString(R.string.kxsp))) {
intent = new Intent(getActivity(), VictoriaKXSPActivity.class);
} else {
intent = new Intent(getActivity(), VictoriaBHOActivity.class);
}
startActivity(intent);
}
}
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() {
for (int i = 0; i < getListView().getChildCount(); i++) {
View v = getListView().getChildAt(i);
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"));
}
}
});
super.onActivityCreated(savedInstanceState);
}
}
classe di dati
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;
}
}
così semplice perché si utilizza il pattern ViewHolder, il modello ViewHolder è di progettazione per riutilizzare la struttura di righe esistente senza crearne una nuova. Devi impostare in modo esplicito il colore della riga selezionata. –