繁体   English   中英

如何使用webviewfragment更改方向时保存webview的状态

[英]how can i save the state of my webview when changing orientation using a webviewfragment

我的一个布局中有一个小的webView,作为我网站的新闻源。每次我改变轮换它会重新加载网页。

所以我编辑了代码以包含配置更改和恢复状态。然后调整我的Android清单,以包括配置更改,从开发站点说包括方向和屏幕大小。

这解决了保存状态的问题,但反过来又产生了一个新问题,即我在某些设备上以横向模式设置了不同大小的布局。 但是使用清单设置它是如何覆盖屏幕大小的。 是在那里保存状态,但没有在我的清单中使用屏幕大小。 那么我可以使用我的景观布局

这是我的方向更改代码。

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_homescreen);
    if (savedInstanceState != null)
        ((WebView)findViewById(R.id.neweview)).restoreState(savedInstanceState);
    svHomeScreen = (ScrollView) findViewById(R.id.svHomeScreen);
    svHomeScreen.setScrollbarFadingEnabled(false);

    wv = (WebView) findViewById(R.id.neweview
    );
    wv.setScrollbarFadingEnabled(false);

    wv.getSettings().setJavaScriptEnabled(true);
    wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    wv.getSettings().setRenderPriority(WebSettings.RenderPriority.NORMAL);
    wv.getSettings().setLoadsImagesAutomatically(true);
    wv.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

    if (Build.VERSION.SDK_INT >= 19) {
        // chromium, enable hardware acceleration
        wv.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    } else {
        // older android version, disable hardware acceleration
        wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    wv.canGoBackOrForward(5);
    wv.getSettings().setLoadWithOverviewMode(true);
    wv.getSettings().setUseWideViewPort(true);
    wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);


    wv.setPadding(0, 0, 0, 0);
    wv.loadUrl(NEWS);

然后我的清单文件了

 <activity
        android:name=".Homescreen"
        android:label="@string/title_activity_homescreen"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        />

我添加了一个容器和一个Web视图片段,但现在当旋转是横向时视图消失了。 当我换回时,然后完全消失。 所以我尝试添加保存输出状态和savestate到它加载webview时旋转到横向,但在回到肖像时崩溃的应用程序

下面是一些示例代码,它将WebView作为片段放在活动布局中。 在纵向模式下,视图居中,在横向中,它位于底部。 两个方向都使用相同的单个WebView实例。

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // load the fragment - we only need to do this when the activity is first created
        // - not on a recreation due to screen rotation, etc
        if (savedInstanceState == null) {
            FragmentManager fm = this.getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.container, new WebViewFragment());
            ft.commit();
        }

    }

    // this could go in a separate file
    public static class WebViewFragment extends Fragment {
        WebView mWebView;

        @Override
        public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
            View v = layoutInflater.inflate(R.layout.webviewfrag, null, false);
            this.mWebView = (WebView)v.findViewById(R.id.NewsFeedWbview);
            this.mWebView.loadUrl("https://google.com");
            return v;
        }

    }
}

res / layout / main.xml(默认/纵向模式)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <!-- container layout for the webview fragment
    - note the id for this view is the same in both this and the landscape version -->
   <FrameLayout
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="240dp"
   android:layout_centerVertical="true"
    />

</RelativeLayout>

res / layout-land / main.xml(landscape)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <!-- container layout for the webview fragment
    - note the id for this view is the same in both this and the portrait version -->
   <FrameLayout
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="100dp"
   android:layout_alignParentBottom="true"
   />

</RelativeLayout>

RES /布局/ webviewfrag.xml

<!-- make the WebView fill its container -->
    <WebView
      xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/NewsFeedWbview"
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
      />

我认为您可以通过在AndroidManifest.xml中为您的活动添加android:configChanges来解决所有问题,如下所示:

    <activity
        android:name=".activities.MyActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label=""
        android:theme="@style/MyTheme"
        android:parentActivityName=".activities.Home">
    </activity>

好的家伙,所以我怀疑它使用片段为Webview。 我制作了两个片段,一个叫做webivew,另一个叫做webviewfragmentlandscape。

然后在创建我加载了两个片段,但使景观一个看不见,直到设备进入使用我已经使用的配置更改方法的景观scape。 这是我的新代码,如果任何身体可以改善我所做的将是一个很大的帮助

这是在oncreate之前

 @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    View Sidepane = findViewById(R.id.NewsFeed);
    View Squarepane = findViewById(R.id.NewsFeed2);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();


        if (Sidepane.getVisibility() == View.VISIBLE) {
            Sidepane.setVisibility(View.GONE);}

        Squarepane.setVisibility(View.VISIBLE);


    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();

        if (Squarepane.getVisibility() == View.VISIBLE) {
            Squarepane.setVisibility(View.GONE);

            Sidepane.setVisibility(View.VISIBLE);


        }

这是在创造

 View Sidepane = findViewById(R.id.NewsFeed);
    View Squarepane = findViewById(R.id.NewsFeed2);

    Squarepane.setVisibility(View.GONE);
    Sidepane.setVisibility(View.VISIBLE);

这是肖像的片段

 <<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


   <WebView
   android:layout_width="match_parent"
   android:layout_height="240dp"
   android:id="@+id/NewsFeedWbview"
   android:layout_gravity="center_vertical"
   android:layout_alignParentBottom="true"
   android:layout_alignParentLeft="true"
   android:layout_alignParentStart="true">

   </WebView>
 </RelativeLayout>

最后是景观片段

 <?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">

   <WebView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/NewsFeedWbview2"

    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="bottom">

   </WebView>
 </LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM