setVisibility无法正常工作

时间:2013-01-03 11:09:02

标签: android android-layout visibility

这是我的布局:

<?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;
    }

}

addRace是通过OnPostExecute的{​​{1}}方法调用的。但是,它不会使任何ImageViews可见。我已调试它在AsyncTask行上放置一个断点,它实际上在主线程上调用了setVisibility,但它不起作用。尝试更改ImageView drawable也不起作用。

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

我错过了什么吗?

3 个答案:

答案 0 :(得分:1)

好的,解决了。

我的DayView是在ArrayAdapter<Calendar>上动态创建的。我所要做的只是在getView的{​​{1}}方法中执行所有视图操作,然后调用ArrayAdapter而不是尝试直接修改它们。

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

答案 1 :(得分:0)

您尝试从后台ImageView设置Async thread的可见性。 您无法直接访问来自后台主题的访问视图!

您可以使用Handler,并在下一个方法中更新您的观看次数

public void handleMessage(Message msg) {
    super.handleMessage(msg);
    // update views here
}

或者您可以直接在代码中使用下一个示例代码:

    imageView.post(new Runnable() {
        @Override
        public void run() {
            imageView.setVisibility(View.VISIBLE));
        }
    });

答案 2 :(得分:0)

的情况下,总是为我工作的最后手段
.setVisibility(VISIBLE/INVISIBLE/GONE) ;

失败的是添加/删除附加到ViewGroup的布局。将其发布到View线程,它将完成工作:

m_viewIsVisible=/false/true; //set before hand

public void run()
{    
    try
    {
       if (!m_viewIsVisible)
           RootView.rootGroop().removeView(currentLayout);
       else
           RootView.rootGroop().addView(currentLayout);

    } catch (Exception ex)
    {
        Log.e(TAG, "-Add/Remove layout throws exception:: " + ex.toString());
    }
}

此处currentLayout是您想要显示/隐藏的顽固布局。 RootView.rootGroop()为我提供了currentLayout所属的ViewGroup。 您应该将自己的逻辑应用于保持并获取对父ViewGroup的引用。