Skip to content

Commit

Permalink
init popupwindow delay
Browse files Browse the repository at this point in the history
  • Loading branch information
hurshi1 committed Aug 21, 2018
1 parent 66adbd5 commit 20c35e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
32 changes: 17 additions & 15 deletions app/src/main/java/com/github/hurshi/clickedwords/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -58,20 +46,34 @@ private void setTextViewSpanStr() {
}

@Override
public void wordDisplay(String words) {
tv.setText(words);
public void wordFetched(PopupWindow popupWindow, String words) {
popupWindow.getContentView().<TextView>findViewById(R.id.textview).setText(words);
}

@Override
public void showPopupWindow(PopupWindow popupWindow, TextView textView, int offsetX, int offsetY) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@
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<Integer, Integer> 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);
}
}
});
builder.build();
}

private static void setClickedStyle(final TextView textView, final PopupWindow popupWindow, Pair<Integer, Integer> indexs, int focusedFgColor, int focusedBgColor) {
if (null == popupWindow) {
return;
}
final CharSequence spannableTxt = textView.getText();
setTextViewClicked(textView, indexs, focusedFgColor, focusedBgColor);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 20c35e1

Please sign in to comment.