|
7 | 7 | import android.graphics.BitmapFactory;
|
8 | 8 | import android.graphics.Canvas;
|
9 | 9 | import android.graphics.Paint;
|
| 10 | +import android.graphics.Rect; |
10 | 11 | import android.graphics.Typeface;
|
11 | 12 | import android.graphics.drawable.BitmapDrawable;
|
12 | 13 | import android.graphics.drawable.Drawable;
|
|
26 | 27 | import android.view.MotionEvent;
|
27 | 28 | import android.view.View;
|
28 | 29 | import android.view.ViewGroup;
|
| 30 | +import android.view.ViewTreeObserver; |
29 | 31 | import android.widget.BaseAdapter;
|
30 | 32 | import android.widget.Filter;
|
31 | 33 | import android.widget.Filterable;
|
|
39 | 41 | import com.rey.material.util.TypefaceUtil;
|
40 | 42 | import com.rey.material.widget.EditText;
|
41 | 43 | import com.rey.material.widget.ListPopupWindow;
|
| 44 | +import com.rey.material.widget.ListView; |
42 | 45 | import com.squareup.picasso.Picasso;
|
43 | 46 | import com.squareup.picasso.Target;
|
44 | 47 |
|
@@ -246,10 +249,51 @@ public void onDismiss() {
|
246 | 249 | mSelectedSpan = null;
|
247 | 250 | }
|
248 | 251 | });
|
249 |
| - mReplacementPopup.setAdapter(mReplacementAdapter); |
| 252 | + |
250 | 253 | mReplacementPopup.setAnchorView(this);
|
251 | 254 | mReplacementPopup.setModal(true);
|
| 255 | + mReplacementPopup.setAdapter(mReplacementAdapter); |
252 | 256 | mReplacementPopup.show();
|
| 257 | + |
| 258 | + mReplacementPopup.getListView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { |
| 259 | + @Override |
| 260 | + public void onGlobalLayout() { |
| 261 | + ListView lv = mReplacementPopup.getListView(); |
| 262 | + ViewTreeObserver observer = lv.getViewTreeObserver(); |
| 263 | + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) |
| 264 | + observer.removeOnGlobalLayoutListener(this); |
| 265 | + else |
| 266 | + observer.removeGlobalOnLayoutListener(this); |
| 267 | + |
| 268 | + View v = lv.getChildAt(0); |
| 269 | + v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); |
| 270 | + mReplacementPopup.setContentWidth(v.getMeasuredWidth()); |
| 271 | + |
| 272 | + |
| 273 | + int[] popupLocation = new int[2]; |
| 274 | + lv.getLocationOnScreen(popupLocation); |
| 275 | + |
| 276 | + int[] inputLocation = new int[2]; |
| 277 | + mInputView.getLocationOnScreen(inputLocation); |
| 278 | + |
| 279 | + Drawable background = mReplacementPopup.getPopup().getBackground(); |
| 280 | + Rect backgroundPadding = new Rect(); |
| 281 | + int verticalOffset; |
| 282 | + int horizontalOffset = inputLocation[0] + (int)mSelectedSpan.mX - (popupLocation[0] + backgroundPadding.left); |
| 283 | + |
| 284 | + if(background != null) |
| 285 | + background.getPadding(backgroundPadding); |
| 286 | + |
| 287 | + if(inputLocation[1] < popupLocation[1]) //popup show at bottom |
| 288 | + verticalOffset = inputLocation[1] + mSelectedSpan.mY - (popupLocation[1] + backgroundPadding.top); |
| 289 | + else |
| 290 | + verticalOffset = inputLocation[1] + mSelectedSpan.mY + mSpanHeight - (popupLocation[1] + lv.getHeight() - backgroundPadding.bottom); |
| 291 | + |
| 292 | + mReplacementPopup.setVerticalOffset(verticalOffset); |
| 293 | + mReplacementPopup.setHorizontalOffset(horizontalOffset); |
| 294 | + mReplacementPopup.show(); |
| 295 | + } |
| 296 | + }); |
253 | 297 | }
|
254 | 298 | }
|
255 | 299 |
|
@@ -468,24 +512,30 @@ private void queryNumber(Recipient recipient){
|
468 | 512 | Cursor cursor = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, COLS, selection, selectionArgs, null);
|
469 | 513 | if (cursor.getCount() > 0) {
|
470 | 514 | mItems = new Recipient[cursor.getCount()];
|
| 515 | + mItems[0] = recipient; |
471 | 516 | int index = 1;
|
472 | 517 | while (cursor.moveToNext()) {
|
473 | 518 | String number = cursor.getString(0);
|
474 | 519 |
|
475 |
| - if(number.equals(recipient.number)) |
476 |
| - mItems[0] = recipient; |
477 |
| - else{ |
| 520 | + if(!number.equals(recipient.number)){ |
478 | 521 | Recipient newRecipient = new Recipient();
|
479 | 522 | newRecipient.lookupKey = recipient.lookupKey;
|
480 | 523 | newRecipient.name = recipient.name;
|
481 | 524 | newRecipient.number = number;
|
| 525 | + if(index == mItems.length){ |
| 526 | + Recipient[] newItems = new Recipient[mItems.length + 1]; |
| 527 | + System.arraycopy(mItems, 0, newItems, 0, mItems.length); |
| 528 | + mItems = newItems; |
| 529 | + } |
482 | 530 | mItems[index] = newRecipient;
|
483 | 531 | index++;
|
484 | 532 | }
|
485 | 533 | }
|
486 | 534 | }
|
487 |
| - cursor.close(); |
| 535 | + else |
| 536 | + mItems = new Recipient[]{recipient}; |
488 | 537 |
|
| 538 | + cursor.close(); |
489 | 539 | notifyDataSetChanged();
|
490 | 540 | }
|
491 | 541 |
|
@@ -558,6 +608,7 @@ class RecipientSpan extends ContactChipSpan implements Target{
|
558 | 608 | private Recipient mRecipient;
|
559 | 609 | int mWidth;
|
560 | 610 | float mX;
|
| 611 | + int mY; |
561 | 612 |
|
562 | 613 | public RecipientSpan(Recipient recipient) {
|
563 | 614 | super(TextUtils.isEmpty(recipient.name) ? recipient.number : recipient.name,
|
@@ -613,6 +664,7 @@ public int getSize(Paint paint, CharSequence text, int start, int end, Paint.Fon
|
613 | 664 | @Override
|
614 | 665 | public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
|
615 | 666 | mX = x;
|
| 667 | + mY = top; |
616 | 668 | super.draw(canvas, text, start, end, x, top, y, bottom, paint);
|
617 | 669 | }
|
618 | 670 |
|
|
0 commit comments