8000 Add code to adjust width and location of replacement popup of Contact… · githubzhaoliang/Material@dcb579b · GitHub
[go: up one dir, main page]

Skip to content

Commit dcb579b

Browse files
committed
Add code to adjust width and location of replacement popup of ContactEditText class.
Expose some on() function of EditText class. Add OnSelectionChangedListener interface for TextView and EditText class.
1 parent ec2289e commit dcb579b

File tree

4 files changed

+503
-34
lines changed

4 files changed

+503
-34
lines changed

app/src/main/java/com/rey/material/app/ContactEditText.java

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.graphics.BitmapFactory;
88
import android.graphics.Canvas;
99
import android.graphics.Paint;
10+
import android.graphics.Rect;
1011
import android.graphics.Typeface;
1112
import android.graphics.drawable.BitmapDrawable;
1213
import android.graphics.drawable.Drawable;
@@ -26,6 +27,7 @@
2627
import android.view.MotionEvent;
2728
import android.view.View;
2829
import android.view.ViewGroup;
30+
import android.view.ViewTreeObserver;
2931
import android.widget.BaseAdapter;
3032
import android.widget.Filter;
3133
import android.widget.Filterable;
@@ -39,6 +41,7 @@
3941
import com.rey.material.util.TypefaceUtil;
4042
import com.rey.material.widget.EditText;
4143
import com.rey.material.widget.ListPopupWindow;
44+
import com.rey.material.widget.ListView;
4245
import com.squareup.picasso.Picasso;
4346
import com.squareup.picasso.Target;
4447

@@ -246,10 +249,51 @@ public void onDismiss() {
246249
mSelectedSpan = null;
247250
}
248251
});
249-
mReplacementPopup.setAdapter(mReplacementAdapter);
252+
250253
mReplacementPopup.setAnchorView(this);
251254
mReplacementPopup.setModal(true);
255+
mReplacementPopup.setAdapter(mReplacementAdapter);
252256
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+
});
253297
}
254298
}
255299

@@ -468,24 +512,30 @@ private void queryNumber(Recipient recipient){
468512
Cursor cursor = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, COLS, selection, selectionArgs, null);
469513
if (cursor.getCount() > 0) {
470514
mItems = new Recipient[cursor.getCount()];
515+
mItems[0] = recipient;
471516
int index = 1;
472517
while (cursor.moveToNext()) {
473518
String number = cursor.getString(0);
474519

475-
if(number.equals(recipient.number))
476-
mItems[0] = recipient;
477-
else{
520+
if(!number.equals(recipient.number)){
478521
Recipient newRecipient = new Recipient();
479522
newRecipient.lookupKey = recipient.lookupKey;
480523
newRecipient.name = recipient.name;
481524
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+
}
482530
mItems[index] = newRecipient;
483531
index++;
484532
}
485533
}
486534
}
487-
cursor.close();
535+
else
536+
mItems = new Recipient[]{recipient};
488537

538+
cursor.close();
489539
notifyDataSetChanged();
490540
}
491541

@@ -558,6 +608,7 @@ class RecipientSpan extends ContactChipSpan implements Target{
558608
private Recipient mRecipient;
559609
int mWidth;
560610
float mX;
611+
int mY;
561612

562613
public RecipientSpan(Recipient recipient) {
563614
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
613664
@Override
614665
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
615666
mX = x;
667+
mY = top;
616668
super.draw(canvas, text, start, end, x, top, y, bottom, paint);
617669
}
618670

0 commit comments

Comments
 (0)
0