Android Open Source - MaterialManager Navigation Drawer Adapter






From Project

Back to project page MaterialManager.

License

The source code is released under:

There is no license, someone decided to pretty much republish Cabinet with no credit so I?m taking away the license altogether.

If you think the Android project MaterialManager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.afollestad.cabinet.adapters;
//from  w  ww.j av a  2s . c  o m
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.cabinet.R;
import com.afollestad.cabinet.file.LocalFile;
import com.afollestad.cabinet.file.base.File;
import com.afollestad.cabinet.utils.Pins;
import com.afollestad.cabinet.utils.Utils;

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

public class NavigationDrawerAdapter extends RecyclerView.Adapter<NavigationDrawerAdapter.ShortcutViewHolder> implements View.OnClickListener, View.OnLongClickListener {

    @Override
    public void onClick(View view) {
        mListener.onClick((Integer) view.getTag());
    }

    @Override
    public boolean onLongClick(View view) {
        mListener.onLongClick((Integer) view.getTag());
        return false;
    }

    public interface ClickListener {
        public abstract void onClick(int index);

        public abstract boolean onLongClick(int index);
    }

    public NavigationDrawerAdapter(Activity context, ClickListener listener) {
        mContext = context;
        mItems = new ArrayList<Pins.Item>();
        mListener = listener;
        selectionColor = context.getResources().getColor(R.color.cabinet_color);
        bodyText = Utils.resolveColor(context, R.attr.body_text);

        if (Pins.getAll(context).size() == 0) {
            LocalFile item = new LocalFile(context);
            Pins.add(context, new Pins.Item(item));
            item = new LocalFile(context, Environment.getExternalStorageDirectory());
            Pins.add(context, new Pins.Item(item));
            try {

                // TODO SD card stuff
//                java.io.File sd = new java.io.File("/external_sd");
//                if (sd.exists()) {
//                    item = new LocalFile(context, sd);
//                    Pins.add(context, new Pins.Item(item));
//                } else {
//                    sd = new java.io.File("/extSdCard");
//                    if (sd.exists()) {
//                        item = new LocalFile(context, sd);
//                        Pins.add(context, new Pins.Item(item));
//                    }
//                }

                item = new LocalFile(context, new java.io.File(Environment.getExternalStorageDirectory(), "DCIM"));
                if (item.existsSync())
                    Pins.add(context, new Pins.Item(item));
                item = new LocalFile(context, new java.io.File(Environment.getExternalStorageDirectory(), "Download"));
                if (item.existsSync())
                    Pins.add(context, new Pins.Item(item));
                item = new LocalFile(context, new java.io.File(Environment.getExternalStorageDirectory(), "Music"));
                if (item.existsSync())
                    Pins.add(context, new Pins.Item(item));
                item = new LocalFile(context, new java.io.File(Environment.getExternalStorageDirectory(), "Pictures"));
                if (item.existsSync())
                    Pins.add(context, new Pins.Item(item));
            } catch (Exception e) {
                Toast.makeText(context, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        }
        reload(context);
    }

    private Activity mContext;
    private List<Pins.Item> mItems;
    private int mCheckedPos = -1;
    private ClickListener mListener;
    private int selectionColor;
    private int bodyText;

    public void reload(Context context) {
        set(Pins.getAll(context));
    }

    public void set(List<Pins.Item> items) {
        mItems.clear();
        for (Pins.Item i : items)
            mItems.add(i);
        notifyDataSetChanged();
    }

    public int setCheckedFile(File file) {
        int index = -1;
        for (int i = 0; i < mItems.size(); i++) {
            Pins.Item item = mItems.get(i);
            if (item.getPath().equals(file.getPath())) {
                index = i;
                break;
            }
        }
        setCheckedPos(index);
        return index;
    }

    public void setCheckedPos(int index) {
        mCheckedPos = index;
        notifyDataSetChanged();
    }

    public Pins.Item getItem(int index) {
        return mItems.get(index);
    }

    public static class ShortcutViewHolder extends RecyclerView.ViewHolder {

        public ShortcutViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView;
        }

        TextView title;
    }

    @Override
    public ShortcutViewHolder onCreateViewHolder(ViewGroup parent, int index) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_drawer, parent, false);
        return new ShortcutViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ShortcutViewHolder holder, int index) {
        Pins.Item item = mItems.get(index);
        holder.title.setTag(index);
        holder.title.setOnClickListener(this);
        holder.title.setOnLongClickListener(this);
        holder.title.setActivated(mCheckedPos == index);
        holder.title.setTextColor(mCheckedPos == index ? selectionColor : bodyText);

        if (mCheckedPos == index) {
            holder.title.setTypeface(Typeface.create("sans-serif", Typeface.NORMAL));
        } else {
            holder.title.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
        }

        if (item.isRemote()) {
            holder.title.setText(item.getDisplay(mContext));
        } else {
            File file = new LocalFile(mContext, item.getPath());
            if (file.isRoot()) {
                holder.title.setText(R.string.root);
            } else if (file.isStorageDirectory()) {
                holder.title.setText(R.string.storage);
            } else if (file.getName().startsWith("sdcard")) {
                holder.title.setText(R.string.sdcard);
            } else {
                holder.title.setText(item.getDisplay(mContext));
            }
        }
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }
}




