Last active
August 29, 2015 14:05
-
-
Save nein37/db201d906a70c6c9e901 to your computer and use it in GitHub Desktop.
マルチペインレイアウトを簡単に実装する方法 ref: http://qiita.com/nein37/items/d3a5e4b016a1251cfad7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_slidingpanel, container, false); | |
mSlidingPaneLayout = (SlidingPaneLayout) view.findViewById(R.id.sliding_pane_layout); | |
// パネルの状態に応じてメニュー制御を行う | |
mSlidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() { | |
@Override | |
public void onPanelSlide(View view, float v) { | |
} | |
@Override | |
public void onPanelOpened(View view) { | |
panelOpened(); | |
} | |
@Override | |
public void onPanelClosed(View view) { | |
panelClosed(); | |
} | |
}); | |
// 初回のみ、レイアウト状態に応じてメニュー制御を行う必要がある | |
mSlidingPaneLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { | |
@SuppressWarnings("deprecation") | |
@Override | |
public void onGlobalLayout() { | |
if (mSlidingPaneLayout.isSlideable()) { | |
// パネルがスライド可能な場合、状態に応じてメニュー変更 | |
if (mSlidingPaneLayout.isOpen()) { | |
panelOpened(); | |
} else { | |
panelClosed(); | |
} | |
} | |
// 初回のみわかれば良いのでリスナー解除 | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) | |
mSlidingPaneLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); | |
else | |
mSlidingPaneLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); | |
} | |
}); | |
// 以下略 | |
} | |
/** | |
* パネルが閉じられた時の処理 | |
*/ | |
private void panelClosed() { | |
// リスト側メニューを無効化、詳細側メニューを有効化 | |
setHasOptionsMenu(false); | |
if (getChildFragmentManager().findFragmentById(R.id.content) != null) { | |
getChildFragmentManager().findFragmentById(R.id.content).setHasOptionsMenu(true); | |
} | |
} | |
/** | |
* パネルが開かれた時の処理 | |
*/ | |
private void panelOpened() { | |
// リスト側メニューを有効化、詳細側メニューを有効化 | |
setHasOptionsMenu(true); | |
if (getChildFragmentManager().findFragmentById(R.id.content) != null) { | |
getChildFragmentManager().findFragmentById(R.id.content).setHasOptionsMenu(false); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<android.support.v4.widget.SlidingPaneLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/sliding_pane_layout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ListView | |
android:id="@android:id/list" | |
android:layout_width="280dp" | |
android:layout_height="wrap_content" /> | |
<FrameLayout | |
android:id="@+id/content" | |
android:layout_width="300dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@android:color/white" /> | |
</android.support.v4.widget.SlidingPaneLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SlidingPanelFragment extends Fragment { | |
SlidingPaneLayout mSlidingPaneLayout; | |
public SlidingPanelFragment() { | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_slidingpanel, container, false); | |
mSlidingPaneLayout = (SlidingPaneLayout) view.findViewById(R.id.sliding_pane_layout); | |
// 適当にリストビューの設定 | |
ListView listView = (ListView) mSlidingPaneLayout.findViewById(android.R.id.list); | |
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { | |
// 詳細側Fragmentの遷移処理 | |
DetailFragment fragment = new DetailFragment(); | |
// パラメータが必要な場合はsetArgumentsする | |
FragmentTransaction ft = getFragmentManager().beginTransaction(); | |
ft.replace(R.id.content, fragment); | |
ft.addToBackStack(null); | |
ft.commit(); | |
// パネルを閉じる | |
if (mSlidingPaneLayout.isSlideable() && mSlidingPaneLayout.isOpen()) { | |
mSlidingPaneLayout.closePane(); | |
} | |
} | |
}); | |
return view; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment