2012-02-27 15 views
2

我正在開發一個應用程序,這是非常相似,典型的新聞閱讀器應用程序。我在屏幕左側的一個片段中有Listview,如果我們處於橫向視圖中,右側顯示了所選項目的詳細信息。否則,詳細信息片段將顯示在其自己的活動中。我有這個工作很好。確定先前的活動是否能在當前方向處理多個片段

但是有一點需要注意:我只希望它表現在更大的屏幕尺寸這樣(大概只有藥片但是這可能會更改)。在較小的設備上,我只想看到Listview片段,並在其自己的活動中啓動細節片段,而不管方向如何。我也有這個工作。

我現在遇到的問題是在細節活動中,以及如何確定何時完成()並返回到之前的活動以並排顯示片段。我有以下的代碼,對大型設備的工作原理,但不允許較小的設備來觀看這一活動,因此細節片段都:

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

    if (getResources().getConfiguration().orientation == 
    Configuration.ORIENTATION_LANDSCAPE) { 
     finish(); 
     return; 
    } 

我如何能夠確定要回先前的活動將允許片段並排顯示?

回答

3

尤里卡!我的解決方案是爲詳細信息活動提供一個空白的備用佈局。我膨脹佈局後,我可以檢查,看看我期望的片段是否在佈局中,如果不是,那麼我完成()。

details_view.xml在/ RES /佈局:在/ RES /佈局 - [屏幕尺寸] 這一個是你不想要顯示的詳細信息任何屏幕尺寸

<?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="horizontal"> 
    <fragment 
     android:name="com.website.DetailsFragment" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:id="@+id/details_fragment" 
     android:layout_weight="75"> 
    </fragment> 
</LinearLayout> 

details_view.xml在橫向模式下自己的活動片段(在我的情況下或多或少的片劑)。

<?xml version="1.0" encoding="utf-8"?> 
<!-- Note: This file has an empty layout to notify 
    DetailsViewActivity that we should go back to landscape 
    and view the fragments side by side --> 
<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 
</LinearLayout> 

只需將空的details_view.xml文件放在任何佈局文件中,該佈局文件允許並排顯示片段。

最後的DetailsViewActivity退出,如果我們想通過側回去端的代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.details_view); 

    mDetailsView = (TopicViewFragment)getSupportFragmentManager() 
    .findFragmentById(R.id.details_fragment); 

    // We can handle the fragments side by side in the previous activity 
    // so lets go back there 
    if ((mDetailsView == null || !mDetailsView.isInLayout()) && 
    getResources().getConfiguration().orientation == 
    Configuration.ORIENTATION_LANDSCAPE) { 
    finish(); 
    return; 
    } 

我也喜歡數據的少一些重疊,但這種解決方案並不差。它只會強制您將一個額外的佈局文件複製到您具有並排碎片佈局文件的同一目錄中。真的不算太糟糕。

+1

酷。你可以回答你自己的問題並獲得積分。甚至還有一個徽章。 – Pedantic 2012-02-28 04:06:19

1

我認爲下面的工作沒有使用空的佈局。

package de.vogella.android.fragments; 

    import android.app.Activity; 
    import android.content.res.Configuration; 
    import android.os.Bundle; 
    import android.widget.TextView; 

    public class DetailActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Need to check if Activity has been switched to landscape mode 
    // If yes, finished and go back to the start Activity 
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     finish(); 
     return; 
    } 

    setContentView(R.layout.details_activity_layout); 
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     String s = extras.getString("value"); 
     TextView view = (TextView) findViewById(R.id.detailsText); 
     view.setText(s); 
    } 
} 

}