0


我想用导航抽屉创建一个应用程序。我已经使用了这个示例链接 https://developer.android.com/training/implementing-navigation/nav-drawer.html
但现在我有一个问题。是否可以更换Fragment with a Layout

4

2 回答 2

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;
}
于 2013-07-26T09:17:40.650 回答
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);
      }
    }
}
于 2013-07-26T09:27:29.780 回答