woong's

Android ResideMenu 사용하기 본문

Develop/Android

Android ResideMenu 사용하기

dlsdnd345 2016. 2. 14. 18:30
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Android ResideMenu 사용하기


좋은 라이브러리가 있어 포스트를 쓰고 있습니다.

아이폰 ui 로 많이 사용하는 ui인데 , 코드도 깔끔하고 퍼포먼스도 좋아 사용 방법에 대해

포스트 해보려 합니다.


https://github.com/SpecialCyCi/AndroidResideMenu


 



1. 라이브러리 사용 준비


-home build.gradle repository 추가

1
2
3
 repositories {
        mavenCentral()
    }
cs


-app build.gradle dependency 추가 

1
2
3
4
5
6
ependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.specyci:residemenu:1.6+'
 
}
cs



2. 사용 방법


-위와 같이 라이브러리를 추가 하면 사용준비가 끝났습니다.


1. 라이브러리 구성

 - activity 안의 5개의 프래그먼트로 구성

 -activity에서 5개의 프래그먼트 관리


4개의 프래그먼트는 간단하게 구성된 프래그먼트이므로 따로 설명에 언급하지는 않겠습니다.


MainActivity


ResideMenu : ResideMenuItem 을 관리 , open / close 등 동작 관리

ResideMenuItem : 왼쪽/오른쪽 메뉴 구성


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
public class MainActivity extends FragmentActivity implements View.OnClickListener {
 
 
    private ResideMenu resideMenu;
    private MainActivity mContext;
    private ResideMenuItem itemHome;
    private ResideMenuItem itemProfile;
    private ResideMenuItem itemCalendar;
    private ResideMenuItem itemSettings;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
 
        initData();
 
        mContext = this;
        if( savedInstanceState == null )
            changeFragment(new HomeFragment());
    }
 
    /**
     * 데이터 초기화
     */
    private void initData(){
 
        setUpMenu();
    }
 
    /**
     * 메뉴 구성
     */
    private void setUpMenu() {
 
        // attach to current activity;
        resideMenu = new ResideMenu(this);
        resideMenu.setBackground(R.drawable.menu_background);
        resideMenu.attachToActivity(this);
        resideMenu.setMenuListener(menuListener);
        //valid scale factor is between 0.0f and 1.0f. leftmenu'width is 150dip.
        resideMenu.setScaleValue(0.6f);
 
        // 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.DIRECTION_LEFT);
        resideMenu.addMenuItem(itemProfile, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(itemCalendar, ResideMenu.DIRECTION_RIGHT);
        resideMenu.addMenuItem(itemSettings, ResideMenu.DIRECTION_RIGHT);
 
        // You can disable a direction by setting ->
        // resideMenu.setSwipeDirectionDisable(ResideMenu.DIRECTION_RIGHT);
 
        findViewById(R.id.title_bar_left_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
            }
        });
        findViewById(R.id.title_bar_right_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resideMenu.openMenu(ResideMenu.DIRECTION_RIGHT);
            }
        });
    }
 
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return resideMenu.dispatchTouchEvent(ev);
    }
 
    @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;
    }
 
}
cs


위 코드만 봐도 굉장히 간단하고 명확한것 같습니다.

메뉴 하나하나를 구성하고 메뉴에 추가 하고 이벤트 를 부여 하는정도 있것 같습니다.

메뉴 선택시 프래그먼트 변경 작업이 있습니다.


코드도 깔끔하고 , 섬세하게 메뉴를 여러개 추가해보면 스크롤이 생기는것까지 확인 할 수 있습니다.


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <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_left_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"/>
 
            <Button
                android:layout_width="28dp"
                android:layout_height="28dp"
                android:background="@drawable/titlebar_menu_selector"
                android:id="@+id/title_bar_right_menu"
                android:layout_gravity="right|center_vertical"
                android:layout_marginRight="10dp"/>
        </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>
 
cs


xml 도 단순히 레이아웃 프레임 작업만 되어 있습니다.

코드가 깔끔해서 따로 말씀드릴부분은 없는것 같습니다. 

샘플은 최상단 github 를 이용하시면 확인해 볼수 있습니다.


Comments