2015-09-10 22 views
6

trong https://github.com/Yalantis/Side-Menu.Androidý định hoạt động trong Side-Menu.Android của vật liệu thiết kế

hãy giúp tôi :(

Làm thế nào có thể khi tôi bấm vào mỗi mục trong menu bên, hãy chỉ huy "ý định hoạt động khác "thay vì vận chuyển giữa các hình ảnh

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.animation.AccelerateInterpolator; 
import android.widget.LinearLayout; 

import java.util.ArrayList; 
import java.util.List; 

import io.codetail.animation.SupportAnimator; 
import io.codetail.animation.ViewAnimationUtils; 
import yalantis.com.sidemenu.interfaces.Resourceble; 
import yalantis.com.sidemenu.interfaces.ScreenShotable; 
import yalantis.com.sidemenu.model.SlideMenuItem; 
import yalantis.com.sidemenu.sample.fragment.ContentFragment; 
import yalantis.com.sidemenu.util.ViewAnimator; 


public class MainActivity 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_main); 
    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()); 
    SupportAnimator animator = ViewAnimationUtils.createCircularReveal(view, 0, topPosition, 0, finalRadius); 
    animator.setInterpolator(new AccelerateInterpolator()); 
    animator.setDuration(ViewAnimator.CIRCULAR_REVEAL_ANIMATION_DURATION); 

    findViewById(R.id.content_overlay).setBackgroundDrawable(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); 
} 
} 

Rất cám ơn cho tất cả

+0

riêng void replaceFragment (ScreenShotable screenShotable, int position) { Fragment fragment = null; công tắc (vị trí) { trường hợp 0: fragment = new HomeFragment(); ngắt; trường hợp 1: fragment = new HomeFragment(); ngắt; trường hợp 2: fragment = new HomeFragment(); ngắt; mặc định: ngắt; } tôi sử dụng theo cách của bạn nhưng không hoạt động .. Vui lòng viết cho tôi mã đầy đủ .. và verrrrrrry Thaaanks :) @Tasos – BestAndroid

Trả lời

2

Hi hiện tại tôi cũng sử dụng thư viện đó. tôi đã gắn mã làm việc của tôi. (Trong trường hợp này, sử dụng phân đoạn) Đi và tìm cách sử dụng startActivity thì bạn có thể sử dụng Intent & putExtra resId của bạn.

@Override 
public ScreenShotable onSwitch(Resourceble resourceble, ScreenShotable screenShotable, int position) { 

    int primaryColorCanvas = R.color.colorPrimary; 

    switch (resourceble.getName()) { 
     case ContentFragment.CLOSE: 
      return screenShotable; 

     case ContentFragment.DASHBOARD: 
      DashboardFragment fragment = DashboardFragment.newInstance(primaryColorCanvas); 
      getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit(); 
      return replaceFragment(fragment, position); 

     case ContentFragment.FRIDGE: 
      FridgeFragment fragment2 = FridgeFragment.newInstance(primaryColorCanvas); 
      getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment2).commit(); 
      return replaceFragment(fragment2, position); 

     case ContentFragment.COOKBOOK: 
      DashboardFragment fragment3 = DashboardFragment.newInstance(primaryColorCanvas); 
      getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment3).commit(); 
      return replaceFragment(fragment3, position); 

     case ContentFragment.SHOPPINGLIST: 
     case ContentFragment.FEEDBACK: 
      ContentFragment fragment4 = ContentFragment.newInstance(primaryColorCanvas); 
      getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,fragment4).commit(); 
      return replaceFragment(fragment4, position); 
     default: 
      ContentFragment fragment0 = ContentFragment.newInstance(primaryColorCanvas); 
      getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,fragment0).commit(); 
      return replaceFragment(fragment0, position); 
    } 
} 

private ScreenShotable replaceFragment(ScreenShotable screenShotable, int position) { 

    int finalRadius = Math.max(contentFrame.getWidth(), contentFrame.getHeight()); 
    SupportAnimator animator = ViewAnimationUtils.createCircularReveal(contentFrame, 0, position, 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(); 
    return screenShotable; 
} 

private void initializedMenu() { 
    SlideMenuItem menuItem0 = new SlideMenuItem(contentFragment.CLOSE, R.drawable.back_btn); 
    list.add(menuItem0); 
    SlideMenuItem menuItem = new SlideMenuItem(contentFragment.DASHBOARD, R.drawable.dashboard_icon_a); 
    list.add(menuItem); 
    SlideMenuItem menuIte2 = new SlideMenuItem(contentFragment.FRIDGE, R.drawable.fridge_icon_a); 
    list.add(menuIte2); 
    SlideMenuItem menuItem3 = new SlideMenuItem(contentFragment.COOKBOOK, R.drawable.cookbook_icon_a); 
    list.add(menuItem3); 
    SlideMenuItem menuItem4 = new SlideMenuItem(contentFragment.SHOPPINGLIST, R.drawable.right_icon_a); 
    list.add(menuItem4); 
    SlideMenuItem menuItem5 = new SlideMenuItem(contentFragment.FEEDBACK, R.drawable.feedback_icon_a); 
    list.add(menuItem5); 

} 

Hãy nhớ rằng tôi đã gán biến chuỗi tĩnh trong ContentFragment. Bạn có thể xem tại initializedMenu().

Hy vọng trợ giúp!

Các vấn đề liên quan