Permalink
Browse files

updated custom navigation drawer implementation to NavigationView

  • Loading branch information...
frogermcs committed Jul 15, 2015
1 parent db2d384 commit bdcc2abb89998cfca75487450cc4a9607a71ac41
@@ -1,28 +1,20 @@
package io.github.froger.instamaterial.ui.activity;
import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.Optional;
import io.github.froger.instamaterial.ui.utils.DrawerLayoutInstaller;
import io.github.froger.instamaterial.R;
import io.github.froger.instamaterial.Utils;
import io.github.froger.instamaterial.ui.view.GlobalMenuView;
/**
* Created by Miroslaw Stanek on 19.01.15.
*/
public class BaseActivity extends AppCompatActivity implements GlobalMenuView.OnHeaderClickListener {
public class BaseActivity extends AppCompatActivity {
@Optional
@InjectView(R.id.toolbar)
@@ -33,16 +25,20 @@
ImageView ivLogo;
private MenuItem inboxMenuItem;
private DrawerLayout drawerLayout;
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
injectViews();
}
protected void injectViews() {
ButterKnife.inject(this);
setupToolbar();
if (shouldInstallDrawer()) {
setupDrawer();
}
}
public void setContentViewWithoutInject(int layoutResId) {
super.setContentView(layoutResId);
}
protected void setupToolbar() {
@@ -52,22 +48,6 @@ protected void setupToolbar() {
}
}
protected boolean shouldInstallDrawer() {
return true;
}
private void setupDrawer() {
GlobalMenuView menuView = new GlobalMenuView(this);
menuView.setOnHeaderClickListener(this);
drawerLayout = DrawerLayoutInstaller.from(this)
.drawerRoot(R.layout.drawer_root)
.drawerLeftView(menuView)
.drawerLeftWidth(Utils.dpToPx(300))
.withNavigationIconToggler(getToolbar())
.build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
@@ -87,19 +67,4 @@ public MenuItem getInboxMenuItem() {
public ImageView getIvLogo() {
return ivLogo;
}
@Override
public void onGlobalMenuHeaderClick(final View v) {
drawerLayout.closeDrawer(Gravity.START);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int[] startingLocation = new int[2];
v.getLocationOnScreen(startingLocation);
startingLocation[0] += v.getWidth() / 2;
UserProfileActivity.startUserProfileFromLocation(startingLocation, BaseActivity.this);
overridePendingTransition(0, 0);
}
}, 200);
}
}
@@ -0,0 +1,81 @@
package io.github.froger.instamaterial.ui.activity;
import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import butterknife.InjectView;
import butterknife.OnClick;
import io.github.froger.instamaterial.R;
import io.github.froger.instamaterial.ui.utils.CircleTransformation;
/**
* Created by Miroslaw Stanek on 15.07.15.
*/
public class BaseDrawerActivity extends BaseActivity {
@InjectView(R.id.drawerLayout)
DrawerLayout drawerLayout;
@InjectView(R.id.ivUserProfilePhoto)
ImageView ivUserProfilePhoto;
private int avatarSize;
private String profilePhoto;
@Override
public void setContentView(int layoutResID) {
super.setContentViewWithoutInject(R.layout.activity_drawer);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.flContentRoot);
LayoutInflater.from(this).inflate(layoutResID, viewGroup, true);
injectViews();
setupHeader();
}
@Override
protected void setupToolbar() {
super.setupToolbar();
if (getToolbar() != null) {
getToolbar().setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.LEFT);
}
});
}
}
@OnClick(R.id.vGlobalMenuHeader)
public void onGlobalMenuHeaderClick(final View v) {
drawerLayout.closeDrawer(Gravity.LEFT);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int[] startingLocation = new int[2];
v.getLocationOnScreen(startingLocation);
startingLocation[0] += v.getWidth() / 2;
UserProfileActivity.startUserProfileFromLocation(startingLocation, BaseDrawerActivity.this);
overridePendingTransition(0, 0);
}
}, 200);
}
private void setupHeader() {
this.avatarSize = getResources().getDimensionPixelSize(R.dimen.global_menu_avatar_size);
this.profilePhoto = getResources().getString(R.string.user_profile_photo);
Picasso.with(this)
.load(profilePhoto)
.placeholder(R.drawable.img_circle_placeholder)
.resize(avatarSize, avatarSize)
.centerCrop()
.transform(new CircleTransformation())
.into(ivUserProfilePhoto);
}
}
@@ -16,15 +16,15 @@
import android.widget.LinearLayout;
import butterknife.InjectView;
import io.github.froger.instamaterial.ui.adapter.CommentsAdapter;
import io.github.froger.instamaterial.R;
import io.github.froger.instamaterial.Utils;
import io.github.froger.instamaterial.ui.adapter.CommentsAdapter;
import io.github.froger.instamaterial.ui.view.SendCommentButton;
/**
* Created by froger_mcs on 11.11.14.
*/
public class CommentsActivity extends BaseActivity implements SendCommentButton.OnSendClickListener {
public class CommentsActivity extends BaseDrawerActivity implements SendCommentButton.OnSendClickListener {
public static final String ARG_DRAWING_START_LOCATION = "arg_drawing_start_location";
@InjectView(R.id.contentRoot)
@@ -21,7 +21,7 @@
import io.github.froger.instamaterial.ui.view.FeedContextMenuManager;
public class MainActivity extends BaseActivity implements FeedAdapter.OnFeedItemClickListener,
public class MainActivity extends BaseDrawerActivity implements FeedAdapter.OnFeedItemClickListener,
FeedContextMenu.OnFeedContextMenuItemClickListener {
public static final String ACTION_SHOW_LOADING_ITEM = "action_show_loading_item";
@@ -101,11 +101,6 @@ public void onError() {
});
}
@Override
protected boolean shouldInstallDrawer() {
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_publish, menu);
@@ -137,11 +137,6 @@ private void setupPhotoFilters() {
rvFilters.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
}
@Override
protected boolean shouldInstallDrawer() {
return false;
}
@Override
protected void onResume() {
super.onResume();
@@ -16,7 +16,7 @@
/**
* Created by Miroslaw Stanek on 14.01.15.
*/
public class UserProfileActivity extends BaseActivity implements RevealBackgroundView.OnStateChangeListener {
public class UserProfileActivity extends BaseDrawerActivity implements RevealBackgroundView.OnStateChangeListener {
public static final String ARG_REVEAL_START_LOCATION = "reveal_start_location";
@InjectView(R.id.vRevealBackground)
@@ -1,132 +0,0 @@
package io.github.froger.instamaterial.ui.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
import io.github.froger.instamaterial.R;
/**
* Created by Miroslaw Stanek on 30.01.15.
*/
public class GlobalMenuAdapter extends ArrayAdapter<GlobalMenuAdapter.GlobalMenuItem> {
private static final int TYPE_MENU_ITEM = 0;
private static final int TYPE_DIVIDER = 1;
private final LayoutInflater inflater;
private final List<GlobalMenuItem> menuItems = new ArrayList<>();
public GlobalMenuAdapter(Context context) {
super(context, 0);
this.inflater = LayoutInflater.from(context);
setupMenuItems();
}
private void setupMenuItems() {
menuItems.add(new GlobalMenuItem(R.drawable.ic_global_menu_feed, "My Feed"));
menuItems.add(new GlobalMenuItem(R.drawable.ic_global_menu_direct, "Instagram Direct"));
menuItems.add(new GlobalMenuItem(R.drawable.ic_global_menu_news, "News"));
menuItems.add(new GlobalMenuItem(R.drawable.ic_global_menu_popular, "Popular"));
menuItems.add(new GlobalMenuItem(R.drawable.ic_global_menu_nearby, "Photos Nearby"));
menuItems.add(new GlobalMenuItem(R.drawable.ic_global_menu_likes, "Photos You've Liked"));
menuItems.add(GlobalMenuItem.dividerMenuItem());
menuItems.add(new GlobalMenuItem(0, "Settings"));
menuItems.add(new GlobalMenuItem(0, "About"));
notifyDataSetChanged();
}
@Override
public int getCount() {
return menuItems.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public GlobalMenuItem getItem(int position) {
return menuItems.get(position);
}
@Override
public int getItemViewType(int position) {
return menuItems.get(position).isDivider ? TYPE_DIVIDER : TYPE_MENU_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == TYPE_MENU_ITEM) {
MenuItemViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_global_menu, parent, false);
holder = new MenuItemViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (MenuItemViewHolder) convertView.getTag();
}
GlobalMenuItem item = getItem(position);
holder.tvLabel.setText(item.label);
holder.ivIcon.setImageResource(item.iconResId);
holder.ivIcon.setVisibility(item.iconResId == 0 ? View.GONE : View.VISIBLE);
return convertView;
} else {
return inflater.inflate(R.layout.item_menu_divider, parent, false);
}
}
@Override
public boolean isEnabled(int position) {
return !menuItems.get(position).isDivider;
}
public static class MenuItemViewHolder {
@InjectView(R.id.ivIcon)
ImageView ivIcon;
@InjectView(R.id.tvLabel)
TextView tvLabel;
public MenuItemViewHolder(View view) {
ButterKnife.inject(this, view);
}
}
public static class GlobalMenuItem {
public int iconResId;
public String label;
public boolean isDivider;
private GlobalMenuItem() {
}
public GlobalMenuItem(int iconResId, String label) {
this.iconResId = iconResId;
this.label = label;
this.isDivider = false;
}
public static GlobalMenuItem dividerMenuItem() {
GlobalMenuItem globalMenuItem = new GlobalMenuItem();
globalMenuItem.isDivider = true;
return globalMenuItem;
}
}
}
Oops, something went wrong.

0 comments on commit bdcc2ab

Please sign in to comment.