gpt4 book ai didi

android - 重复的 webview,我想在每个中加载相同的内容

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:35:08 28 4
gpt4 key购买 nike

如何在工作方式类似的分屏中显示两个 webview,我想复制一个 webview,我想同时滚动两个。谢谢

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">

<WebView
android:id="@+id/webView"
android:layout_width="150dp"
android:layout_height="fill_parent"
android:layout_weight="1"
>

</WebView>

<WebView
android:id="@+id/webView2"
android:layout_width="150dp"
android:layout_height="fill_parent"
android:layout_weight="1"></WebView>
</LinearLayout>

我试过了,但我想复制 webview。这是显示另一个,我必须再次加载 url。

我不想在第二个中加载 agin 我只想成为镜像。

最佳答案

试试这个,

  1. create layout file content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="9">

<com.pramod.webviewdemo.ObservableWebView
android:id="@+id/webViewOriginal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second View"/>


<WebView
android:id="@+id/webViewMirror"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"/>

</LinearLayout>
  1. Create Class ObservableWebView for handle event from originalWebview
public class ObservableWebView extends WebView
{
private OnScrollChangedCallback mOnScrollChangedCallback;

public ObservableWebView(final Context context)
{
super(context);
}

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

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

@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.onScroll(l, t);
}

public OnScrollChangedCallback getOnScrollChangedCallback()
{
return mOnScrollChangedCallback;
}

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

/**
* Impliment in the activity/fragment/view that you want to listen to the webview
*/
public static interface OnScrollChangedCallback
{
public void onScroll(int l, int t);
}
}
  1. Update MainActivity.java with,
    public class MainActivity extends AppCompatActivity {

private WebView webViewMirror;
private ObservableWebView webViewOriginal;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);

// initialize view
webViewOriginal = (ObservableWebView)findViewById(R.id.webViewOriginal);
webViewMirror = (WebView)findViewById(R.id.webViewMirror);

//set web settings for original
webViewOriginal.getSettings().setLoadsImagesAutomatically(true);
webViewOriginal.getSettings().setJavaScriptEnabled(true);
webViewOriginal.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

//set web settings for duplicate / mirroe
webViewOriginal.getSettings().setLoadsImagesAutomatically(true);
webViewOriginal.getSettings().setJavaScriptEnabled(true);
webViewOriginal.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

// set clients for webview
webViewOriginal.setWebViewClient(new WebClientForOriginal());
webViewMirror.setWebViewClient(new WebClientForMirror());

// add listener to listen scroll For original webview
webViewOriginal.setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback() {
public void onScroll(int x, int y) {
Log.d("MainActivity", "We Scrolled etc..." + " X " + x + " Y " + y);
webViewMirror.scrollTo(x,y);
}
});

//disabled touch event event.
webViewMirror.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}
});

// load urls
webViewOriginal.loadUrl("http://www.google.com");
webViewMirror.loadUrl("http://www.google.com");

}

/**
* Set web client for original web view
*/
private class WebClientForOriginal extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
webViewMirror.loadUrl(url);
return true;
}
}

/**
* Set web client for mirror web view
*/
private class WebClientForMirror extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}

关于android - 重复的 webview,我想在每个中加载相同的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38405966/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com