Share SDK 안의 사이드 슬라이딩 효과를 추상화합니다.

share SDK를 사용할 때 안의 사이드 슬라이딩 효과가 괜찮다는 것을 발견했다. 지금은 그것을 단독으로 추상화했다. 유일한 결함은 시작할 때마다 번개가 치는 것이다. 그리고 그것이 주는 DEMO도 이런 현상이 있다. 이 문제는 나중에 해결한다.
MainActivity:
package com.home.testslidingmenu;

import m.framework.ui.widget.slidingmenu.SlidingMenu;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;

public class MainActivity extends Activity {
	private SlidingMenu menu;
	private int orientation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		orientation = getResources().getConfiguration().orientation;
		menu = new SlidingMenu(this);
		menu.setMenuItemBackground(R.color.sliding_menu_item_down,
				R.color.sliding_menu_item_release);
		menu.setMenuBackground(R.color.sliding_menu_background);
		menu.setTtleHeight(cn.sharesdk.framework.utils.R.dipToPx(this, 44));
		menu.setBodyBackground(R.color.sliding_menu_body_background);
		menu.setShadowRes(R.drawable.sliding_menu_right_shadow);
		menu.setMenuDivider(R.drawable.sliding_menu_sep);
		menu.setAdapter(new MainAdapter(menu));
		menu.triggerItem(MainAdapter.GROUP_1, MainAdapter.ITEM_1);
		setContentView(menu);
	}

	/**
	 *      ,       ,        
	 */
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (orientation != newConfig.orientation) {
			orientation = newConfig.orientation;
			menu.refresh();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN
				&& !menu.isMenuShown()) {
			menu.showMenu();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}

MainAdapter:
package com.home.testslidingmenu;

import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.FrameLayout.LayoutParams;
import m.framework.ui.widget.slidingmenu.MenuAdapter;
import m.framework.ui.widget.slidingmenu.SlidingMenu;
import m.framework.ui.widget.slidingmenu.SlidingMenuItem;

/**
 *        。           、     、              。
 */
public class MainAdapter extends MenuAdapter {
	/**     */
	public static final int GROUP_1 = 1;
	/**     */
	public static final int GROUP_2 = 2;

	/**     */
	public static final int ITEM_1 = 1;
	/**     */
	public static final int ITEM_2 = 2;
	/**     */
	public static final int ITEM_3 = 3;
	/**     */
	public static final int ITEM_4 = 4;
	/**     */
	public static final int ITEM_5 = 5;
	/**     */
	public static final int ITEM_6 = 6;
	/**     */
	public static final int ITEM_7 = 7;
	/**     */
	public static final int ITEM_8 = 8;
	/**     */
	public static final int ITEM_9 = 9;
	/**     */
	public static final int ITEM_10 = 10;
	private SlidingMenu menu;
	private SlidingMenuItem curItem;

	public MainAdapter(SlidingMenu menu) {
		super(menu);
		this.menu = menu;
		initData();
	}

	private void initData() {
		setGroup(GROUP_1, "");
		setGroup(GROUP_2, "  ");

		SlidingMenuItem item = new SlidingMenuItem();
		item.id = ITEM_1;
		item.body = "Item1";
		setItem(GROUP_1, item);

		item = new SlidingMenuItem();
		item.id = ITEM_2;
		item.body = "Item2";
		setItem(GROUP_1, item);

		item = new SlidingMenuItem();
		item.id = ITEM_3;
		item.body = "Item3";
		setItem(GROUP_1, item);

		item = new SlidingMenuItem();
		item.id = ITEM_4;
		item.body = "Item4";
		setItem(GROUP_1, item);

		item = new SlidingMenuItem();
		item.id = ITEM_5;
		item.body = "Item5";
		setItem(GROUP_1, item);

		item = new SlidingMenuItem();
		item.id = ITEM_6;
		item.body = "Item6";
		setItem(GROUP_2, item);

		item = new SlidingMenuItem();
		item.id = ITEM_7;
		item.body = "Item7";
		setItem(GROUP_2, item);

		item = new SlidingMenuItem();
		item.id = ITEM_8;
		item.body = "Item8";
		setItem(GROUP_2, item);

		item = new SlidingMenuItem();
		item.id = ITEM_9;
		item.body = "Item9";
		setItem(GROUP_2, item);

		item = new SlidingMenuItem();
		item.id = ITEM_10;
		item.body = "Item10";
		setItem(GROUP_2, item);
	}

