发生了什么我有一个列表视图,我将背景颜色更改为选择。默认情况下,我将第一个列表视图项目选为
public class OneWayFlightResult extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public OneWayFlightResult(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.row, null);
TextView flightTime = (TextView)vi.findViewById(R.id.flightTime); // title
TextView flightCompanyName = (TextView)vi.findViewById(R.id.flightCompanyName); // title
TextView flightNumber = (TextView)vi.findViewById(R.id.flightNumber); // title
ImageView flightLogo = (ImageView)vi.findViewById(R.id.flightLogo);
HashMap<String, String> flight = new HashMap<String, String>();
flight = data.get(position);
flightTime.setText(flight.get(TestActivity.FlightTime));
flightCompanyName.setText(TestActivity.FlightCompanyName);
flightNumber.setText(TestActivity.FlightNumber);
if(position == 0){
vi.setBackgroundResource(R.drawable.selection_effect);
vi.setSelected(true);
}
return vi;
}
这是我在这个selection_effect.xml中使用的 XML 文件
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape>
<solid android:color="#ffffff" />
</shape>
</item>
<item>
<shape>
<solid android:color="#00a7eb" />
</shape>
</item>
</selector>
所以默认情况下,第一个列表视图被选中。现在当用户选择另一个列表视图项目时,第一个保持选中状态,另一个也得到相同的效果。那么如何动态改变点击列表视图项目的效果。意味着默认情况下,当用户选择其他项目时,第一个项目被选中,其他项目被选中,默认的效果被删除