Android listview令人费解的行为

时间:2015-01-25 22:34:23

标签: android android-listview

我在android应用程序中有一个listview,其中一些listview项目将根据绑定的对象属性以不同方式显示。我在绑定发生时尝试在适配器中进行,但结果并不像预期的那样。

根据条件,只有C先生的项目按钮应该是灰色的,但是除了这对夫妇之外,其他人也会变灰。有人可以提供一些有关此行为的见解吗?

MainActivity:

    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>

但我得到的结果如下: listview out put

1 个答案:

答案 0 :(得分:1)

视图得到回收,这意味着在您将一个List元素变灰后,它可能会在其他地方重新使用并仍然显示为灰色。

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

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