	/**
	 * dp   px
	 * 
	 * @param context
	 * @param dip
	 * @return
	 */
	private int dipToPx(Context context, int dip) {
		return (int) (dip * context.getResources().getDisplayMetrics().density + 0.5f);
	}

	/**
	 *           ,       。                       。
	 */
	public boolean onItemTrigger(SlidingMenuItem item) {
		if (curItem != null && curItem.equals(item) && item.group == GROUP_1) {
			return false;
		}
		curItem = item;
		switch (item.group) {
		case GROUP_1: {
			SlidingMenuPage page = null;
			switch (item.id) {
			case ITEM_1: {
				page = new Page1(menu);
			}
				break;
			case ITEM_2: {
				page = new Page2(menu);
			}
				break;
			case ITEM_3: {
				Toast.makeText(menu.getContext(), "3", Toast.LENGTH_SHORT)
						.show();
			}
				break;
			case ITEM_4: {
				Toast.makeText(menu.getContext(), "4", Toast.LENGTH_SHORT)
						.show();
			}
				break;
			case ITEM_5: {
				Toast.makeText(menu.getContext(), "5", Toast.LENGTH_SHORT)
						.show();
			}
				break;
			}
			if (page != null) {
				menu.setBodyView(page.getPage());
			}
		}
			break;
		case GROUP_2: {
			switch (item.id) {
			case ITEM_6: {
				Toast.makeText(menu.getContext(), "6", Toast.LENGTH_SHORT)
						.show();
			}
				break;
			case ITEM_7: {
				Toast.makeText(menu.getContext(), "7", Toast.LENGTH_SHORT)
						.show();
			}
				break;
			case ITEM_8: {
				Toast.makeText(menu.getContext(), "8", Toast.LENGTH_SHORT)
						.show();
			}
				break;
			case ITEM_9: {
				Toast.makeText(menu.getContext(), "9", Toast.LENGTH_SHORT)
						.show();
			}
				break;
			case ITEM_10: {
				Toast.makeText(menu.getContext(), "10", Toast.LENGTH_SHORT)
						.show();
			}
				break;
			}
			break;
		}
		}
		return false;
	}

	/**
	 *  “ ”  
	 */
	@Override
	public View getGroupView(int position, ViewGroup menu) {
		String text = getTitle(position);
		if (text == null || text.length() <= 0) {
			return new LinearLayout(menu.getContext());
		}

		TextView tvTitle = new TextView(menu.getContext());
		tvTitle.setBackgroundResource(R.drawable.sidebar_titlt_back);
		int dp_13 = dipToPx(menu.getContext(), 13);
		int dp_5 = dipToPx(menu.getContext(), 3);
		tvTitle.setPadding(dp_13, dp_5, dp_13, dp_5);
		tvTitle.setText(text);
		tvTitle.setGravity(Gravity.CENTER_VERTICAL);
		tvTitle.setTextColor(0xff999999);
		tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 13);
		tvTitle.setLayoutParams(new ScrollView.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		return tvTitle;
	}

