Permalink
Browse files

GlobalMenuView implementation

  • Loading branch information...
frogermcs committed Jan 31, 2015
1 parent 4f42f96 commit cfb4ee819fea9fad852a2eeda671b15ccac6ade2
@@ -0,0 +1,131 @@
package io.github.froger.instamaterial;
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;
/**
* 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;
}
}
}
@@ -0,0 +1,79 @@
package io.github.froger.instamaterial.view;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import com.squareup.picasso.Picasso;
import io.github.froger.instamaterial.CircleTransformation;
import io.github.froger.instamaterial.GlobalMenuAdapter;
import io.github.froger.instamaterial.R;
/**
* Created by Miroslaw Stanek on 30.01.15.
*/
public class GlobalMenuView extends ListView implements View.OnClickListener {
private OnHeaderClickListener onHeaderClickListener;
private GlobalMenuAdapter globalMenuAdapter;
private ImageView ivUserProfilePhoto;
private int avatarSize;
private String profilePhoto;
public GlobalMenuView(Context context) {
super(context);
init();
}
private void init() {
setChoiceMode(CHOICE_MODE_SINGLE);
setDivider(getResources().getDrawable(android.R.color.transparent));
setDividerHeight(0);
setBackgroundColor(Color.WHITE);
setupHeader();
setupAdapter();
}
private void setupAdapter() {
globalMenuAdapter = new GlobalMenuAdapter(getContext());
setAdapter(globalMenuAdapter);
}
private void setupHeader() {
this.avatarSize = getResources().getDimensionPixelSize(R.dimen.global_menu_avatar_size);
this.profilePhoto = getResources().getString(R.string.user_profile_photo);
setHeaderDividersEnabled(true);
View vHeader = LayoutInflater.from(getContext()).inflate(R.layout.view_global_menu_header, null);
ivUserProfilePhoto = (ImageView) vHeader.findViewById(R.id.ivUserProfilePhoto);
Picasso.with(getContext())
.load(profilePhoto)
.placeholder(R.drawable.img_circle_placeholder)
.resize(avatarSize, avatarSize)
.centerCrop()
.transform(new CircleTransformation())
.into(ivUserProfilePhoto);
addHeaderView(vHeader);
vHeader.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (onHeaderClickListener != null) {
onHeaderClickListener.onGlobalMenuHeaderClick(v);
}
}
public interface OnHeaderClickListener {
public void onGlobalMenuHeaderClick(View v);
}
public void setOnHeaderClickListener(OnHeaderClickListener onHeaderClickListener) {
this.onHeaderClickListener = onHeaderClickListener;
}
}

1 comment on commit cfb4ee8

@njovujsh

This comment has been minimized.

Show comment
Hide comment
@njovujsh

njovujsh Nov 4, 2015

This is very nice !:)

This is very nice !:)

Please sign in to comment.