Skip to content

Commit

Permalink
fix #87 ScrollView中高亮的位置判断有偏差
Browse files Browse the repository at this point in the history
fix 多次调用show重复显示
  • Loading branch information
huburt-Hu committed Dec 11, 2018
1 parent 5e3dc2a commit 8e1fb98
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
</intent-filter>
</activity>
<activity android:name=".RecyclerViewActivity" />

<activity android:name=".ScrollViewActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ public void onClick(View v) {
RecyclerViewActivity.start(FirstActivity.this);
}
});
findViewById(R.id.btn_scroll).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ScrollViewActivity.start(FirstActivity.this);
}
});

final View btnRelative = findViewById(R.id.btn_relative);
btnRelative.setOnClickListener(new View.OnClickListener() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.app.hubert.newbieguide;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class ObservableScrollView extends ScrollView {

private OnScrollChangeListener scrollChangeListener = null;

public ObservableScrollView(Context context) {
super(context);
}

public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public ObservableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public void setScrollViewListener(OnScrollChangeListener onScrollChangeListener) {
this.scrollChangeListener = onScrollChangeListener;
}

@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if (scrollChangeListener != null) {
scrollChangeListener.onScrollChanged(this, x, y, oldx, oldy);
}
}

public interface OnScrollChangeListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.app.hubert.newbieguide;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ScrollView;

import com.app.hubert.guide.NewbieGuide;
import com.app.hubert.guide.core.Controller;
import com.app.hubert.guide.model.GuidePage;
import com.app.hubert.guide.util.LogUtil;

/**
* Created by hubert on 2018/12/11.
*/
public class ScrollViewActivity extends AppCompatActivity {

private Controller controller;

public static void start(Context context) {
Intent starter = new Intent(context, ScrollViewActivity.class);
context.startActivity(starter);
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll_view);
final View light = findViewById(R.id.v_light);
controller = NewbieGuide.with(ScrollViewActivity.this)
.setLabel("scroll1")
.alwaysShow(true)
.addGuidePage(GuidePage.newInstance()
.addHighLight(light))
.build();
ObservableScrollView scrollView = findViewById(R.id.scrollView);
scrollView.setScrollViewListener(new ObservableScrollView.OnScrollChangeListener() {
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
if (scrollView.getScrollY() + scrollView.getHeight() -
scrollView.getPaddingTop() - scrollView.getPaddingBottom()
== scrollView.getChildAt(0).getHeight()) {//判断滑动到底部
LogUtil.i("call show");
controller.show();
}
}
});
}
}
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_first.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
android:layout_height="wrap_content"
android:text="列表" />

<Button
android:id="@+id/btn_scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ScrollView" />

<Button
android:id="@+id/btn_relative"
android:layout_width="wrap_content"
Expand Down
95 changes: 95 additions & 0 deletions app/src/main/res/layout/activity_scroll_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<com.app.hubert.newbieguide.ObservableScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">

<View
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@color/colorPrimaryDark"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@color/colorPrimaryDark"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@color/colorPrimaryDark"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@color/colorPrimaryDark"
android:layout_height="100dp" />

<TextView
android:id="@+id/v_light"
android:layout_width="match_parent"
android:text="Highlight View"
android:gravity="center"
android:textSize="20sp"
android:background="@color/testColor"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@android:color/black"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@android:color/darker_gray"
android:layout_height="100dp" />

<View
android:layout_width="match_parent"
android:background="@android:color/holo_purple"
android:layout_height="100dp" />
</LinearLayout>
</com.app.hubert.newbieguide.ObservableScrollView>
12 changes: 12 additions & 0 deletions guide/src/main/java/com/app/hubert/guide/core/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class Controller {
private FrameLayout mParentView;
private SharedPreferences sp;
private int indexOfChild = -1;//使用anchor时记录的在父布局的位置
private boolean isShowing;

public Controller(Builder builder) {
this.activity = builder.activity;
Expand Down Expand Up @@ -103,6 +104,10 @@ public void show() {
}
}

if (isShowing) {
return;
}
isShowing = true;
mParentView.post(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -173,6 +178,7 @@ public void onGuideLayoutDismiss(GuideLayout guideLayout) {
if (onPageChangedListener != null) {
onPageChangedListener.onPageChanged(current);
}
isShowing = true;
}

private void showNextOrRemove() {
Expand All @@ -184,6 +190,7 @@ private void showNextOrRemove() {
onGuideChangedListener.onRemoved(Controller.this);
}
removeListenerFragment();
isShowing = false;
}
}

Expand Down Expand Up @@ -228,6 +235,11 @@ public void remove() {
}
currentLayout = null;
}
isShowing = false;
}

public boolean isShowing() {
return isShowing;
}

private void addListenerFragment() {
Expand Down
17 changes: 17 additions & 0 deletions guide/src/main/java/com/app/hubert/guide/util/ViewUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.graphics.Rect;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;

/**
* Created by zhy on 15/10/8.
Expand Down Expand Up @@ -33,7 +35,9 @@ public static Rect getLocationInView(View parent, View child) {
return result;
}
while (tmp != decorView && tmp != parent) {
LogUtil.i("tmp class:" + tmp.getClass().getSimpleName());
tmp.getHitRect(tmpRect);
LogUtil.i("tmp hit Rect:" + tmpRect);
if (!tmp.getClass().equals(FRAGMENT_CON)) {
result.left += tmpRect.left;
result.top += tmpRect.top;
Expand All @@ -42,6 +46,19 @@ public static Rect getLocationInView(View parent, View child) {
if (tmp == null) {
throw new IllegalArgumentException("the view is not showing in the window!");
}
//fix ScrollView中无法获取正确的位置
if (tmp.getParent() instanceof ScrollView) {
ScrollView scrollView = (ScrollView) tmp.getParent();
int scrollY = scrollView.getScrollY();
LogUtil.i("scrollY:" + scrollY);
result.top -= scrollY;
}
if (tmp.getParent() instanceof HorizontalScrollView) {
HorizontalScrollView horizontalScrollView = (HorizontalScrollView) tmp.getParent();
int scrollX = horizontalScrollView.getScrollX();
LogUtil.i("scrollX:" + scrollX);
result.left -= scrollX;
}

//added by [email protected] fix bug #21 the wrong rect user will received in ViewPager
if (tmp.getParent() != null && (tmp.getParent() instanceof ViewPager)) {
Expand Down

0 comments on commit 8e1fb98

Please sign in to comment.