我试图在自定义的 PopupWindow 中添加一个 ViewSwitcher,当执行 showNext() 方法时出现问题它什么也没发生,显示相同的第一个 View 。
这是 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>
java代码如下:
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();
}
}
}
代码中添加的logger在logcat控制台中绘制,然后执行正确。
如果有人知道解决方案,知道它会很高兴。
提前致谢!
最佳答案
经过长时间的研究,我终于找到了问题所在。
它与问题中提到的代码无关,真正的问题是当我试图从 ScheduledExecutorService 对象内的新 Runnable() 调用 ViewSwitcher 的 showNext() 方法时,因此代码从未被处决。
所以这里的教训是我们要运行 UI 线程的所有代码都不能在同一个线程外执行。
希望这对您有所帮助!
问候!
关于android - PopupWindow 中的 ViewSwitcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13360796/