这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/day_frame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="4dip"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:padding="2dip" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dip" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/marca_bike_event" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dip" >

            <ImageView
                android:id="@+id/imageView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/marca_bike_event" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/imageView13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView18"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/marca_bike_event" />
        </LinearLayout>

        <TextView
            android:id="@+id/day_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dip"
            android:gravity="right|bottom"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>


这是我的自定义视图

import java.util.Calendar;

public class DayView extends LinearLayout {

    private final boolean belongsToMonth;
    private final TextView numberView;
    private int racesNum = 0;
    private final ImageView iv1, iv2, iv3, iv4, iv5, iv6, iv7, iv8, iv9, iv10,
    iv11, iv12, iv13, iv14, iv15, iv16, iv17, iv18;

    public DayView(final Context context, final Calendar moving,
            final SingleCalendar calendar) {
        super(context);
        View.inflate(context, R.layout.calendar_week_day, this);
        iv1 = (ImageView) findViewById(R.id.imageView1);
        iv2 = (ImageView) findViewById(R.id.imageView2);
        iv3 = (ImageView) findViewById(R.id.imageView3);
        iv4 = (ImageView) findViewById(R.id.imageView4);
        iv5 = (ImageView) findViewById(R.id.imageView5);
        iv6 = (ImageView) findViewById(R.id.imageView6);
        iv7 = (ImageView) findViewById(R.id.imageView7);
        iv8 = (ImageView) findViewById(R.id.imageView8);
        iv9 = (ImageView) findViewById(R.id.imageView9);
        iv10 = (ImageView) findViewById(R.id.imageView10);
        iv11 = (ImageView) findViewById(R.id.imageView11);
        iv12 = (ImageView) findViewById(R.id.imageView12);
        iv13 = (ImageView) findViewById(R.id.imageView13);
        iv14 = (ImageView) findViewById(R.id.imageView14);
        iv15 = (ImageView) findViewById(R.id.imageView15);
        iv16 = (ImageView) findViewById(R.id.imageView16);
        iv17 = (ImageView) findViewById(R.id.imageView17);
        iv18 = (ImageView) findViewById(R.id.imageView18);
        final ImageView[] views = new ImageView[] { iv1, iv2, iv3, iv4, iv5,
                iv6, iv7, iv8, iv9, iv10, iv11, iv12, iv13, iv14, iv15, iv16,
                iv17, iv18 };
        for (final ImageView v : views) {
            v.setVisibility(View.INVISIBLE);
        }

        numberView = (TextView) findViewById(R.id.day_number);
        numberView.setText(Integer.toString(moving.get(Calendar.DAY_OF_MONTH)));
        setTag(moving.clone());
        belongsToMonth = (moving.get(Calendar.MONTH) == calendar.current
                .get(Calendar.MONTH));
        final Resources res = getResources();
        if (!belongsToMonth) {
            numberView.setTextColor(res
                    .getColor(R.color.calendar_unbelonging_dayview_text));
            setBackgroundResource(R.drawable.unbelonging_dayview);
        } else {
            numberView.setTextColor(res
                    .getColor(R.color.calendar_belonging_dayview_text));
            setBackgroundResource((DateParseUtils.areEqualDays(moving,
                    calendar.today)) ? R.drawable.selected_dayview
                            : R.drawable.unselected_dayview);
        }
    }

    protected void onMeasure(final int widthMeasureSpec,

    public boolean belongsToMonth() {

    public Calendar getCalendar() {

    public void addRace(final ParseObject carrera) {
        final ImageView[] views = new ImageView[] { iv1, iv2, iv3, iv4, iv5,
                iv6, iv7, iv8, iv9, iv10, iv11, iv12, iv13, iv14, iv15, iv16,
                iv17, iv18 };


        if (racesNum < views.length) {
            final ImageView iv = views[racesNum];
            final int id = getRaceId(carrera);
            iv.setImageDrawable(getContext().getResources().getDrawable(id));
            for (int i = 0; i < views.length; i++) {
                final ImageView iv2 = views[i];
                if (i <= racesNum) {
                    iv2.setVisibility(View.VISIBLE);
                } else {
                    iv2.setVisibility(View.INVISIBLE);
                }
            }
            racesNum++;
        }

    }

    private int getRaceId(final ParseObject carrera) {

        final String disciplina = carrera.getString("disciplinaId");
        if (disciplina.equals(SettingsActivity.ID_BIKE_EVENT)) {
            return R.drawable.marca_bike_event;
        } else if (disciplina.equals(SettingsActivity.ID_BMX)) {
            return R.drawable.marca_bmx;
        } else if (disciplina.equals(SettingsActivity.ID_ENDURO)) {
            return R.drawable.marca_enduro;
        } else if (disciplina.equals(SettingsActivity.ID_FREESTYLE)) {
            return R.drawable.marca_freestyle;
        } else if (disciplina.equals(SettingsActivity.ID_MOTO_GP)) {
            return R.drawable.marca_moto_gp;
        } else if (disciplina.equals(SettingsActivity.ID_MOTOCROSS)) {
            return R.drawable.marca_motocross;
        } else if (disciplina.equals(SettingsActivity.ID_MOTORBIKE_RALLY)) {
            return R.drawable.marca_mc_rally;
        } else if (disciplina.equals(SettingsActivity.ID_MTB_4X)) {
            return R.drawable.marca_mtb_4x;
        } else if (disciplina.equals(SettingsActivity.ID_MTB_DH)) {
            return R.drawable.marca_mtb_dh;
        } else if (disciplina.equals(SettingsActivity.ID_MTB_ENDURO)) {
            return R.drawable.marca_mtb_enduro;
        } else if (disciplina.equals(SettingsActivity.ID_QUADCROSS)) {
            return R.drawable.marca_quad_cross;
        } else if (disciplina.equals(SettingsActivity.ID_SUPERBIKE)) {
            return R.drawable.marca_superbike;
        } else if (disciplina.equals(SettingsActivity.ID_SUPERMOTO)) {
            return R.drawable.marca_supermoto;
        } else if (disciplina.equals(SettingsActivity.ID_TRIAL)) {
            return R.drawable.marca_trial;
        } else if (disciplina.equals(SettingsActivity.ID_URBAN_DH)) {
            return R.drawable.marca_urban_dh;
        } else if (disciplina.equals(SettingsActivity.ID_XC_RALLIES)) {
            return R.drawable.marca_xc_rallies;
        }
        return R.drawable.marca_other_event;
    }

}


addRaceOnPostExecute方法调用AsyncTask。但是,它不会使任何ImageView可见。我已经调试了它在setVisibility行上放置一个断点,并且实际上在主线程上调用了setVisibility,但这是行不通的。尝试更改ImageView可绘制对象也不起作用。

我尝试设置它的可见性并更改构造函数上的drawable,并且它可以工作。似乎setVisibility方法什么也不做。

我想念什么吗?

最佳答案

好,解决了

我的DayView是在ArrayAdapter<Calendar>上动态创建的。我要做的就是在getViewArrayAdapter方法中进行所有视图操作,然后调用adapter.notifyDataSetChanged()而不是尝试直接修改它们。

另外,我必须使用ViewHolder模式才能正确完成修改。

10-05 22:17