5

htmlファイルを表示するWebビューがあります。ユーザーがWebビューでこのファイルの一番下までスクロールすると、以前は非表示になっていたボタンを表示したいのですが、ユーザーはこのボタンを押してアクティビティを実行できます。

iOSでも同様のことを行いました。ここでは、デリゲートをViewControllerに設定し、ボタンを表示として設定しました。Androidで同様のことを行うにはどうすればよいですか?iOSのようなコールバックメソッドがないことに気づきました。

編集:現在、2つのオブジェクトを持つアクティビティがあります。テキストを含むWebビューと、現在表示されていないボタンです。Webビューのテキストが一番下までスクロールしたときにアクティビティにメッセージを受信させ、ボタンを表示させたい

4

5 に答える 5

8

ユーザーがEULAの一番下までスクロールしたときに「同意する」ボタンを表示するには、これを自分で行う必要がありました。弁護士か?

実際、(@JackTurky からの回答のように ScrollView ではなく) WebView をオーバーライドすると、computeVerticalScrollRange() を呼び出してコンテンツの高さを取得できます。

これが私の包括的なソリューションです。私が見る限り、これはすべて API レベル 1 のものなので、どこでも動作するはずです。

public class EulaWebView extends WebView {
    public EulaWebView(Context context) 
    {
        this(context, null);
    }

    public EulaWebView(Context context, AttributeSet attrs) 
    {
        this(context, attrs, 0);
    }

    public EulaWebView(Context context, AttributeSet attrs, int defStyle) 
    {
        super(context, attrs, defStyle);
    }

    public OnBottomReachedListener mOnBottomReachedListener = null;
    private int mMinDistance = 0;

    /**
     * Set the listener which will be called when the WebView is scrolled to within some
     * margin of the bottom.
     * @param bottomReachedListener
     * @param allowedDifference
     */
    public void setOnBottomReachedListener(OnBottomReachedListener bottomReachedListener, int allowedDifference ) {
        mOnBottomReachedListener = bottomReachedListener;
        mMinDistance = allowedDifference;
    }

    /**
     * Implement this interface if you want to be notified when the WebView has scrolled to the bottom.
     */
    public interface OnBottomReachedListener {
        void onBottomReached(View v);
    }

    @Override
    protected void onScrollChanged(int left, int top, int oldLeft, int oldTop) {
        if ( mOnBottomReachedListener != null ) {
            if ( (computeVerticalScrollRange() - (top + getHeight())) <= mMinDistance )
                mOnBottomReachedListener.onBottomReached(this);
        }
        super.onScrollChanged(left, top, oldLeft, oldTop);
    }

}

これを使用して、ユーザーが WebView の一番下までスクロールしたら、「同意する」ボタンを表示します。ここで、次のように呼び出します (「OnBottomReachedListener を実装する」クラスで:

EulaWebView mEulaContent;
Button mEulaAgreed;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.eula);
    mEulaContent = (EulaWebView) findViewById(R.id.eula_content);
    StaticHelpers.loadWebView(this, mEulaContent, R.raw.stylesheet, StaticHelpers.readRawTextFile(this, R.raw.eula), null);
    mEulaContent.setVerticalScrollBarEnabled(true);
    mEulaContent.setOnBottomReachedListener(this, 50);

    mEulaAgreed = (Button) findViewById(R.id.eula_agreed);
    mEulaAgreed.setOnClickListener(this);
    mEulaAgreed.setVisibility(View.GONE);
}

@Override
public void onBottomReached(View v) {
    mEulaAgreed.setVisibility(View.VISIBLE);
}

そのため、最下部に到達すると (または、この場合は 50 ピクセル以内に到達すると)、「同意する」ボタンが表示されます。

于 2013-04-27T23:15:38.533 に答える
0

ロード中 / webview が下に到達 / スクロールしたときにのみボタンが表示されます。

JavaScript クラスを作成します。

public class JavaScriptInterface {

  @android.webkit.JavascriptInterface
  public void didScrollToBottom() {
    Log.d(TAG, "Scroll to Bottom");
    myHandler.post(new Runnable() {
      @Override
      public void run() {
         btnAccept.setVisibility(View.VISIBLE);

          }
       });
      }
    }

