将抽屉设置为片段

时间:2016-10-13 12:19:59

标签: android android-fragments navigation-drawer

我想在多个活动中将SettingsDrawer设置为片段(在点击特定按钮后,settingsDrawer将显示)。设置抽屉的背景应该是z当前活动不幸地我试图设置片段静态(在活动xml中)并且还动态地。首先尝试我得到膨胀类片段错误,并动态地获取空指针。这是我的动态版本:

SettingsDrawer类片段

public class SettingsDrawer extends Fragment{
    Context mContext;
    ArrayList<SettingsDrawerItem> items;
    ListView listView;
    DrawerLayout mDrawerLayout;
    RelativeLayout content;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.settings_drawer_layout, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mContext=getContext();
        mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawerLayout);
        listView = (ListView) view.findViewById(R.id.settingsList);
        DrawListAdapter adapter = new DrawListAdapter(mContext, items);
        listView.setAdapter(adapter);
        content = (RelativeLayout)view.findViewById(R.id.drawerPane);
        openAndCollapseDrawerList();

    }
    public  SettingsDrawer() {
        mContext=getContext();
        items = new ArrayList<>();}
public void openAndCollapseDrawerList()
    {
        if (mDrawerLayout.isDrawerOpen(Gravity.LEFT))
            mDrawerLayout.closeDrawer(content);

    else
        mDrawerLayout.openDrawer(content);
    }

设置抽屉布局

<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">


    <RelativeLayout
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:id="@+id/drawerPane"
        android:layout_gravity="start">

        <ListView
            android:id="@+id/settingsList"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_below="@+id/logoBox"
            android:choiceMode="singleChoice"
            android:background="#ffffffff" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

活动我初始化片段的部分

private void initSettingsDrawer() {
        Button settingsDrawerButton =(Button)findViewById(R.id.settingsDrawer);
        settingsDrawerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = null;
                Class fragmentClass = null;
                fragmentClass = SettingsDrawer.class;
                try {
                    fragment = (Fragment) fragmentClass.newInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.activity_main, fragment).commit();
            }
        });
    }

这给了我一个nullPointer异常。

1 个答案:

答案 0 :(得分:1)

在SettingsDrawer中添加此代码

static SettingsDrawer fragment;
static SettingsDrawer getInstance(){
if(fragment==null){
    return new SettingsDrawer();
}else{
    return fragment;
}

并在initSettingsDrawer中:

fragment = SettingsDrawer.getInstance();