2 baidu 29663457 baidu_29663457 于 2015.07.25 10:06 提问

我想请大神帮我看看这个安卓开发的问题,我想实现自制侧滑菜单,然后我点击左边菜单按钮,界面不跳转啊

ackage com.wecampus.slidingmenu;

import com.imooc.slidingmenu.R;
import com.wecampus.slidingmenu.view.SlidingMenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;

public class MainActivity extends Activity
{

private SlidingMenu mLeftMenu ; 

@Override
protected void onCreate(Bundle savedInstanceState)
{
    requestWindowFeature(Window.FEATURE_NO_TITLE); // 无标题
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    mLeftMenu = (SlidingMenu) findViewById(R.id.id_menu);
}

public void toggleMenu(View view)
{
    mLeftMenu.toggle();
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}
图片说明


2个回答

baidu_29663457
baidu_29663457   2015.07.25 10:08
 package com.wecampus.slidingmenu.fragment;

import com.imooc.slidingmenu.R;
import com.wecampus.slidingmenu.MainActivity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;

public class LeftFragment extends FragmentActivity implements OnClickListener{

    private View mapView;
    private View liveView;
    private View moreView;
    private View lineView;
    private View collectionView;
    private View scheduleView;

    private Fragment mMap;
    private Fragment mLine;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public void onAttach(Fragment activity) {
        super.onAttachFragment(activity);
    }


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.left_menu, container, false);
        findViews(view);
        return view;
    }

    public void findViews(View view) {
        // TODO Auto-generated method stub
        mapView = view.findViewById(R.id.w_map);
        lineView = view.findViewById(R.id.w_line);

        mapView.setOnClickListener(this);
        lineView.setOnClickListener(this);
    }


     /*public void onDestroyView() {
          super.onDestroyView();
    }


     public void onDestroy() {
          super.onDestroy();
    }
*/

    public void onClick(View v) {

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        String title = null;
        /*switch (v.getId()) {
        case R.id.w_map: 
            MapFragment mapfragment=new  MapFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
            beginTransaction.add(R.id.id_menu, mapfragment);
            beginTransaction.addToBackStack(null);
            beginTransaction.commit();
            break;
        }
        */
        switch (v.getId()) {
        case R.id.w_map: // 校园地图
            if (mMap == null)
        {
            mMap = new MapFragment();
            transaction.add(R.id.content_frame, mMap);
        } else
        {
            transaction.show(mMap);
        }

            title = getString(R.string.map);
            break;
        case R.id.w_line:// 路线查询
            if (mLine == null)
            {
                mLine = new MapFragment();
                transaction.add(R.id.content_frame, mLine);
            } else
            {
                transaction.show(mLine);
            }

            title = getString(R.string.line);
            break;
        default:
            break;  



    }

        transaction.commit();


    }


    private void hideFragment(FragmentTransaction transaction) {
        // TODO Auto-generated method stub

    }


    }

Evankaka
Evankaka   Ds   Rxr 2015.07.25 12:04
Csdn user default icon
上传中...
上传图片
插入图片