欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-01 18:45:34
本文实例为大家分享了android分类侧滑菜单的制作方法,供大家参考,具体内容如下 classificmenuactivity.java代码: package...

本文实例为大家分享了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, 0, topposition, 0, 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)

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