diff --git a/app/src/main/java/com/github/hurshi/clickedwords/MainActivity.java b/app/src/main/java/com/github/hurshi/clickedwords/MainActivity.java index d0660c7..64a79ec 100644 --- a/app/src/main/java/com/github/hurshi/clickedwords/MainActivity.java +++ b/app/src/main/java/com/github/hurshi/clickedwords/MainActivity.java @@ -22,9 +22,6 @@ public class MainActivity extends AppCompatActivity implements OnWordsDisplayLis private TextView textView2; private TextView textView3; - private PopupWindow popupWindow; - private TextView tv; - @Override protected void onCreate(Bundle savedInstanceState) { @@ -36,15 +33,6 @@ protected void onCreate(Bundle savedInstanceState) { setTextViewSpanStr(); - LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); - View popupView = inflater.inflate(R.layout.view_words, null); - tv = (TextView) popupView.findViewById(R.id.textview); - - popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true); - - if (Build.VERSION.SDK_INT >= 21) - popupWindow.setElevation(20); - setClickedWords(textView1); setClickedWords(textView2); setClickedWords(textView3); @@ -58,8 +46,8 @@ private void setTextViewSpanStr() { } @Override - public void wordDisplay(String words) { - tv.setText(words); + public void wordFetched(PopupWindow popupWindow, String words) { + popupWindow.getContentView().findViewById(R.id.textview).setText(words); } @Override @@ -67,11 +55,25 @@ public void showPopupWindow(PopupWindow popupWindow, TextView textView, int offs popupWindow.showAtLocation(textView, Gravity.CENTER_HORIZONTAL | Gravity.TOP, offsetX, offsetY); } + PopupWindow popupWindow = null; + + @Override + public PopupWindow getInitedPopupWindow() { + if (null == popupWindow) { + LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); + View popupView = inflater.inflate(R.layout.view_words, null); + popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true); + if (Build.VERSION.SDK_INT >= 21) + popupWindow.setElevation(20); + } + return popupWindow; + } + + private void setClickedWords(TextView textView) { ClickedWordsDisplayer.showAsPopupWindow( new ClickedWords.Builder() .setTextView(textView), - popupWindow, this, R.color.focusedFgColor, R.color.focusedBgColor diff --git a/clickedwordslib/src/main/java/com/github/hurshi/clickedwordslib/helper/ClickedWordsDisplayer.java b/clickedwordslib/src/main/java/com/github/hurshi/clickedwordslib/helper/ClickedWordsDisplayer.java index c4caacf..21018b5 100644 --- a/clickedwordslib/src/main/java/com/github/hurshi/clickedwordslib/helper/ClickedWordsDisplayer.java +++ b/clickedwordslib/src/main/java/com/github/hurshi/clickedwordslib/helper/ClickedWordsDisplayer.java @@ -15,19 +15,18 @@ import com.github.hurshi.clickedwordslib.util.ClickWordsUtils; public class ClickedWordsDisplayer { - public static void showAsPopupWindow(ClickedWords.Builder builder, final PopupWindow popupWindow, final OnWordsDisplayListener listener, final int focusedFgColor, final int focusedBgColor) { - if (null == popupWindow) { - return; - } + public static void showAsPopupWindow(ClickedWords.Builder builder, final OnWordsDisplayListener listener, final int focusedFgColor, final int focusedBgColor) { builder.addListener(new OnWordsClickedListener() { @Override public void wordsClicked(TextView textView, String words, Pair index, Rect focusedRect, int[] locationInScreen) { int x = (int) (locationInScreen[0] + focusedRect.left + (focusedRect.right - focusedRect.left - ClickWordsUtils.getScreenW(textView.getContext())) / 2.0f); int y = locationInScreen[1] + focusedRect.bottom; - setClickedStyle(textView, popupWindow, index, focusedFgColor, focusedBgColor); + if (null != listener) { - listener.wordDisplay(words); + PopupWindow popupWindow = listener.getInitedPopupWindow(); + listener.wordFetched(popupWindow, words); listener.showPopupWindow(popupWindow, textView, x, y); + setClickedStyle(textView, popupWindow, index, focusedFgColor, focusedBgColor); } } }); @@ -35,6 +34,9 @@ public void wordsClicked(TextView textView, String words, Pair } private static void setClickedStyle(final TextView textView, final PopupWindow popupWindow, Pair indexs, int focusedFgColor, int focusedBgColor) { + if (null == popupWindow) { + return; + } final CharSequence spannableTxt = textView.getText(); setTextViewClicked(textView, indexs, focusedFgColor, focusedBgColor); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { diff --git a/clickedwordslib/src/main/java/com/github/hurshi/clickedwordslib/ui/OnWordsDisplayListener.java b/clickedwordslib/src/main/java/com/github/hurshi/clickedwordslib/ui/OnWordsDisplayListener.java index 1211d2a..869c048 100644 --- a/clickedwordslib/src/main/java/com/github/hurshi/clickedwordslib/ui/OnWordsDisplayListener.java +++ b/clickedwordslib/src/main/java/com/github/hurshi/clickedwordslib/ui/OnWordsDisplayListener.java @@ -4,7 +4,10 @@ import android.widget.TextView; public interface OnWordsDisplayListener { - void wordDisplay(String words); + + PopupWindow getInitedPopupWindow(); + + void wordFetched(PopupWindow popupWindow, String words); void showPopupWindow(PopupWindow popupWindow, TextView textView, int offsetX, int offsetY); }