onCreate() で:

final JavaScriptInterface jsInterface = new JavaScriptInterface();
myWebView.addJavascriptInterface(jsInterface, "AndroidFunction");
于 2014-12-17T11:35:41.673 に答える
0

これを試して:

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        View view = (View) getChildAt(getChildCount()-1);
        int diff = (view.getBottom()-(getHeight()+getScrollY()));// Calculate the scrolldiff
        if( diff == 0 ){  // if diff is zero, then the bottom has been reached
            Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
            yourButton.setVisible(true);
        }
        super.onScrollChanged(l, t, oldl, oldt);
}

これを実装するには、ScrollView を拡張し、onScrollChanged メソッド (View から継承) をオーバーライドします。

于 2012-06-08T22:41:25.987 に答える
-1

上記の解決策は、同様の問題に対して完全には機能しませんでした(webViewのスクロール中にボタンを非表示にし、スクロールが終了した後に表示します)。スクロール中に非表示にしたい理由は、非表示にしたいボタンがwebviewの一番下にジャンプするためであり、webviewが静的なときにのみ機能したが、ビューがまだ表示されている間は下にジャンプしなかったためです。スクロールしました。だから私は次のことをしました:

近くに提案されているように、オーバーライドされた webView に onScrollChanged コールバックを追加しました。

private OnScrollChangedCallback mOnScrollChangedCallback;

public OnScrollChangedCallback getOnScrollChangedCallback() {
    return mOnScrollChangedCallback;
}

public void setOnScrollChangedCallback(
        final OnScrollChangedCallback onScrollChangedCallback) {
    mOnScrollChangedCallback = onScrollChangedCallback;
}

@Override
protected void onScrollChanged(final int l, final int t, final int oldl,
        final int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);
    if (mOnScrollChangedCallback != null){
        mOnScrollChangedCallback.onScrollChanged(l, t);
    }
}

/**
 * Implement in the activity/fragment/view that you want to listen to the
 * webview
 */
public static interface OnScrollChangedCallback {
    public void onScrollChanged(int l, int t);
}

そして、OnScrollChangedCallback を実装するアクティビティ クラスで

更新しました:

Timer timer2showJumpButton;
private long lastScrollEventTimestamp;
public final static int HIDING_JUMP_BUTTON_ON_SCROLL_DELAY  = 500;

    public void onScrollChanged(int l, int t) {

    // showing button when scrolling starts
    if (btnJumpToBottom != null) {
        btnJumpToBottom.setVisibility(View.VISIBLE);
    }

    if (btnJumpToTop!= null) {
        btnJumpToTop.setVisibility(View.VISIBLE);
    }


    if (timer2showJumpButton == null) {

        final Runnable r2 = new Runnable() {

            @Override
            public void run() {
                if (btnJumpToBottom != null) {
                    btnJumpToBottom.setVisibility(View.GONE);
                }
                if (btnJumpToTop!= null) {
                    btnJumpToTop.setVisibility(View.GONE);
                }

            }
        };

        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                if (btnJumpToTop.getVisibility() == View.VISIBLE || btnJumpToBottom.getVisibility() == View.VISIBLE){
                    long currentTimestamp = System.currentTimeMillis();

                    if (currentTimestamp - lastScrollEventTimestamp > HIDING_JUMP_BUTTON_ON_SCROLL_DELAY1 ){                        
                        webView.postDelayed(r2, HIDING_JUMP_BUTTON_ON_SCROLL_DELAY);
                    }else{
                        //too soon
                    }
                }
            }
        };

        try {
            timer2showJumpButton = new Timer();
            timer2showJumpButton.schedule(timerTask, 500, 500);
        } catch (IllegalStateException e) {
            logger.warn(TAG + "/onScrollChanged/" + e.getMessage());

        }   
    }

    // adding runnable which will hide button back
    long currentTimestamp = System.currentTimeMillis();

    lastScrollEventTimestamp = currentTimestamp;
}
于 2014-10-08T12:16:38.820 に答える