navigation open我构建了在网络视图中打开不同网站的Android应用程序,但是当我打开沃尔玛网站时,我在网站导航栏上遇到问题,当我向下滚动我的网站导航栏时,它不会在顶部显示它#s; s perfrom刷卡刷新。 Scroll for going on top of navigation top but not scroll going swipe to refresh
我的xml文件是: -
`<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_below="@+id/progressBar"
android:layout_height= "match_parent">
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>`
我的java代码是: -
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
Context context;
Toolbar toolbar;
ProgressBar progressBar;
SwipeRefreshLayout mySwipeRefreshLayout;
WebSettings settings;
RelativeLayout relativeLayout;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClientDemo());
myWebView.setWebChromeClient(new WebChromeClientDemo());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("https://www.walmart.com/");
myWebView.getSettings().setDomStorageEnabled(true);
mySwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
myWebView.reload();
}
}
);
mySwipeRefreshLayout.setNestedScrollingEnabled(false);
抱歉英语不好
提前谢谢你。
答案 0 :(得分:0)
步骤1:你必须看到滚动位置,如果它被完全向下滑动,即当y位置为零时刷新。
第2步:您可以使用canChileScrollUp()方法
来完成此操作这是一个自定义的refreshlayout,它有一个函数“canChildScrollUp”检测天气是否刷新,如果返回false则不会刷新。
“canSwipeRefreshChildScrollUp”是您将在活动中编写逻辑的地方。在你的情况下使用自定义webview并检测滚动位置,如果滚动完成,则只返回true
public class CustomSwipeRefreshLayout extends SwipeRefreshLayout {
private CanChildScrollUpCallback mCanChildScrollUpCallback;
public interface CanChildScrollUpCallback {
boolean canSwipeRefreshChildScrollUp();
}
public void setCanChildScrollUpCallback(CanChildScrollUpCallback canChildScrollUpCallback) {
mCanChildScrollUpCallback = canChildScrollUpCallback;
}
@Override
public boolean canChildScrollUp() {
if (mCanChildScrollUpCallback != null) {
return mCanChildScrollUpCallback.canSwipeRefreshChildScrollUp();
}
return super.canChildScrollUp();
}
}
在您的活动中
public class YourActivity implements CustomSwipeRefreshLayout.CanChildScrollUpCallback {
private CustomSwipeRefreshLayout mRefreshLayout;
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// after initialization
mRefreshLayout.setCanChildScrollUpCallback(this);
}
@Override
public boolean canSwipeRefreshChildScrollUp() {
return mWebview.getScrollY() > 0;
}
}
要检测嵌套滚动,您可以使用此
public class MyWebView extends WebView{
@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
if(clampedX || clampedY){
//Content is not scrolling
//TODO Enable SwipeRefreshLayout
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getActionMasked()==MotionEvent.ACTION_UP){
//TODO Disable SwipeRefreshLayout
}
}
}