2015-06-18 165 views
-2

我的應用程序有一個主屏幕,用戶可以點擊在主頁片段,這將打開導航抽屜一個可點擊的佈局,但我在使用的openDrawer(查看視圖)麻煩方法一旦佈局被點擊,讓抽屜打開。的Android:開放抽屜式導航欄上的佈局點擊

HomeFragment.java(其中可點擊佈局所在地)

public void onClick(View v) { 

       Toast toast = Toast.makeText(context, "Handle Drawer Opening Via This Event", Toast.LENGTH_LONG); 
       toast.show(); 
       FragmentDrawer fragDrawer = new FragmentDrawer(); 


       fragDrawer.openNavDrawer(); 

      } 

FragmentDrawer.java:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflating view layout 
    View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false); 
    recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList); 

    adapter = new NavigationDrawerAdapter(getActivity(), getData()); 
    recyclerView.setAdapter(adapter); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() { 
     @Override 
     public void onClick(View view, int position) { 
      drawerListener.onDrawerItemSelected(view, position); 
      mDrawerLayout.closeDrawer(containerView); 
     } 

     @Override 
     public void onLongClick(View view, int position) { 

     } 
    })); 



    return layout; 
} 


//This method is called from the click event in HomeFragment.java 
public void openNavDrawer(){ 

mDrawerLayout.openDrawer(Gravity.START); 

} 
+0

什麼樣的錯誤你有沒有遇到過? –

回答

0

我mDrawerLayout這裏實例化:

mDrawerLayout =(DrawerLayout)findViewById (R.id.my_drawer_layout);我的幻燈片狀態:

mSlideState = false;

,如果你需要知道的幻燈片菜單狀態(關閉,打開)。使用此代碼:

mDrawerLayout.setDrawerListener 
(new ActionBarDrawerToggle 
(this, mDrawerLayout, R.drawable.ic_menu_slide, 0, 0){ 
@Override 
public void onDrawerClosed 
(View drawerView) { 
super.onDrawerClosed 
(drawerView); 
mSlideState=false;//is Closed 
} 
@Override 
public void onDrawerOpened 
(View drawerView) { 
super.onDrawerOpened(drawerView); mSlideState=true;//is Opened }}); 

終於。您可以使用您的點擊事件是這樣的:

public void clickEventSlide(){ 

if(mSlideState){ 
mDrawerLayout.closeDrawer(Gravity.END); 
}else{ mDrawerLayout.openDrawer(Gravity.END); } 
} 
在我的情況

我的幻燈片菜單是在正確的(Gravity.END),但是如果你需要在左邊,試圖用Gravity.START

我希望解決了你的問題。