سؤال

I use StickyGridHeaders library to create an Gridview like snapfish. Here is my expected layout:

enter image description here

I show a popup windows when touching a button on header view (red arrow) instead of check box. The problem is my popup window always display wrong position. From debugging window, I can see it has the same location with sticky header, but I turned off sticky header for my gridview by using StickyGridHeadersGridView.setAreHeadersSticky(false);

Here is my adapter header:

    public View getHeaderView(final int position, View convertView, ViewGroup parent) {
            final HeaderViewHolder viewHolder;
            if(convertView == null){
                convertView = inflater.inflate(R.layout.item_header_gallery, null);
                viewHolder = new HeaderViewHolder();
                viewHolder.tvImagePrice = (TextView) convertView.findViewById(R.id.tvImagePrice);
                viewHolder.btnTouch = (Button) convertView.findViewById(R.id.btnTouch);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (HeaderViewHolder) convertView.getTag();       
            }

            ImageGroup item = headers.get(position);    
            viewHolder.tvImagePrice.setText(item.getPriceString());

            viewHolder.btnTouch.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    int[] location      = new int[2];

                    viewHolder.btnTouch.getLocationOnScreen(location);
                    // location always is [905,59] for every header item
                    System.out.println("location = " + location[0] + "," + location[1]);

                    // show popup on this location:
                    mPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]);
                }
            });

            return convertView;
        }

If I show a popup in child view instead of header, the popup window show correctly. Is there something wrong?

هل كانت مفيدة؟

المحلول 2

Look like there is no way to get header item position correctly, because this library used attach and detach header view in StickyGridHeadersBaseAdapterWrapper

HeaderFillerView v = getHeaderFillerView(adapterPosition.mHeader, convertView, parent);
View view = mDelegate.getHeaderView(adapterPosition.mHeader, (View)v.getTag(), parent);
mGridView.detachHeader((View) v.getTag());
v.setTag(view);
mGridView.attachHeader(view);
convertView = v;
mLastHeaderViewSeen = v;
v.forceLayout();

Actually, it called dispatchAttachedToWindow and dispatchDetachedFromWindow of view class by reflection. And it caused getting wrong windows on entire screen as nosacky said.
Now I am quit and replace this StickyGridHeaders by Listview with Header item and Gridview item. This isn't a best way but it can work.

نصائح أخرى

Looks like you are getting the windows location inside the other window and not the location on the entire screen. Take a look at this question for more details: getLocationOnScreen() vs getLocationInWindow()

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top