我不能讓我的簡單的片段在選擇在我的抽屜式導航欄中的選項屏幕只是停留空白露面。抽屜式導航欄工作正常,我知道selectItem()
方法燒製 - 選擇一個項目,因爲drawerLayout.closeDrawer(listView);
被稱爲在selectItem()
方法結束後抽屜關閉。安卓:麻煩與FragmentManager不是取代的FrameLayout
這裏是我的MainActivity
public class MainActivity extends ActionBarActivity implements
OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
selectItem(position);
}
public void selectItem(int position) {
Fragment fragment = null;
Bundle args = new Bundle();
fragment = new BookStayFragment();
args.putString("randomString", "testing this string");
fragment.setArguments(args);
FragmentManager fragmentManager = getSupportFragmentManager();
//This line doesn't seem to be replacing mainContent fragment in activity_main ?
fragmentManager.beginTransaction().replace(R.id.mainContent, fragment).commit();
listView.setItemChecked(position, true);
setTitle(myAdapter.customerMainMenu[position]);
drawerLayout.closeDrawer(listView);
}
activity_main.xml中
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:background="#3f51b5"
android:divider="@null"
android:id="@+id/drawerList"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
>
</ListView>
</android.support.v4.widget.DrawerLayout>
簡單的F中的相關代碼ragment類我試圖將它放在@ + ID /搜索Maincontent
package com.example.hotelsystem;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class BookStayFragment extends Fragment{
TextView tvTest;
public static final String ITEM_NAME = "Testing fragment loading from nav drawer";
public BookStayFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.book_stay_fragment, container, false);
tvTest = (TextView) view.findViewById(R.id.book_stay_text);
tvTest.setText(getArguments().getString(ITEM_NAME));
return view;
}
}
使用這種XML文件
<?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="vertical" >
<TextView
android:id="@+id/book_stay_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
/>
</LinearLayout>
如果您嘗試將BookStayFragment添加到活動的onCreate中的佈局,是否有效? – stkent 2014-09-29 21:46:07
@stkent實際上似乎沒有工作否 - 在我的MainActivity中放置完全相同的線onCreate() – user3324984 2014-09-29 22:10:34
您能否顯示完整的活動代碼? – stkent 2014-09-29 22:11:44