com.mohammedsazid.android.szlauncher.DrawerAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.mohammedsazid.android.szlauncher.DrawerAdapter.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Mohammed Sazid-Al-Rashid
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.mohammedsazid.android.szlauncher;

import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.ViewHolder> {
    private static final int HEADER_TYPE = 0;
    private static final int ROW_TYPE = 1;
    private final FragmentActivity activity;
    private DrawerLayout drawerLayout;

    private List<String> rows;

    public DrawerAdapter(FragmentActivity activity, DrawerLayout drawerLayout, List<String> rows) {
        this.activity = activity;
        this.rows = rows;
        this.drawerLayout = drawerLayout;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;

        if (viewType == HEADER_TYPE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_header, parent, false);
            return new ViewHolder(view, viewType);
        } else if (viewType == ROW_TYPE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_row, parent, false);
            return new ViewHolder(view, viewType);
        }

        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        if (viewHolder.viewType == ROW_TYPE) {
            final String rowText = rows.get(position - 1);

            viewHolder.drawerRowText.setText(rowText);
            viewHolder.imageView.setImageResource(R.mipmap.ic_launcher);

            viewHolder.v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (rowText) {
                    case "Single Pane layout":
                        activity.getSupportFragmentManager().beginTransaction()
                                .setCustomAnimations(R.anim.slide_in, R.anim.slide_out)
                                .replace(R.id.content, new SinglePaneAppsFragment()).commit();
                        break;
                    case "Dual Pane layout":
                        activity.getSupportFragmentManager().beginTransaction()
                                .setCustomAnimations(R.anim.slide_in, R.anim.slide_out)
                                .replace(R.id.content, new DualPaneAppsFragment()).commit();
                        break;
                    default:
                        Toast.makeText(view.getContext(), rowText, Toast.LENGTH_SHORT).show();
                        break;
                    }

                    drawerLayout.closeDrawers();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        // + 1 for header
        return rows.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEADER_TYPE;
        }

        return ROW_TYPE;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        protected int viewType;
        private ImageView imageView;
        private TextView drawerRowText;
        private View v;

        public ViewHolder(View itemView, int viewType) {
            super(itemView);

            this.viewType = viewType;
            this.v = itemView;

            if (viewType == ROW_TYPE) {
                imageView = (ImageView) itemView.findViewById(R.id.drawer_row_icon);
                drawerRowText = (TextView) itemView.findViewById(R.id.drawer_row_text);
            }

            itemView.setClickable(true);
        }
    }
}