Android 弹出窗口中的ViewSwitcher

Android 弹出窗口中的ViewSwitcher,android,popupwindow,viewswitcher,Android,Popupwindow,Viewswitcher,我试图在自定义的PopupWindow中添加一个ViewSwitcher,当执行showNext()方法时会出现问题—什么都没有发生,显示的是相同的第一个视图 这是xml布局: <?xml version="1.0" encoding="utf-8"?> <ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/lyt_popup_arm"

我试图在自定义的PopupWindow中添加一个ViewSwitcher,当执行showNext()方法时会出现问题—什么都没有发生,显示的是相同的第一个视图

这是xml布局:

<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lyt_popup_arm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/popup_padding">

    <RelativeLayout
        android:id="@+id/lyt_progress"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:contentDescription="@string/popup_icon_contentdesc"
            android:layout_marginLeft="@dimen/popup_spinner_left_padding"
            android:layout_marginRight="@dimen/popup_spinner_right_padding"/>

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/spinner"
            android:textColor="#FFFFFF"
            android:textSize="@dimen/popup_text_size" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/lyt_result" 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/menu_annex"
            android:contentDescription="@string/popup_icon_contentdesc" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/icon" />

    </RelativeLayout>

</ViewSwitcher>
代码中添加的记录器在logcat控制台中绘制,然后执行正确

如果有人知道解决方案,那么知道它将是一件好事


提前谢谢

经过长时间的研究,我终于发现了问题所在

这与问题中提到的代码无关,真正的问题是当我试图从ScheduledExecutorService对象中的新Runnable()调用ViewSwitcher的showNext()方法时,由于这个原因,代码从未执行过

所以这里的教训是,我们想要运行UI线程的所有代码都不能在同一个线程之外执行

希望这有帮助


问候

顺便说一下,android版本是2.2
public class PopupProgressArm extends PopupProgress {
    protected ViewSwitcher vSwitcher;
    protected View v1;
    protected View v2;

    public PopupProgressArm(Context context) {
        super(context);
    }

    public PopupProgressArm(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void init() {
        baseView = LayoutInflater.from(context).inflate(R.layout.popup_arm, null);
        setContentView(baseView);
        initElements();
        vSwitcher = (ViewSwitcher) baseView.getRootView();
        v1 = vSwitcher.findViewById(R.id.lyt_progress);
        v1.setTag("V1");
        v2 = vSwitcher.findViewById(R.id.lyt_result);
        v2.setTag("V2");
    }

    public void setResult(String message, int icon) {
        Log.d("CheckerService", (String) vSwitcher.getCurrentView().getTag());
        Log.d("CheckerService", "" + !((String) vSwitcher.getCurrentView().getTag()).equals((String) v2.getTag()));

        if (!((String) vSwitcher.getCurrentView().getTag()).equals((String) v2.getTag())) {
            Log.d("CheckerService", "Setting the result");
            Button btn = (Button) v2.findViewById(R.id.btn);
            TextView txtMessage = (TextView) v2.findViewById(R.id.message);
            ImageView image = (ImageView) v2.findViewById(R.id.icon);

            txtMessage.setText(message); 
            image.setImageResource(icon);
            btn.setText(context.getResources().getString(R.string.accept));
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    PopupProgressArm.this.dismiss();
                }
            });
            vSwitcher.showNext();
        }
    }
}