Android学习教程之分类侧滑菜单(5)

Android学习教程之分类侧滑菜单(5),第1张

概述本文实例为大家分享了Android分类侧滑菜单的制作方法,供大家参考,具体内容如下

本文实例为大家分享了AndroID分类侧滑菜单的制作方法,供大家参考,具体内容如下

classificmenuActivity.java代码:

package com.siso.crazyworld;import androID.animation.Animator;import androID.content.res.Configuration;import androID.graphics.color;import androID.graphics.drawable.BitmapDrawable;import androID.os.Bundle;import androID.support.v4.Widget.DrawerLayout;import androID.support.v7.app.ActionBaractivity;import androID.support.v7.app.ActionbarDrawerToggle;import androID.support.v7.Widget.Toolbar;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.vIEw.VIEwAnimationUtils;import androID.vIEw.animation.AccelerateInterpolator;import androID.Widget.linearLayout;import com.siso.crazyworld.fragment.ContentFragment;import java.util.ArrayList;import java.util.List;import sIDemenu.interfaces.Resourceble;import sIDemenu.interfaces.ScreenShotable;import sIDemenu.model.SlIDeMenuItem;import sIDemenu.util.VIEwAnimator;public class classificmenuActivity extends ActionBaractivity implements VIEwAnimator.VIEwAnimatorListener { private DrawerLayout drawerLayout; private ActionbarDrawerToggle drawerToggle; private List<SlIDeMenuItem> List = new ArrayList<>(); private ContentFragment contentFragment; private VIEwAnimator vIEwAnimator; private int res = R.drawable.content_music; private linearLayout linearLayout; @OverrIDe protected voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.activity_classificmenu);  contentFragment = ContentFragment.newInstance(R.drawable.content_music);  getSupportFragmentManager().beginTransaction()    .replace(R.ID.content_frame,contentFragment)    .commit();  drawerLayout = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);  drawerLayout.setScrimcolor(color.transparent);  linearLayout = (linearLayout) findVIEwByID(R.ID.left_drawer);  linearLayout.setonClickListener(new VIEw.OnClickListener() {   @OverrIDe   public voID onClick(VIEw v) {    drawerLayout.closeDrawers();   }  });  setActionbar();  createMenuList();  vIEwAnimator = new VIEwAnimator<>(this,List,contentFragment,drawerLayout,this); } private voID createMenuList() {  SlIDeMenuItem menuItem0 = new SlIDeMenuItem(ContentFragment.CLOSE,R.drawable.icn_close);  List.add(menuItem0);  SlIDeMenuItem menuItem = new SlIDeMenuItem(ContentFragment.BUILDING,R.drawable.icn_1);  List.add(menuItem);  SlIDeMenuItem menuItem2 = new SlIDeMenuItem(ContentFragment.BOOK,R.drawable.icn_2);  List.add(menuItem2);  SlIDeMenuItem menuItem3 = new SlIDeMenuItem(ContentFragment.PAINT,R.drawable.icn_3);  List.add(menuItem3);  SlIDeMenuItem menuItem4 = new SlIDeMenuItem(ContentFragment.CASE,R.drawable.icn_4);  List.add(menuItem4);  SlIDeMenuItem menuItem5 = new SlIDeMenuItem(ContentFragment.SHOP,R.drawable.icn_5);  List.add(menuItem5);  SlIDeMenuItem menuItem6 = new SlIDeMenuItem(ContentFragment.PARTY,R.drawable.icn_6);  List.add(menuItem6);  SlIDeMenuItem menuItem7 = new SlIDeMenuItem(ContentFragment.MOVIE,R.drawable.icn_7);  List.add(menuItem7); } private voID setActionbar() {  Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar);  setSupportActionbar(toolbar);  getSupportActionbar().setHomebuttonEnabled(true);  getSupportActionbar().setdisplayHomeAsUpEnabled(true);  drawerToggle = new ActionbarDrawerToggle(    this,/* host Activity */    drawerLayout,/* DrawerLayout object */    toolbar,/* nav drawer icon to replace 'Up' caret */    R.string.drawer_open,/* "open drawer" description */    R.string.drawer_close /* "close drawer" description */  ) {   /** Called when a drawer has settled in a completely closed state. */   public voID onDrawerClosed(VIEw vIEw) {    super.onDrawerClosed(vIEw);    linearLayout.removeAllVIEws();    linearLayout.invalIDate();   }   @OverrIDe   public voID onDrawerSlIDe(VIEw drawerVIEw,float slIDeOffset) {    super.onDrawerSlIDe(drawerVIEw,slIDeOffset);    if (slIDeOffset > 0.6 && linearLayout.getChildCount() == 0)     vIEwAnimator.showMenuContent();   }   /** Called when a drawer has settled in a completely open state. */   public voID onDrawerOpened(VIEw drawerVIEw) {    super.onDrawerOpened(drawerVIEw);   }  };  drawerLayout.setDrawerListener(drawerToggle); } @OverrIDe protected voID onPostCreate(Bundle savedInstanceState) {  super.onPostCreate(savedInstanceState);  drawerToggle.syncState(); } @OverrIDe public voID onConfigurationChanged(Configuration newConfig) {  super.onConfigurationChanged(newConfig);  drawerToggle.onConfigurationChanged(newConfig); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) {  getMenuInflater().inflate(R.menu.menu_main,menu);  return true; } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) {  if (drawerToggle.onoptionsItemSelected(item)) {   return true;  }  switch (item.getItemID()) {   case R.ID.action_settings:    return true;   default:    return super.onoptionsItemSelected(item);  } } private ScreenShotable replaceFragment(ScreenShotable screenShotable,int topposition) {  this.res = this.res == R.drawable.content_music ? R.drawable.content_films : R.drawable.content_music;  VIEw vIEw = findVIEwByID(R.ID.content_frame);  int finalRadius = Math.max(vIEw.getWIDth(),vIEw.getHeight());  Animator animator = VIEwAnimationUtils.createCircularReveal(vIEw,topposition,finalRadius);  animator.setInterpolator(new AccelerateInterpolator());  animator.setDuration(VIEwAnimator.CIRculaR_REVEAL_ANIMATION_DURATION);  findVIEwByID(R.ID.content_overlay).setBackground(new BitmapDrawable(getResources(),screenShotable.getBitmap()));  animator.start();  ContentFragment contentFragment = ContentFragment.newInstance(this.res);  getSupportFragmentManager().beginTransaction().replace(R.ID.content_frame,contentFragment).commit();  return contentFragment; } @OverrIDe public ScreenShotable onSwitch(Resourceble slIDeMenuItem,ScreenShotable screenShotable,int position) {  switch (slIDeMenuItem.getname()) {   case ContentFragment.CLOSE:    return screenShotable;   default:    return replaceFragment(screenShotable,position);  } } @OverrIDe public voID disableHomebutton() {  getSupportActionbar().setHomebuttonEnabled(false); } @OverrIDe public voID enableHomebutton() {  getSupportActionbar().setHomebuttonEnabled(true);  drawerLayout.closeDrawers(); } @OverrIDe public voID addVIEwToContainer(VIEw vIEw) {  linearLayout.addVIEw(vIEw); }}

