2016-04-28 6 views
-1

Ich erstelle nur eine App mit Listview und Webview. Der Portrait-Modus funktioniert einwandfrei. Aber wenn ich das Telefon im Querformat (Android Studio Emulator) drehen, hat es unbekannt BufferLandscape-Modus kann nicht zwei Fragmente zeigen

04-28 14: 07: 42,822 2202-2216/shiweichen22gmail.q1 E/Oberfläche: getSlotFromBufferLocked: unbekannt Puffer das Fragment zeigt mit list-view und rechte Teil zeigt das Fragment mit web-view 0xab7926c0

ich nach links Teil wollen. Aber es zeigt nur das Fragment mit list-view. ich bereits android Ressourcenverzeichnis mit Querformat-Layout erstellen (in Android nicht gefunden, kann nur in dem Projekt gefunden)

Hier ist mein Code 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> 

Manifest. 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); 
     } 
    } 


} 

enter image description here

+0

Ihre Portait und Landschaft Layout in derselben Aktivität aufgeblasen? Wenn ja, fügen Sie bitte auch den Aktivitätscode hinzu. – USKMobility

+0

http://developer.android.com/training/basics/supporting-devices/screens.html – USKMobility

+0

Ich ändere nur einige Dateien umbenennen, falls es zu Verwirrung kommt. Ich denke, das Hauptproblem ist, dass der Emulator das Landschaftslayout nicht finden kann. – Albert

Antwort

1

Warum Sie zwei registrieren außer Kraft setzen Aktivität in Manifest? Der Name beider Layout-Dateien (Land + Port) muss identisch sein.

Auf diese Weise wählt Ihre Aktivität aus, welches Layout automatisch ausgewählt wird.

Wichtig: - Aber bevor Sie Ihre Layouts und das Hinzufügen von Hörern auf Ansichten in Aktivitätsklasse Hinzufügen von Referenz müssen Sie überprüfen, welche aktuelle Ausrichtung andere weise sie in Nullpointer Absturz führen wird, wie Sie anhand der zu ergreifen, wird versuchen, die ist nicht aufgeblasen.

Was müssen Sie tun: -

  • Entfernen Landschaft Aktivität von manifest
  • Name ändern Landschaftsbelegung wie Porträt und verschieben Sie sie Ressourcenverzeichnis Layout-Land-Ordner in.
  • Bevor Sie findviewbyId in der Aktivitätsklasse verwenden, müssen Sie prüfen, ob die aktuelle Ausrichtung Land oder Port ist.

Beispiel Arbeits-Code

Hauptaktivitätsklasse

public class HomeActivity erweitert 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/layout/home_activity.xml

<ListView 
    android:id="@+id/selector" 
    android:layout_width="200dp" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" > 
</ListView> 

res/Layout-land/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" /> 

Detektiervorrichtung oder ientation

res/Werte/layouts.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <bool name="is_portrait">true</bool> 
</resources> 

res/Werte-Land/layouts.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <bool name="is_portrait">false</bool> 
</resources> 

Extras zeigt Webinhalte mit Hilfe vonUniversalWebViewFragment

Laden von Webcontents in Fragment einfach diese Klasse fallen lassen in Ihrem Projekt

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/layout/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> 

Extra-Layout für youtube Fortschritt (nur für den Fall)

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

Ergebnis: - Ich in gesicherter Proxy bin Netzwerk wird es in offenen n schön aussehen et

masterdetail webview

+0

Vielen Dank. Aber wenn ich auf die Listenansicht klicke, hat die Webansicht den ganzen Bildschirm, anstatt mit der Listenansicht auf der linken Seite zu fragmentieren und mit der Webansicht auf der rechten Seite zu fragmentieren. Welche Art von Code sollte ich vor fingviewbyid hinzufügen – Albert

+0

siehe bearbeiten Ich habe Beispielcode hinzugefügt können Sie ändern, wie Sie möchten –

+0

Danke, es hilft wirklich viel. – Albert

-1

Try in Ihrer Aktivität außer Kraft zu setzen. wie 0,5 für beide

@Override 

public void onConfigurationChanged(Configuration newConfig) { 

    super.onConfigurationChanged(newConfig); 

} 
+0

Danke, aber nicht funktioniert. Das Landschaftslayout wurde erfolgreich erstellt, aber ich denke, der Emulator kann das Landschaftslayout nicht finden, wenn es ausgeführt wird. – Albert

+0

@Albert: Sie können Ihren Emulator in den Querformatmodus verschieben, indem Sie die Zifferntaste 9 drücken. –

+0

Wenn ich in den Landscape-Modus gehe, hat es nur ein Fragment (listview) statt zwei Fragmente (listview und webview) – Albert

0

Keine Notwendigkeit, zwei xml in Ihrem Fall zu behandeln nur beide und gibt Gewicht in der Haupttätigkeit hinzuzufügen. und prüfen Sie, ob Sie im Hochformat-Modus ist, dann ein Fragment ausblenden und wenn der Benutzer ist Landschaft sichtbar zweiten Fragment.

und eine weitere Sache, die Sie nicht Max Gewicht Max. Gewicht des ersten Geschlechts gesetzt sind. und die meisten imp eine Zeile in menifest.xml

android:configChanges="orientation|screenSize" /> 

und fügen Sie, wenn Sie einen Code tun wollen, wenn Ausrichtung ändern, dann können Sie onConfigurationChanged() -Methode

@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 
} 
Verwandte Themen