0
在橫向模式下,我的列表視圖出現在屏幕的左側,而右側是空白的,因爲它應該是。當用戶點擊一個項目時,細節顯示在屏幕的右側,但listivew消失。它看起來像是在肖像模式,但它顯然使用景觀xml佈局土地。在看了幾十個例子之後,我仍然無法獲得列表視圖與細節留在屏幕上。有人能指引我朝着正確的方向嗎?這裏是我的代碼:單擊景觀(雙窗格)時的列表視圖
BookDetailsActivity: 公共類BookDetailsActivity延伸活動{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//** Getting the orientation (Landscape or Portrait) of the screen
int orientation = getResources().getConfiguration().orientation;
//** Landscape Mode
if(orientation == Configuration.ORIENTATION_LANDSCAPE){
//setting the landscape layout for this activity
setContentView(R.layout.buy_tbfrag);
//get fragment manager for fragment related operations
FragmentManager fm = getFragmentManager();
//get fragment transaction object, which can add, move or replace a fragment
FragmentTransaction ft = fm.beginTransaction();
//getting the existing detailed fragment object, if it already exists.
//the fragment object is retrieved by its tag name
Fragment prevFrag = fm.findFragmentByTag("book.details");
//System.out.println("prevFrag = " + prevFrag);
//** Remove the existing detailed fragment object if it exists */
if(prevFrag!=null) {
ft.remove(prevFrag);
}
//instantiating the fragment BookDetailsFragment
BookDetailsFragment detailsFragment = new BookDetailsFragment();
//creating a bundle object to pass the data (clicked item's position)
//from this activity to fragment
Bundle b = new Bundle();
//setting the data to the bundle object from the Intent
b.putString("load the bundle objects);
...
//setting the bundle object to the fragment
detailsFragment.setArguments(b);
//adding the fragment to the fragment transaction
ft.add(R.id.details_fragment_container, detailsFragment,"book.details");
//add the fragment transaction to backstack
//ft.addToBackStack(null);
//Executing the transaction
ft.commit();
} else {
...
start Portrait mode code...
}
}
Buy_tbfrg XML代碼:
<LinearLayout
android:layout_width="200dip"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="3dip" >
<EditText
android:id="@+id/titleSearch"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:background="@drawable/rectangle_box"
android:hint="@string/hint"
android:padding="5dip"
android:textStyle="italic" />
<ListView
android:id="@android:id/list"
android:layout_width="200dip"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<FrameLayout
android:id="@+id/details_fragment_container"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="3.97" >
</FrameLayout>
</LinearLayout>
BookDetailsFragment: 公共類BookDetailsFragment擴展片段{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.book_details, null);
System.out.println("BookDetailsFragment executed");
//Defines the TextViews in R.layout.book_details
TextView bkTitle = (TextView)view.findViewById(R.id.bookTitle);
...
//Retrieve the bundle object passed from BuyFragTab
Bundle b = getArguments();
//Getting the item's clicked position and setting corresponding details
//First check the bundle to ensure the data has been passed
if (b != null) {
bkTitle.setText ("Title: " + b.getString("Selected_Title"));
...
}
return view;
}