Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android UI實現SlidingMenu側滑菜單效果

Android UI實現SlidingMenu側滑菜單效果

編輯:關於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開源庫。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved