网上关于ResideMenu用法的教程很多,但基本上全是从Github上copy下来的,Gitbub上给出的了对应的demo,但是由于我的IDE原因吧,demo一直导入不成功。为此自己又捣鼓了一翻,终于运行成功ResideMenu的真容。
package com.example.residemenutest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import com.special.ResideMenu.ResideMenu;
import com.special.ResideMenu.ResideMenuItem;
public class MenuActivity extends FragmentActivity implements View.OnClickListener{
private ResideMenu resideMenu;
private MenuActivity mContext;
private ResideMenuItem itemHome;
private ResideMenuItem itemProfile;
private ResideMenuItem itemCalendar;
private ResideMenuItem itemSettings;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
setUpMenu();
changeFragment(new HomeFragment());
}
private void setUpMenu() {
// attach to current activity;
resideMenu = new ResideMenu(this);
resideMenu.setBackground(R.drawable.menu_background);
resideMenu.attachToActivity(this);
resideMenu.setMenuListener(menuListener);
// create menu items;
itemHome = new ResideMenuItem(this, R.drawable.icon_home, "Home");
itemProfile = new ResideMenuItem(this, R.drawable.icon_profile, "Profile");
itemCalendar = new ResideMenuItem(this, R.drawable.icon_calendar, "Calendar");
itemSettings = new ResideMenuItem(this, R.drawable.icon_settings, "Settings");
itemHome.setOnClickListener(this);
itemProfile.setOnClickListener(this);
itemCalendar.setOnClickListener(this);
itemSettings.setOnClickListener(this);
resideMenu.addMenuItem(itemHome);
resideMenu.addMenuItem(itemProfile);
resideMenu.addMenuItem(itemCalendar);
resideMenu.addMenuItem(itemSettings);
findViewById(R.id.title_bar_menu).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resideMenu.openMenu();
}
});
}
@Override
public void onClick(View view) {
if (view == itemHome){
changeFragment(new HomeFragment());
}else if (view == itemProfile){
changeFragment(new ProfileFragment());
}else if (view == itemCalendar){
changeFragment(new CalendarFragment());
}else if (view == itemSettings){
changeFragment(new SettingsFragment());
}
resideMenu.closeMenu();
}
private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {
@Override
public void openMenu() {
Toast.makeText(mContext, "Menu is opened!", Toast.LENGTH_SHORT).show();
}
@Override
public void closeMenu() {
Toast.makeText(mContext, "Menu is closed!", Toast.LENGTH_SHORT).show();
}
};
private void changeFragment(Fragment targetFragment){
resideMenu.clearIgnoredViewList();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_fragment, targetFragment, "fragment")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}
// What good method is to access resideMenu?
public ResideMenu getResideMenu(){
return resideMenu;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@android:color/white"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/layout_top">
<ImageView
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#2ea3fe"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="28dp"
android:layout_height="28dp"
android:background="@drawable/titlebar_menu_selector"
android:id="@+id/title_bar_menu"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="7dp"
android:text="RESideMenu DEMO"
android:textSize="24sp"
android:textColor="#999999"
android:layout_gravity="center"/>
</FrameLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#ebebeb"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main_fragment">
</FrameLayout>
</LinearLayout>
package com.example.residemenutest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.special.ResideMenu.ResideMenu;
/**
* User: special
* Date: 14-13-20
* Time: 下午1:33
* Mail: [email protected]
*/
public class HomeFragment extends Fragment {
private View parentView;
private ResideMenu resideMenu;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
parentView = inflater.inflate(R.layout.home, container, false);
setUpViews();
return parentView;
}
private void setUpViews() {
MenuActivity parentActivity = (MenuActivity) getActivity();
resideMenu = parentActivity.getResideMenu();
parentView.findViewById(R.id.btn_open_menu).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resideMenu.openMenu();
}
});
// add gesture operation's ignored views
FrameLayout ignored_view = (FrameLayout) parentView.findViewById(R.id.ignored_view);
resideMenu.addIgnoredView(ignored_view);
}
}