fragment文件夹下ContentFragment.java代码:

package com.siso.crazyworld.fragment;import androID.graphics.Bitmap;import androID.graphics.Canvas;import androID.os.Bundle;import androID.support.annotation.Nullable;import androID.support.v4.app.Fragment;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ImageVIEw;import com.siso.crazyworld.R;import sIDemenu.interfaces.ScreenShotable;public class ContentFragment extends Fragment implements ScreenShotable { public static final String CLOSE = "Close"; public static final String BUILDING = "Building"; public static final String BOOK = "Book"; public static final String PAINT = "Paint"; public static final String CASE = "Case"; public static final String SHOP = "Shop"; public static final String PARTY = "Party"; public static final String MOVIE = "MovIE"; private VIEw containerVIEw; protected ImageVIEw mImageVIEw; protected int res; private Bitmap bitmap; public static ContentFragment newInstance(int resID) {  ContentFragment contentFragment = new ContentFragment();  Bundle bundle = new Bundle();  bundle.putInt(Integer.class.getname(),resID);  contentFragment.setArguments(bundle);  return contentFragment; } @OverrIDe public voID onVIEwCreated(VIEw vIEw,@Nullable Bundle savedInstanceState) {  super.onVIEwCreated(vIEw,savedInstanceState);  this.containerVIEw = vIEw.findVIEwByID(R.ID.container); } @OverrIDe public voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  res = getArguments().getInt(Integer.class.getname()); } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {  VIEw rootVIEw = inflater.inflate(R.layout.fragment_main,container,false);  mImageVIEw = (ImageVIEw) rootVIEw.findVIEwByID(R.ID.image_content);  mImageVIEw.setClickable(true);  mImageVIEw.setFocusable(true);  mImageVIEw.setimageResource(res);  return rootVIEw; } @OverrIDe public voID takeScreenShot() {  Thread thread = new Thread() {   @OverrIDe   public voID run() {    Bitmap bitmap = Bitmap.createBitmap(containerVIEw.getWIDth(),containerVIEw.getHeight(),Bitmap.Config.ARGB_8888);    Canvas canvas = new Canvas(bitmap);    containerVIEw.draw(canvas);    ContentFragment.this.bitmap = bitmap;   }  };  thread.start(); } @OverrIDe public Bitmap getBitmap() {  return bitmap; }}

