重复webview,我想在每个加载相同

时间:2016-07-15 22:43:03

标签: android

如何在分屏中显示两个类似的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。这显示另一个,我必须再次加载网址。

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

4 个答案:

答案 0 :(得分:6)

试试这个,

  
      
  1. 创建布局文件content_main.xml
  2.   
<?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. 为来自ObservableWebView
  2. 的句柄事件创建课程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. 使用
  2. 更新MainActivity.java   
    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;
        }
    }
}

答案 1 :(得分:0)

同样的用例在我的项目中。我通过在列表视图中放置网页视图来实现它。它将滚动整个屏幕而不管webview。尝试一次,让我知道它是否适合你? 希望它能工作:) GlbMP

答案 2 :(得分:0)

我已将这种类型的视图用于水平滚动,其中有imageview和textview 希望这对你有用

 <LinearLayout
    android:id="@+id/rel_c_content"
  android:layout_width="wrap_content"
 android:layout_height="wrap_content"
  android:gravity="center"
   android:orientation="horizontal"
  android:padding="5dp" />

这是java代码

int i = 0;
    do {
        if (i >= arrayofRelated.size()) {
            return;
        }

        View view = getLayoutInflater().inflate(R.layout.related_content, null);
        final ImageView imageView = (ImageView) view.findViewById(R.id.img_rela);
        final TextView txt_relmname = (TextView) view.findViewById(R.id.text_relmname);
        imageView.setId(i);

        linearContent.addView(view);

        objChildBean = arrayofRelated.get(i);
        imageLoader.DisplayImage(Constant.SERVER_IMAGE_THUMB + objChildBean.getRelatedImage().toString(), imageView);
        txt_relmname.setText(objChildBean.getRelatedTitle().toString());

        <Here you can load url in webview>

        i++;
    } while (true);

答案 3 :(得分:0)

**我正在尝试这个...... **

根据需要将布局设置高度设置为webview

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"

 android:background="@color/colorAccent"
    android:weightSum="1">


    <WebView
        android:id="@+id/webView"
        android:layout_width="0dp"
        android:layout_height="1000dp"
     android:layout_weight="0.50"
        >

    </WebView>


    <WebView
        android:id="@+id/webView2"
        android:layout_width="0dp"
        android:layout_height="1000dp"
        android:layout_weight="0.50">

    </WebView>
    </LinearLayout>


</ScrollView>

= 尝试此FOr匹配父级我正在尝试修复高度而不是它的工作尝试更多匹配父级

 android:fillViewport="true"
     android:scrollbars="vertical"

<强>代码

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends AppCompatActivity {
    private WebView webView1,webView2;
    private static final String TAG = "TAGGBOX";
    private ProgressDialog progressBar;
    // check secret prefix
    String msg="";

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

    WebSettings settings = webView1.getSettings();
    WebSettings settings1 = webView2.getSettings();
    settings.setJavaScriptEnabled(true);
    settings1.setJavaScriptEnabled(true);
    //ws.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings1.setJavaScriptCanOpenWindowsAutomatically(true);





//        /wcc = new MyWebChromeClient();


    webView1.setScrollContainer(false);
    webView2.setScrollContainer(false);
    webView1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        return (event.getAction() == MotionEvent.ACTION_MOVE);
        }
    });

    webView2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        return (event.getAction() == MotionEvent.ACTION_MOVE);
        }
    });


    webView1.setVerticalScrollBarEnabled(false);
    webView1.setHorizontalScrollBarEnabled(false);

    webView2.setVerticalScrollBarEnabled(false);
    webView2.setHorizontalScrollBarEnabled(false);


    webView1.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    webView2.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);


    webView1.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.i(TAG, "Processing webview url click..."+url);
        view.loadUrl(url);

        return true;
        }


        public void onPageFinished(WebView view, String url) {
        Log.e(TAG, "Finished loading URL: " + url);


        //webview.reload();
        }


        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
          /*  Log.e(TAG, "Error: " + description);
        Toast.makeText(MainActivity.this, "Please Check the Internet Connection", Toast.LENGTH_SHORT).show();
        alertDialog.setTitle("Error");
        alertDialog.setMessage("Please Check the Internet Connection");
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            return;
            }
        });
        alertDialog.show();*/


        }
    });

    webView1.loadUrl("http://stackoverflow.com/");


    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.


    webView2.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.i(TAG, "Processing webview url click..."+url);
        view.loadUrl(url);

        return true;
        }


        public void onPageFinished(WebView view, String url) {
        Log.e(TAG, "Finished loading URL: " + url);


        //webview.reload();
        }


        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
          /*  Log.e(TAG, "Error: " + description);
        Toast.makeText(MainActivity.this, "Please Check the Internet Connection", Toast.LENGTH_SHORT).show();
        alertDialog.setTitle("Error");
        alertDialog.setMessage("Please Check the Internet Connection");
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            return;
            }
        });
        alertDialog.show();*/


        }
    });

    webView2.loadUrl("http://stackoverflow.com/");


    }



    private void SetypUI() {
    webView1 = (WebView) findViewById(R.id.webView);
    webView2 = (WebView) findViewById(R.id.webView2);
    }



}