1

首先我創建了所有的佈局,然後我將它們與按鈕鏈接起來。當我在Fragment中創建導航菜單時,我創建了另一個類來擴展Fragment並將它們鏈接到我創建的佈局。當我點擊片段導航菜單顯示,但主頁不隱藏。我試圖通過在線搜索解決,我喜歡YouTube上的視頻教程,但仍然沒有解決。主頁沒有隱藏在片段的可見性

MainActivity.java

@SuppressLint("NewApi") 
public class MainActivity extends Activity { 
    private DrawerLayout mDrawerLayout; 
    private ListView mDrawerList; 
    private ActionBarDrawerToggle mDrawerToggle; 
    private CharSequence mDrawerTitle; 
    private CharSequence mTitle; 
    CustomDrawerAdapter adapter; 
    List<DrawerItem> dataList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button btnNextScreen = (Button) findViewById(R.id.button1); 
     btnNextScreen.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View arg0) { 
       Intent nextScreen = new Intent(getApplicationContext(), Layout1.class); 
       startActivity(nextScreen); 
      } 
     }); 
     Button btnNextScreen2 = (Button) findViewById(R.id.button2); 
     btnNextScreen2.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View arg0) { 
       Intent nextScreen = new Intent(getApplicationContext(), Layout2.class); 
       startActivity(nextScreen); 

      } 
     }); 


     // Initializing 
     dataList = new ArrayList<DrawerItem>(); 
     mTitle = mDrawerTitle = getTitle(); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.left_drawer); 

     mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, 
       GravityCompat.START); 

     // Add Drawer Item to dataList 
     dataList.add(new DrawerItem(true)); // adding a spinner to the list 

     dataList.add(new DrawerItem("Normal")); // adding a header to the list 
     dataList.add(new DrawerItem("Sign In", R.drawable.ic_action_email)); 
     dataList.add(new DrawerItem("Promotions",R.drawable.ic_action_good)); 

     dataList.add(new DrawerItem("The Best")); 
     dataList.add(new DrawerItem("Store",R.drawable.ic_action_search)); 
     dataList.add(new DrawerItem("Dish",R.drawable.ic_action_search)); 


     adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item, 
       dataList); 

     mDrawerList.setAdapter(adapter); 

     mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 

     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
       R.drawable.ic_drawer, R.string.drawer_open, 
       R.string.drawer_close) { 
      public void onDrawerClosed(View view) { 
       getActionBar().setTitle(mTitle); 
       invalidateOptionsMenu(); // creates call to 
              // onPrepareOptionsMenu() 
      } 

      public void onDrawerOpened(View drawerView) { 
       getActionBar().setTitle(mDrawerTitle); 
       invalidateOptionsMenu(); // creates call to 
              // onPrepareOptionsMenu() 
      } 
     }; 

     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     if (savedInstanceState == null) { 

      if (dataList.get(0).isSpinner() 
        & dataList.get(1).getTitle() != null) { 
       SelectItem(2); 
      } else if (dataList.get(0).getTitle() != null) { 
       SelectItem(1); 
      } else { 
       SelectItem(0); 
      } 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    public void SelectItem(int possition) { 

     Fragment fragment = null; 
     Bundle args = new Bundle(); 
     switch (possition) { 

     case 2: 
      fragment = new FragmentLayout2(); 
      break; 
     case 3: 
      fragment = new FragmentLayout1(); 
      break; 
     case 4: 
      fragment = new FragmentTwo(); 
      break; 
     case 5: 
      fragment = new FragmentLayout2(); 
      break; 
     case 6: 
      fragment = new FragmentLayout1(); 
      break; 
     default: 
      break; 
     } 
     fragment.setArguments(args); 
     FragmentManager frgManager = getFragmentManager(); 
     frgManager.beginTransaction().replace(R.id.content_frame, fragment) 
       .commit(); 

     mDrawerList.setItemChecked(possition, true); 
     setTitle(dataList.get(possition).getItemName()); 
     mDrawerLayout.closeDrawer(mDrawerList); 
    } 

    private class DrawerItemClickListener implements 
      ListView.OnItemClickListener { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      if (dataList.get(position).getTitle() == null) { 
       SelectItem(position); 
      } 

     } 
    } 
} 

Layout1.java

public class Layout1 extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout1); 

     Button btnClose = (Button) findViewById(R.id.button1); 

     btnClose.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View arg0) { 
       finish(); 
      } 
     }); 

    } 

} 

Fragmentlayout1.java

@SuppressLint("NewApi") 
public class FragmentLayout1 extends Fragment{ 
    public FragmentLayout1() { 

    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.layout1, container, 
       false); 
     return view; 
    } 

} 

activity_main.xml中

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="tma.sdcbatch11.foodordering.MainActivity" > 

    <RelativeLayout 
     android:id="@+id/content_Relative" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 

       android:text="Home page" 
       android:textAppearance="?android:attr/textAppearanceLarge" /> 

     </FrameLayout> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="58dp" 
      android:layout_marginTop="88dp" 
      android:text="Button to layout 1" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/button1" 
      android:layout_below="@+id/button1" 
      android:layout_marginTop="28dp" 
      android:text="Button to layout 2" /> 

    </RelativeLayout> 

    <ListView 
     android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#ffff" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" /> 

</android.support.v4.widget.DrawerLayout> 

link my project:

我試圖想辦法來解決這個問題,爲4天,但仍然沒有結果,這是該集團的一個項目,其他成員也做了佈局,並與按鍵佈局將它們鏈接。任何人都可以給我一些想法嗎?

回答

0

活動用來代替片段

public void SelectItem(int possition) { 
     Intent intent; 
     switch (possition) { 

     case 2: 
      intent = new Intent(getApplicationContext(), Layout2.class); 
      startActivity(intent); 
      break; 
     case 3: 
      intent = new Intent(getApplicationContext(), Layout1.class); 
      startActivity(intent); 
      break; 
     case 5: 
      intent = new Intent(getApplicationContext(), Layout2.class); 
      startActivity(intent); 
      break; 
     case 6: 
      intent = new Intent(getApplicationContext(), Layout1.class); 
      startActivity(intent); 
      break; 
     default: 
      break; 
     } 

     mDrawerList.setItemChecked(possition, true); 
     setTitle(dataList.get(possition).getItemName()); 
     mDrawerLayout.closeDrawer(mDrawerList); 
    }