Sunday, April 20, 2014

[android-developers] What's problem with PopupWindow?

I'm using PopupWindow to develop a Menu style UI. The content view of PopupWindow is created programmatically. The outer view is a scrollview and it has a new LinearLayout, the fragment codes is like:
public class ContainerView extends ScrollView {
...
  public ContainerView(Context context) {
super(context);
init();
}
public ContainerView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

        private void init() {
...
mContentView = new LinearLayout(getContext());
mContentView.setOrientation(LinearLayout.VERTICAL);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.bottomMargin = mArrowHeight;
addView(mContentView, layoutParams); 
}
I just want to dynamically create Menu item before showing PopupWindow, so my codes can accept a user customized Menu item layout and inflate it when user call addMenu function.
But I also want to reuse Menu item view if user dismiss PopupWindow and reshow again! So I setVisibility with GONE when PopupWindow is dismissed and visible it when addMenu is called, the codes just like:
public void addMenu(int id, String title, Drawable d) {
View v = mContainerView.getMenuView(mMenus.size());
mMenus.add(new MenuItem(id, title, d));
if (null != v) {
v.setVisibility(View.VISIBLE);
} else {
v = ((Activity)mContext).getLayoutInflater().inflate(mMenuResId, null);
mContainerView.addMenuView(v);
}
TextView text = (TextView)v.findViewById(mTextResId);
text.setText(title);
text.setId(id);
text.setOnClickListener(mViewOnClickListener);
if (mImageResId > 0) {
ImageView img = (ImageView)v.findViewById(mImageResId);
img.setImageDrawable(d);
img.setId(id);
img.setOnClickListener(mViewOnClickListener);
}
}
The Menu window can display without any error at the first time, but if dismiss it(touch screen out of PopupWindow) and click button to show it again, the exception is raised. The TextView object in the bold line is null. I debugged and the Menu item view is valid and also contains a valid TextView, but the id of TextView is 0, Why? I don't know who reset the id the TextView. I added breakpoint in onDismiss listener and found the ids of all Menu item view are cleared. I have no idea about it. Could you please help me take a look? How could I fix it?
Thanks a lot!

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment