2013-07-26 86 views

回答

2

你的意思是,當抽屜裏的物品被點擊時,你不想創建一個片段,而是想在某處設置一些佈局?我覺得這裏不方便,這不是最好的做法。你可以做的一件事是,你可以膨脹佈局,並以片段形式返回它,這實際上等同於設置佈局。所以你該做的是,在膨脹片段的onCreateView(..)方法的佈局,並返回如下圖所示:

// onCreateView of fragment 
@Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.YOUR_LAYOUT, container, false); 
      // set values for view components if necessary. 

      return rootView; 
} 
+0

@ user2182545,我的回答對您有用嗎? – Suji

+0

是的,謝謝你的回答。 – ternes3

+0

不只是謝謝..接受答案並給予投票! – Suji

0

是,通過保存FrameLayout參考:

FrameLayout frame = findViewById(R.id.content_frame); 

隨着類似於:

private class DrawerItemClickListener implements ListView.OnItemClickListener { 
    final FrameLayout frame; 
    final Context context; 

    public DrawerItemClickListener(FrameLayout f, Context c){ 
     this.frame = f; 
     this.context = c; 
    } 

    @Override 
    public void onItemClick(AdapterView parent, View view, int position, long id) { 
     View detail = getViewForOption(position); 
     frame.removeAllViews(); 
     frame.addView(detail); 
    } 

    private View getViewForOption(int position){ 
     //--inflate and return different detail views-- 
     switch(position){ 
     case 1: 
     return LayoutInflator.from(context).inflate(R.layout.detail_1,null,false); 
     case 2: 
     return LayoutInflator.from(context).inflate(R.layout.detail_2,null,false); 
     } 
    } 
}