com.app.sample.chatting.adapter.chat.FaceCategroyAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.app.sample.chatting.adapter.chat.FaceCategroyAdapter.java

Source

/*
 * Copyright (c) 2015, .
 *
 * 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.app.sample.chatting.adapter.chat;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.widget.ImageView;

import com.app.sample.chatting.activity.chat.ChatFunctionFragment;
import com.app.sample.chatting.activity.chat.FacePageFragment;
import com.app.sample.chatting.activity.chat.OnOperationListener;
import com.app.sample.chatting.R;
import com.app.sample.chatting.data.emoji.EmojiPageFragment;
import com.app.sample.chatting.widget.KJChatKeyboard;
import com.app.sample.chatting.widget.PagerSlidingTabStrip;

import org.kymjs.kjframe.bitmap.BitmapCreate;

import java.io.File;
import java.util.List;

/**
 * viewpager?
 *
 * @author kymjs (http://www.kymjs.com/)
 */
public class FaceCategroyAdapter extends FragmentStatePagerAdapter implements PagerSlidingTabStrip.IconTabProvider {
    private final int sMode;

    //?itemfolder??folderface
    private List<String> datas;
    private OnOperationListener listener;

    public FaceCategroyAdapter(FragmentManager fm, int mode) {
        super(fm);
        sMode = mode;
    }

    @Override
    public void setPageIcon(int position, ImageView image) {
        if (position == 0) {
            image.setImageResource(R.drawable.icon_face_click);
            return;
        }
        File file = new File(datas.get(position - 1));
        String path = null;
        for (int i = 0; i < file.list().length; i++) {
            path = file.list()[i];
            if (path.endsWith(".png") || path.endsWith(".jpg") || path.endsWith(".jpeg")) {
                break;
            }
        }
        Bitmap bitmap = BitmapCreate.bitmapFromFile(file.getAbsolutePath() + "/" + path, 40, 40);
        image.setImageBitmap(bitmap);
    }

    @Override
    public int getCount() {
        if (sMode == KJChatKeyboard.LAYOUT_TYPE_FACE) {
            int count = datas == null ? 0 : datas.size();
            //1emoji
            return (count + 1);
        } else {
            return 1;
        }
    }

    @Override
    public Fragment getItem(int position) {
        Fragment f = null;
        if (sMode == KJChatKeyboard.LAYOUT_TYPE_FACE) {
            if (position == 0) {
                f = new EmojiPageFragment();
                ((EmojiPageFragment) f).setOnOperationListener(listener);
            } else {
                position--;
                f = new FacePageFragment();
                ((FacePageFragment) f).setOnOperationListener(listener);
                Bundle bundle = new Bundle();
                bundle.putString(FacePageFragment.FACE_FOLDER_PATH, datas.get(position));
                f.setArguments(bundle);
            }
        } else {
            f = new ChatFunctionFragment();
            ((ChatFunctionFragment) f).setOnOperationListener(listener);
        }
        return f;
    }

    public void setOnOperationListener(OnOperationListener onOperationListener) {
        this.listener = onOperationListener;
    }

    public void refresh(List<String> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }
}