我有一个webview,显示一个HTML文件.当用户在webview中滚动到此文件的底部时,我想要一个先前隐藏的按钮显示,然后用户可以按此按钮执行某些活动
我在iOS中做了类似的事情,我只是将委托设置为ViewController,只是将按钮设置为可见.我如何在Android上做类似的事情?我注意到iOS中没有回调方法.
编辑:现在,我有一个包含2个对象的活动:包含我的文本的webview和一个当前不可见的按钮.我希望我的活动在webview文本滚动到底部时收到一条消息,并使按钮可见
最佳答案
我必须自己这样做,以便在用户滚动到EULA底部后显示“我同意”按钮.律师,对吧?
原文链接:https://www.f2er.com/android/430624.html事实上,当你覆盖WebView(而不是像@JackTurky的答案中的ScrollView)时,你可以调用getContentHeight()来获取内容的高度,而不是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,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 ( (getContentHeight() - (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像素范围内时),出现“我同意”按钮.