activity_classificmenu.xml内容:

<androID.support.v4.Widget.DrawerLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/drawer_layout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <io.codetail.Widget.RevealFrameLayout  androID:ID="@+ID/container_frame"  xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent">  <linearLayout   androID:ID="@+ID/content_overlay"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:orIEntation="vertical"/>  <linearLayout   androID:ID="@+ID/content_frame"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:orIEntation="vertical"/>  <androID.support.v7.Widget.Toolbar   androID:ID="@+ID/toolbar"   androID:layout_height="wrap_content"   androID:layout_wIDth="match_parent"   androID:minHeight="?attr/actionbarSize"   androID:background="?attr/colorPrimary"/> </io.codetail.Widget.RevealFrameLayout> <ScrollVIEw  androID:ID="@+ID/scrollVIEw"  androID:scrollbarThumbVertical="@androID:color/transparent"  androID:layout_wIDth="@dimen/slIDing_menu_wIDth"  androID:layout_height="match_parent"  androID:layout_gravity="start|bottom">  <linearLayout   androID:ID="@+ID/left_drawer"   androID:orIEntation="vertical"   androID:layout_wIDth="@dimen/slIDing_menu_wIDth"   androID:layout_height="wrap_content"   androID:divIDer="@androID:color/transparent"   androID:background="@androID:color/transparent">  </linearLayout> </ScrollVIEw></androID.support.v4.Widget.DrawerLayout>

.xml内容:

<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item androID:state_pressed="true" androID:drawable="@drawable/item_down"/> <item androID:state_selected="true" androID:drawable="@drawable/item_down"/> <item androID:state_focused="true" androID:drawable="@drawable/item_down"/> <item androID:drawable="@drawable/item_up"/></selector>

fragment_main.xml内容:

<?xml version="1.0" enCoding="utf-8"?><io.codetail.Widget.RevealFrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <FrameLayout  androID:ID="@+ID/container"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent">  <ImageVIEw   androID:scaleType="fitXY"   androID:ID="@+ID/image_content"   androID:src="@drawable/content_films"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"/> </FrameLayout></io.codetail.Widget.RevealFrameLayout>

strings.xml:

<?xml version="1.0" enCoding="utf-8"?><resources> <string name="app_name">CrazyWorld</string> <string name="action_settings">设置</string> <string name="drawer_open">Open</string> <string name="drawer_close">Close</string></resources>

styles.xml

<resources> <!-- Base application theme. --> <style name="Apptheme" parent="theme.AppCompat.light.NoActionbar"> </style></resources>

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android学习教程之分类侧滑菜单(5)全部内容,希望文章能够帮你解决Android学习教程之分类侧滑菜单(5)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147708.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存