編輯:關於Android編程
本篇博客給大家分享一個效果比較好的側滑菜單的Demo,實現點擊左邊菜單切換Fragment。
效果如下:
主Activity代碼:
package com.infzm.slidingmenu.demo; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.ImageView; import android.widget.TextView; import com.infzm.slidingmenu.demo.fragment.LeftFragment; import com.infzm.slidingmenu.demo.fragment.TodayFragment; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity; /** * @date 2014/11/14 * @author wuwenjie * @description 主界面 */ public class MainActivity extends SlidingFragmentActivity implements OnClickListener { private ImageView topButton; private Fragment mContent; private TextView topTextView; @Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); // 無標題 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initSlidingMenu(savedInstanceState); topButton = (ImageView) findViewById(R.id.topButton); topButton.setOnClickListener(this); topTextView = (TextView) findViewById(R.id.topTv); } /** * 初始化側邊欄 */ private void initSlidingMenu(Bundle savedInstanceState) { // 如果保存的狀態不為空則得到之前保存的Fragment,否則實例化MyFragment if (savedInstanceState != null) { mContent = getSupportFragmentManager().getFragment( savedInstanceState, "mContent"); } if (mContent == null) { mContent = new TodayFragment(); } // 設置左側滑動菜單 setBehindContentView(R.layout.menu_frame_left); getSupportFragmentManager().beginTransaction() .replace(R.id.menu_frame, new LeftFragment()).commit(); // 實例化滑動菜單對象 SlidingMenu sm = getSlidingMenu(); // 設置可以左右滑動的菜單 sm.setMode(SlidingMenu.LEFT); // 設置滑動陰影的寬度 sm.setShadowWidthRes(R.dimen.shadow_width); // 設置滑動菜單陰影的圖像資源 sm.setShadowDrawable(null); // 設置滑動菜單視圖的寬度 sm.setBehindOffsetRes(R.dimen.slidingmenu_offset); // 設置漸入漸出效果的值 sm.setFadeDegree(0.35f); // 設置觸摸屏幕的模式,這裡設置為全屏 sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); // 設置下方視圖的在滾動時的縮放比例 sm.setBehindScrollScale(0.0f); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); getSupportFragmentManager().putFragment(outState, "mContent", mContent); } /** * 切換Fragment * * @param fragment */ public void switchConent(Fragment fragment, String title) { mContent = fragment; getSupportFragmentManager().beginTransaction() .replace(R.id.content_frame, fragment).commit(); getSlidingMenu().showContent(); topTextView.setText(title); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.topButton: toggle(); break; default: break; } } }
側邊欄菜單Fragment
package com.infzm.slidingmenu.demo.fragment; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import com.infzm.slidingmenu.demo.MainActivity; import com.infzm.slidingmenu.demo.R; /** * @date 2014/11/14 * @author wuwenjie * @description 側邊欄菜單 */ public class LeftFragment extends Fragment implements OnClickListener{ private View todayView; private View lastListView; private View discussView; private View favoritesView; private View commentsView; private View settingsView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onAttach(Activity activity) { super.onAttach(activity); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_menu, null); findViews(view); return view; } public void findViews(View view) { todayView = view.findViewById(R.id.tvToday); lastListView = view.findViewById(R.id.tvLastlist); discussView = view.findViewById(R.id.tvDiscussMeeting); favoritesView = view.findViewById(R.id.tvMyFavorites); commentsView = view.findViewById(R.id.tvMyComments); settingsView = view.findViewById(R.id.tvMySettings); todayView.setOnClickListener(this); lastListView.setOnClickListener(this); discussView.setOnClickListener(this); favoritesView.setOnClickListener(this); commentsView.setOnClickListener(this); settingsView.setOnClickListener(this); } @Override public void onDestroyView() { super.onDestroyView(); } @Override public void onDestroy() { super.onDestroy(); } @Override public void onClick(View v) { Fragment newContent = null; String title = null; switch (v.getId()) { case R.id.tvToday: // 今日 newContent = new TodayFragment(); title = getString(R.string.today); break; case R.id.tvLastlist:// 往期列表 newContent = new LastListFragment(); title = getString(R.string.lastList); break; case R.id.tvDiscussMeeting: // 討論集會 newContent = new DiscussFragment(); title = getString(R.string.discussMeetting); break; case R.id.tvMyFavorites: // 我的收藏 newContent = new MyFavoritesFragment(); title = getString(R.string.myFavorities); break; case R.id.tvMyComments: // 我的評論 newContent = new MyCommentsFragment(); title = getString(R.string.myComments); break; case R.id.tvMySettings: // 設置 newContent = new MySettingsFragment(); title = getString(R.string.settings); break; default: break; } if (newContent != null) { switchFragment(newContent, title); } } /** * 切換fragment * @param fragment */ private void switchFragment(Fragment fragment, String title) { if (getActivity() == null) { return; } if (getActivity() instanceof MainActivity) { MainActivity fca = (MainActivity) getActivity(); fca.switchConent(fragment, title); } } }
上面是核心代碼,引入SlidingMenu開源庫。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
在用Android Studio開發的過程中,一遇到廢棄、不被推薦的方法和類,我就想做點什麼去掉上面的橫線。然後,被一個不是問題的問題困擾了很久。之前我們在創建固定Tab
本文主要講解局部加權(線性)回歸。在講解局部加權線性回歸之前,先講解兩個概念:欠擬合、過擬合,由此引出局部加權線性回歸算法。 欠擬合、過擬合如下圖中三個擬合模型
一、概述運行時變更就是設備在運行時發生變化(例如屏幕旋轉、鍵盤可用性及語言)。發生這些變化,Android會重啟Activity,這時就需要保存activity的狀態及與
今天因為要做一個設置開機畫面的功能,主要是讓用戶可以設置自己的開機畫面,應用層需要做讓用戶選擇開機畫面圖片的功能。所以需要做一個簡單的圖片浏覽選擇程序。最後選用Galle