Android 列表视图令人费解的行为

Android listview puzzling behavior

我在 android 应用程序中有一个列表视图,其中一些列表视图项目将根据绑定对象属性以不同方式显示。当发生绑定时,我尝试在适配器本身中执行操作,但结果与预期不符。

根据条件,只有 C 先生的项目按钮应该变灰,但与那对其他项目按钮一起变灰。有人可以提供有关此行为的一些见解吗?

主要活动:

    super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ListView listView = (ListView) findViewById(R.id.relatedFeelingsList);
            List<Car> cars = new ArrayList<Car>();
            cars.add(new Car("Ford",2008, "Mr A"));
            cars.add(new Car("BMW",2009, "Mr B"));
            cars.add(new Car("Mercedes",1999, "Mr C"));
            cars.add(new Car("Honda",2004, "Mr D"));
            cars.add(new Car("Maruti",2005, "Mr E"));
            cars.add(new Car("Ferrari",2015, "Mr F"));
            ArrayAdapter<Car> adapter = new    ListAdpater(getApplicationContext(),R.id.relatedFeelingsList,cars);
            listView.setAdapter(adapter);  


Adapter Code:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    final Car car = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.listview_item, null);
        holder = new ViewHolder();
        holder.carNameView = (TextView) convertView.findViewById(R.id.name);
        holder.carOwnerView = (TextView) convertView.findViewById(R.id.owner);
        holder.startButton = (Button) convertView.findViewById(R.id.btnStart);
        holder.parkButton = (Button) convertView.findViewById(R.id.btnPark);
        holder.reportButton = (Button) convertView.findViewById(R.id.btnReport);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.carNameView.setText(car.getMake());
    holder.carOwnerView.setText(car.getOwner());
    setCarViewInListView(convertView, holder, car);
    return convertView;
}

private void setCarViewInListView(View convertView, ViewHolder holder, Car car) {

    if (car.getYear() < 2000) {
        setOldCarView(convertView, holder, car);
    } 
}

private void setOldCarView(View convertView, ViewHolder holder, Car car) {
    //  int color = getContext().getResources().getColor(R.color.greyColor);
    DisableButton(holder.startButton);//.setBackgroundColor(color);
    DisableButton(holder.parkButton);//.setBackgroundColor(color);
    DisableButton(holder.reportButton);//.setBackgroundColor(color);
}

private void DisableButton(Button button) {
    button.setEnabled(false);
    button.setClickable(false);
    button.setBackgroundColor(Color.GRAY);
}

ListItem.xml

        <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_height="fill_parent"
        android:layout_marginBottom="50dp"
        >

        <RelativeLayout
            android:id="@+id/feelingDetails"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:padding="7dp"
            android:layout_height="wrap_content">

            <LinearLayout
                android:orientation="horizontal"
                android:id="@+id/feelingTextLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <TextView
                    android:id="@+id/name"

                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="15dp"
                    android:text="Car"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/owner"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="15dp"
                    android:layout_marginBottom="8dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Owner" />
            </LinearLayout>


        </RelativeLayout>

         <LinearLayout
            android:layout_weight="0"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="7dp">

            <Button
                android:id="@+id/btnStart"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                style="@style/buttonStyle"
                android:paddingRight="7dp"
                android:layout_weight="1"
                android:text="Start" />

            <Button
                android:id="@+id/btnPark"
                android:layout_marginLeft="2dp"
                android:paddingRight="7dp"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                style="@style/buttonStyle"
                android:layout_weight="1"
                android:text="Park" />

            <Button
                style="@style/buttonStyle"
                android:id="@+id/btnReport"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_weight="1"
                android:paddingRight="7dp"
                android:layout_alignTop="@id/btnPark"
                android:layout_marginLeft="2dp"
                android:text="Report" />
        </LinearLayout>

    </LinearLayout>

但我得到的结果如下:

视图被回收,这意味着在您将 List 元素变灰后,它可能会在其他地方重新使用并且仍然是灰色的。

您必须撤消此操作并恢复原始视图状态,例如通过在此处添加:

if (car.getYear() < 2000) {
        setOldCarView(convertView, holder, car);
    } else {
        setNewCarView(convertView, holder, car);
}