public class MainActivity extends SlidingFragmentActivity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); show(); } private void show() { //Set the layout file of the side sliding menu setBehindContentView(R.layout.menu); //Get the Fergment manager FragmentManager fragmentManager = getSupportFragmentManager(); // start the transaction FragmentTransaction transaction = fragmentManager.beginTransaction(); // commit the transaction transaction.replace(R.id.fragment_menu, new MyFragment(),"right").commit(); //Get the slidingMenu instance SlidingMenu slidingMenu = getSlidingMenu(); //Set the slide out direction mode slidingMenu.setMode(SlidingMenu.RIGHT); //Set the slide out position slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN); //set gradient slidingMenu.setFadeDegree(0.5f); //Set the remaining width of the Activity after sliding out slidingMenu.setBehindOffset(100); } /** * Umeng third party login */ public void UM(){ Toast.makeText(this, "Make third-party login", 1).show(); } }
public class MyFragment extends Fragment { private View view; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = View.inflate(getActivity(), R.layout.frgment_menu, null); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { TextView textView = (TextView) view.findViewById(R.id.textView); textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(), "Clicked on the slide menu", 0).show(); // Get the activity that the current Fragment is attached to MainActivity mainActivity = (MainActivity) getActivity (); // call the activity's method mainActivity.UM(); } }); super.onActivityCreated(savedInstanceState); } }
menu <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:id="@+id/fragment_menu" android:layout_height="match_parent" android:orientation="vertical" > </FrameLayout>
fragment_menu <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Side menu" /> </RelativeLayout>