我只是使用listview和webview创建一个应用程序。肖像模式工作正常。 但是当我以横向模式(Android工作室模拟器)打开手机时,它有未知的缓冲区
04-28 14:07:42.822 2202-2216 / shiweichen22gmail.q1 E / Surface: getSlotFromBufferLocked:未知缓冲区:0xab7926c0
我想左侧部分可以显示带有list-view
的片段,右侧部分可以显示带有web-view
的片段。但它只显示list-view
的片段。
我已经创建了具有横向布局的android资源目录(它在android中找不到,只能在项目中找到)
这是我的代码
Portrait.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="shiweichen22gmail.q1.Portrait"
tools:showIn="@layout/activity_portrait">
<fragment
android:id ="@+id/fragment1"
class ="shiweichen22gmail.q1.List"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Landscape Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment1"
android:name="shiweichen22gmail.q1.List"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</fragment>
<FrameLayout
android:id="@+id/detail"
android:name="shiweichen22gmail.q1.Detail"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
清单。enter image description here XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shiweichen22gmail.q1">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<user-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Portrait"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Landscape"
android:label="@string/title_activity_landscape"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>
</manifest>
Portiat.java
package shiweichen22gmail.q1;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.FragmentManager;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
public class Portrait extends FragmentActivity implements List.OnSiteSelectedListener{
Detail web;
List list;
FragmentManager manager;
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
Log.e("On Config Change", "LANDSCAPE");
}else{
Log.e("On Config Change","PORTRAIT");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_portrait);
manager = getFragmentManager();
list = (List) getSupportFragmentManager().findFragmentById(R.id.fragment1);
list.setRefrence(this);
}
@Override
public void onSiteSelected(int i) {
// TODO Auto-generated method stub
web = (Detail) getSupportFragmentManager().findFragmentById(R.id.detail);
// Check for content_portrait mode
if (web!= null && web.isVisible())
{
web.setNewPage(i);
}
else
{
Intent intent = new Intent(this, Landscape.class);
intent.putExtra("index", i);
startActivity(intent);
}
}
}
答案 0 :(得分:1)
为什么要在清单中注册两项活动?两个布局文件(Land + Port)的名称必须相同。
这样您的活动就会选择自动选择的布局。
重要: - 但在添加对布局的引用并在活动类中的视图上添加侦听器之前,您必须检查当前的方向是否会导致NullPointer崩溃,因为您将尝试没有夸大的观点的参考。
您需要做什么: -
示例工作代码
主要活动类
公共类HomeActivity扩展了FragmentActivity {
private UniversalWebViewFragment detailFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
ListView listView = (ListView) findViewById(R.id.selector);
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
//Load random Google search URLS for List
list.add("https://www.google.com/search?q=" + i);
}
listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list));
if (!isPortrait(getApplicationContext())) {
detailFragment = (UniversalWebViewFragment) getSupportFragmentManager().findFragmentById(R.id.webView);
}
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (isPortrait(getApplicationContext())) {
Toast.makeText(getApplicationContext(), "Do Fragment Transition with URL" + list.get(arg2), 300).show();
} else {
if (null != detailFragment)
detailFragment.loadURL(list.get(arg2));
}
}
});
}
//Check if device is in portrait mode
public static boolean isPortrait(Context context) {
return context.getResources().getBoolean(R.bool.is_portrait);
}
}
RES /布局/ home_activity.xml
<ListView
android:id="@+id/selector"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true" >
</ListView>
RES /布局脊/ home_activity.xml
<ListView
android:id="@+id/selector"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true" >
</ListView>
<fragment
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/selector"
class="com.masterdetail_webview.UniversalWebViewFragment" />
检测设备方向
RES /值/ layouts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_portrait">true</bool>
</resources>
RES /值-脊/ layouts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_portrait">false</bool>
</resources>
借助 {strong> UniversalWebViewFragment
显示网页内容的其他内容在片段中加载webcontents只需将此类放在项目中
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
@SuppressLint("NewApi")
public class UniversalWebViewFragment extends Fragment {
public static final String WEB_URL_TO_LOAD = "webURLToLoad";
private static final String GOOGLE_SERACH_URL = "https://www.google.com/search?q=";
private WebView webView;
private FrameLayout customViewContainer;
private WebChromeClient.CustomViewCallback customViewCallback;
private View mCustomView;
private myWebChromeClient mWebChromeClient;
private myWebViewClient mWebViewClient;
public static UniversalWebViewFragment newInstance(String webUrl, boolean serachOnWeb) {
Bundle bdl = new Bundle();
if (serachOnWeb) {
// serach on google for query
bdl.putString(WEB_URL_TO_LOAD, GOOGLE_SERACH_URL + webUrl);
} else {
// seimply load url
bdl.putString(WEB_URL_TO_LOAD, webUrl);
}
UniversalWebViewFragment newInstance = new UniversalWebViewFragment();
newInstance.setArguments(bdl);
return newInstance;
}
/**
* Called when the activity is first created.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.universal_web_view, container, false);
// if (null != getArguments() && null !=
// getArguments().getString(WEB_URL_TO_LOAD)) {
customViewContainer = (FrameLayout) rootView.findViewById(R.id.customViewContainer);
webView = (WebView) rootView.findViewById(R.id.webView);
mWebViewClient = new myWebViewClient();
webView.setWebViewClient(mWebViewClient);
mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);
webView.getSettings().setJavaScriptEnabled(true);
// Important for PayUMoney
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSaveFormData(true);
loadURL(GOOGLE_SERACH_URL);
// webView.requestFocus();
// Handle Back keyPress
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
rootView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (inCustomView()) {
hideCustomView();
getActivity().getSupportFragmentManager().popBackStack();
return true;
}
if ((mCustomView == null) && webView.canGoBack()) {
webView.goBack();
getActivity().getSupportFragmentManager().popBackStack();
return true;
}
}
// return super.onKeyDown(keyCode, event);
return true;
}
});
// }
return rootView;
}
/**
*
*/
public void loadURL(String URl) {
webView.loadUrl(URl);
}
public boolean inCustomView() {
return (mCustomView != null);
}
public void hideCustomView() {
mWebChromeClient.onHideCustomView();
}
@Override
public void onPause() {
super.onPause(); // To change body of overridden methods use File |
// Settings | File Templates.
webView.onPause();
}
@Override
public void onResume() {
super.onResume(); // To change body of overridden methods use File |
// Settings | File Templates.
webView.onResume();
}
@Override
public void onStop() {
super.onStop(); // To change body of overridden methods use File |
// Settings | File Templates.
if (inCustomView()) {
hideCustomView();
}
}
class myWebChromeClient extends WebChromeClient {
private View mVideoProgressView;
@Override
public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
onShowCustomView(view, callback); // To change body of overridden
// methods use File | Settings |
// File Templates.
}
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomView = view;
webView.setVisibility(View.GONE);
customViewContainer.setVisibility(View.VISIBLE);
customViewContainer.addView(view);
customViewCallback = callback;
}
@Override
public View getVideoLoadingProgressView() {
if (mVideoProgressView == null) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
mVideoProgressView = inflater.inflate(R.layout.video_progress, null);
}
return mVideoProgressView;
}
@Override
public void onHideCustomView() {
super.onHideCustomView(); // To change body of overridden methods
// use File | Settings | File Templates.
if (mCustomView == null)
return;
webView.setVisibility(View.VISIBLE);
customViewContainer.setVisibility(View.GONE);
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
customViewContainer.removeView(mCustomView);
customViewCallback.onCustomViewHidden();
mCustomView = null;
}
}
class myWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
private int webViewPreviousState;
private final int PAGE_STARTED = 0x1;
private final int PAGE_REDIRECTED = 0x2;
Dialog dialog = new Dialog(getActivity());
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
webViewPreviousState = PAGE_STARTED;
if (dialog == null || !dialog.isShowing())
dialog = ProgressDialog.show(getActivity(), "", "Loading Please Wait", true, true,
new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// do something
}
});
}
@Override
public void onPageFinished(WebView view, String url) {
if (webViewPreviousState == PAGE_STARTED) {
if (null != dialog)
dialog.dismiss();
dialog = null;
}
}
}
}
RES /布局/ universal_web_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView"
android:layout_gravity="center"
/>
<FrameLayout
android:id="@+id/customViewContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
/>
</LinearLayout>
youtube进度的额外布局(以防万一)
res / video_progress
<ProgressBar android:id="@android:id/progress"
style="?android:attr/progressBarStyleLarge"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:paddingTop="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="loading"
android:textSize="14sp"
android:textColor="?android:attr/textColorPrimary"/>
结果: - 我在安全的代理网络中,在开放网络中看起来不错
答案 1 :(得分:0)
在您的情况下无需处理两个xml 只需在主要活动中添加两者,并为两者赋予0.5的重量。 并检查您是否是纵向模式然后隐藏一个片段,当用户是横向可见的第二个片段时。
还有一件事你没有设定最大重量第一性最大体重。 并且大多数imp在menifest.xml中添加一行
android:configChanges="orientation|screenSize" />
如果你想在方向改变时做一些代码,那么你可以覆盖onConfigurationChanged()方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// write here code
// here you can check orientation and set your code as per your requirement
}
答案 2 :(得分:-1)
尝试覆盖您的活动中的onConfigurationChanged()
。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}