在横向模式下,我的列表视图显示在屏幕左侧,而右侧显示为空白,应该如此。当用户单击某个项目时,详细信息显示在屏幕的右侧但是listivew消失。它似乎表现得像是在纵向模式,但它显然使用景观xml布局 - 土地。在查看了几十个示例后,我仍然无法将listview与详细信息一起保留在屏幕上。有人能指出我正确的方向吗?这是我的代码:
BookDetailsActivity: 公共类BookDetailsActivity扩展了Activity {
@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扩展Fragment {
@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;
}
答案 0 :(得分:1)
你使用装载机吗?在ListFragment或DetailFragment中启动或重新启动Loader时,请确保传递不同的数字(arg0)。