Android:导航抽屉和片段活动

时间:2015-04-08 12:44:37

标签: android android-fragments navigation-drawer

我使用导航抽屉expandablelist创建一个android应用程序,在我的Main中,我使用活动片段,但是当我从菜单中更改片段时,会显示这个片段但是我无法使用它,它在单击时关闭而不是展开列表

这是我的代码的一部分:

public class ProductsFragment extends Fragment {

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

    View rootView = inflater.inflate(R.layout.fragment_products, container, false);

    return rootView;
}




public class MainActivity extends FragmentActivity {

private DrawerLayout mDrawerLayout;
android.app.Fragment fragment = null;
ExpandableListView expListView;
HashMap<String, List<String>> listDataChild;
ExpandableListAdapter listAdapter;
List<String> listDataHeader;
GlobalConstant g;
static String saveDirectory = String.valueOf(Environment.getExternalStorageDirectory());
static String backgroundPath;
static ImageView layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setUpDrawer();
    creatDirectoryForApp();

    layout = (ImageView) findViewById(R.id.background);
    g = (GlobalConstant)getApplication();
    backgroundPath = saveDirectory + "/galaxyv2Img/" + g.getBackgroundImage() + ".jpg";
    Drawable background = Drawable.createFromPath(backgroundPath);
    layout.setBackgroundDrawable(background);

    DatabaseHelper myDb = new DatabaseHelper(this);
    SQLiteDatabase db = myDb.getReadableDatabase();
    System.out.println("apres création de la base");

}


private void setUpDrawer() {
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
    mDrawerLayout.setDrawerListener(mDrawerListener);
    expListView = (ExpandableListView) findViewById(R.id.list_slidermenu);
    prepareListData();
    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
    // setting list adapter
    expListView.setAdapter(listAdapter);
    fragment = new HomeFragment();
    //getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
    mDrawerLayout.closeDrawer(expListView);

    expListView.setOnChildClickListener(new OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            switch (groupPosition) {
                case 0:
                    switch (childPosition) {
                        case 0:
                            new MainActivity();
                            //System.out.println("btn pressed");
                            //fragment = new MercuryFragment();
                            break;
                        default:
                            break;
                    }
                    break;

                case 1:
                    switch (childPosition) {
                        case 0:
                            fragment = new ProductsFragment();
                            break;
                        case 1:
                            fragment = new ProductsFragment();
                            //fragment = new VenusFragment();
                            break;
                        case 2:
                            fragment = new ProductsFragment();
                            //fragment = new EarthFragment();
                            break;
                        default:
                            break;
                    }
                    break;

                case 2:
                    switch (childPosition) {
                        case 0:
                            //fragment = new MercuryFragment();
                            break;
                        case 1:
                            //fragment = new VenusFragment();
                            break;
                        case 2:
                            //fragment = new EarthFragment();
                            break;
                        default:
                            break;
                    }
                    break;

                default:
                    break;
            }
            getFragmentManager().beginTransaction().replace(R.id.drawer_layout, fragment).commit();
            mDrawerLayout.closeDrawer(expListView);
            return false;
        }
    });
}

View.OnClickListener homeOnclickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        if(mDrawerLayout.isDrawerOpen(expListView)){
            mDrawerLayout.closeDrawer(expListView);
        }else{
            mDrawerLayout.openDrawer(expListView);
        }
    }
};

private OnItemClickListener mDrawerItemClickedListener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

        switch(position){
            case 0:
                //fragment = new MercuryFragment();
                break;
            case 1:
                //fragment = new VenusFragment();
                break;
            case 2:
                //fragment = new EarthFragment();
                break;
            default:
                return;
        }

        //getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();

        mDrawerLayout.closeDrawer(expListView);
    }
};

// Catch the events related to the drawer to arrange views according to this
// action if necessary...
private DrawerListener mDrawerListener = new DrawerListener() {

    @Override
    public void onDrawerStateChanged(int status) {

    }

    @Override
    public void onDrawerSlide(View view, float slideArg) {

    }

    @Override
    public void onDrawerOpened(View view) {
    }

    @Override
    public void onDrawerClosed(View view) {
    }
};

你知道我的问题是什么吗?

0 个答案:

没有答案