com.chatwingsdk.adapters.EmoticonsAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.chatwingsdk.adapters.EmoticonsAdapter.java

Source

/*
 * Copyright (C) 2014 ChatWing
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.chatwingsdk.adapters;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.chatwingsdk.fragments.EmoticonsPageFragment;

import java.util.ArrayList;
import java.util.TreeMap;

/**
 * Author: Huy Nguyen
 * Date: 7/8/13
 * Time: 3:38 PM
 */
public class EmoticonsAdapter extends FragmentStatePagerAdapter {

    private ArrayList<String> mEmoticons;
    private TreeMap<String, String> mEmoticonMap;
    private int mEmoticonsPerPage;

    public EmoticonsAdapter(FragmentManager fm, TreeMap<String, String> emoticonMap, int emoticonPerPage) {
        super(fm);
        mEmoticonMap = emoticonMap;
        mEmoticons = new ArrayList<String>(emoticonMap.keySet());
        mEmoticonsPerPage = emoticonPerPage;
    }

    @Override
    public Fragment getItem(int position) {
        int from = mEmoticonsPerPage * position;
        int to = Math.min(from + mEmoticonsPerPage, mEmoticons.size()) - 1;
        String fromInclusive = mEmoticons.get(from);
        String toInclusive = mEmoticons.get(to);
        TreeMap<String, String> pageEmoticons = new TreeMap<String, String>(
                mEmoticonMap.subMap(fromInclusive, true, toInclusive, true));
        return EmoticonsPageFragment.newInstance(pageEmoticons);
    }

    @Override
    public int getCount() {
        // Round up the number of pages
        return (mEmoticons.size() / mEmoticonsPerPage) + 1;
    }
}