Dan*_*iel 5 android delegates scroll android-webview
我有一个webview,显示一个HTML文件.当用户在webview中滚动到此文件的底部时,我想要一个先前隐藏的按钮显示,然后用户可以按此按钮执行某些活动
我在iOS中做了类似的事情,我只是将委托设置为ViewController,只是将按钮设置为可见.我如何在Android上做类似的事情?我注意到iOS中没有回调方法.
编辑:现在,我有一个包含2个对象的活动:包含我的文本的webview和一个当前不可见的按钮.我希望我的活动在webview文本滚动到底部时收到一条消息,并使按钮可见
我必须自己这样做,以便在用户滚动到 EULA 底部后显示“我同意”按钮。律师吧?
事实上,当您覆盖 WebView(而不是 @JackTurky 的答案中的 ScrollView)时,您可以调用 computeVerticalScrollRange() 来获取内容的高度,而不是 getBottom() 返回可见底部并且没有用。
这是我的综合解决方案。据我所知,这都是 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);
}
}
Run Code Online (Sandbox Code Playgroud)
一旦用户滚动到 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);
}
Run Code Online (Sandbox Code Playgroud)
因此,当到达底部时(或者在这种情况下,当它们到达底部 50 像素以内时)会出现“我同意”按钮。
尝试这个:
@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);
}
Run Code Online (Sandbox Code Playgroud)
要实现此目的,请扩展 ScrollView,然后重写 onScrollChanged 方法(继承自 View)。
| 归档时间: |
|
| 查看次数: |
6017 次 |
| 最近记录: |