Java Source Code List

com.afollestad.cabinet.App.java
com.afollestad.cabinet.ApplicationTest.java
com.afollestad.cabinet.adapters.FileAdapter.java
com.afollestad.cabinet.adapters.NavigationDrawerAdapter.java
com.afollestad.cabinet.cab.CopyCab.java
com.afollestad.cabinet.cab.CutCab.java
com.afollestad.cabinet.cab.MainCab.java
com.afollestad.cabinet.cab.PickerCab.java
com.afollestad.cabinet.cab.base.BaseCab.java
com.afollestad.cabinet.cab.base.BaseFileCab.java
com.afollestad.cabinet.comparators.AlphabeticalComparator.java
com.afollestad.cabinet.comparators.ExtensionComparator.java
com.afollestad.cabinet.comparators.FoldersFirstComparator.java
com.afollestad.cabinet.comparators.HighLowSizeComparator.java
com.afollestad.cabinet.comparators.LastModifiedComparator.java
com.afollestad.cabinet.comparators.LowHighSizeComparator.java
com.afollestad.cabinet.file.CloudFile.java
com.afollestad.cabinet.file.LocalFile.java
com.afollestad.cabinet.file.Remote.java
com.afollestad.cabinet.file.base.FileFilter.java
com.afollestad.cabinet.file.base.File.java
com.afollestad.cabinet.file.root.LsParser.java
com.afollestad.cabinet.file.root.LsTokenizer.java
com.afollestad.cabinet.file.root.RootFile.java
com.afollestad.cabinet.fragments.AboutDialog.java
com.afollestad.cabinet.fragments.DetailsDialog.java
com.afollestad.cabinet.fragments.DirectoryFragment.java
com.afollestad.cabinet.fragments.NavigationDrawerFragment.java
com.afollestad.cabinet.fragments.RemoteConnectionDialog.java
com.afollestad.cabinet.fragments.WelcomeFragment.java
com.afollestad.cabinet.services.NetworkService.java
com.afollestad.cabinet.sftp.FileNotExistsException.java
com.afollestad.cabinet.sftp.SftpClient.java
com.afollestad.cabinet.ui.DrawerActivity.java
com.afollestad.cabinet.ui.SettingsActivity.java
com.afollestad.cabinet.ui.TextEditor.java
com.afollestad.cabinet.ui.base.NetworkedActivity.java
com.afollestad.cabinet.ui.base.ThemableActivity.java
com.afollestad.cabinet.utils.APKIconDownloader.java
com.afollestad.cabinet.utils.PauseOnScrollListener.java
com.afollestad.cabinet.utils.Perm.java
com.afollestad.cabinet.utils.Pins.java
com.afollestad.cabinet.utils.StorageHelper.java
com.afollestad.cabinet.utils.ThemeUtils.java
com.afollestad.cabinet.utils.TimeUtils.java
com.afollestad.cabinet.utils.Utils.java
com.afollestad.cabinet.zip.Unzipper.java
com.afollestad.cabinet.zip.Zipper.java
eu.chainfire.libsuperuser.ApplicationTest.java
eu.chainfire.libsuperuser.Application.java
eu.chainfire.libsuperuser.Debug.java
eu.chainfire.libsuperuser.HideOverlaysReceiver.java
eu.chainfire.libsuperuser.ShellNotClosedException.java
eu.chainfire.libsuperuser.ShellOnMainThreadException.java
eu.chainfire.libsuperuser.Shell.java
eu.chainfire.libsuperuser.StreamGobbler.java