	/**
	 *  “   ”
	 */
	@Override
	public View getItemView(SlidingMenuItem item, ViewGroup menu) {
		TextView tvItem = new TextView(menu.getContext());
		tvItem.setGravity(Gravity.CENTER_VERTICAL);
		tvItem.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
		tvItem.setTextColor(tvItem.getResources().getColorStateList(
				R.color.normal_gray_pressed_white));
		tvItem.setText(String.valueOf(item.body));
		tvItem.setCompoundDrawablesWithIntrinsicBounds(0, 0,
				R.drawable.pointer, 0);
		int dp_13 = dipToPx(menu.getContext(), 13);
		tvItem.setCompoundDrawablePadding(dp_13);
		tvItem.setPadding(dp_13, 0, dp_13, 0);
		int dp_52 = dipToPx(menu.getContext(), 52);
		tvItem.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, dp_52));
		return tvItem;
	}

}

Page1:
package com.home.testslidingmenu;

import m.framework.ui.widget.slidingmenu.SlidingMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class Page1 extends SlidingMenuPage implements OnClickListener {
	private ImageView backView;

	public Page1(final SlidingMenu menu) {
		super(menu);
		View pageView = getPage();
		backView = (ImageView) pageView.findViewById(R.id.page1_iv_back);
		backView.setOnClickListener(this);
	}

	@Override
	protected View initPage() {
		return LayoutInflater.from(menu.getContext()).inflate(R.layout.page1,
				null);
	}

	@Override
	public void onClick(View v) {
		if (v == backView) {
			if (menu.isMenuShown()) {
				menu.hideMenu();
			} else {
				menu.showMenu();
			}
		}
	}
}

Page2:
package com.home.testslidingmenu;

import m.framework.ui.widget.slidingmenu.SlidingMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class Page2 extends SlidingMenuPage implements OnClickListener {
	private ImageView backView;

	public Page2(final SlidingMenu menu) {
		super(menu);
		View pageView = getPage();
		backView = (ImageView) pageView.findViewById(R.id.page2_iv_back);
		backView.setOnClickListener(this);
	}

	@Override
	protected View initPage() {
		return LayoutInflater.from(menu.getContext()).inflate(R.layout.page2,
				null);
	}

	@Override
	public void onClick(View v) {
		if (v == backView) {
			if (menu.isMenuShown()) {
				menu.hideMenu();
			} else {
				menu.showMenu();
			}
		}
	}
}

SlidingMenuPage:
package com.home.testslidingmenu;

import android.view.View;
import m.framework.ui.widget.slidingmenu.SlidingMenu;

/**           */
public abstract class SlidingMenuPage {
	protected SlidingMenu menu;
	private View pageView;

	public SlidingMenuPage(SlidingMenu menu) {
		this.menu = menu;
		pageView = initPage();
	}

	protected abstract View initPage();

	/**
	 *      View  
	 * 
	 * @return
	 */
	public View getPage() {
		return pageView;
	}
}

page1.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/auth_title_back" >

        <ImageView
            android:id="@+id/page1_iv_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:background="@drawable/image_back" />

        <TextView
            android:id="@+id/page1_tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Item1"
            android:textColor="#FFF"
            android:textSize="20sp" />
    </RelativeLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="This is the first page" />

</RelativeLayout>

page2.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/auth_title_back" >

        <ImageView
            android:id="@+id/page2_iv_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:background="@drawable/image_back" />

        <TextView
            android:id="@+id/page2_tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Item2"
            android:textColor="#FFF"
            android:textSize="20sp" />
    </RelativeLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="This is the second page" />

</RelativeLayout>

colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="sliding_menu_item_down">#ff39393b</color>
    <color name="sliding_menu_item_release">#00000000</color>
    <color name="sliding_menu_background">#ff2c2c2e</color>
    <color name="sliding_menu_body_background">#fff5f5f5</color>
</resources>

drawable에서 imageback.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/returnback_normal" android:state_pressed="false"/>
    <item android:drawable="@drawable/returnback_press" android:state_pressed="true"/>
</selector>

color 중 normalgray_pressed_white.xml:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:state_pressed="true" android:color="#ffffffff" />
	<item android:state_focused="true" android:color="#ffffffff" />
	<item android:color="#ffb1b1b1" />
</selector>

사용할 jar:mframework.jar와ShareSDK-Core-2.3.2.jar

좋은 웹페이지 즐겨찾기