kimi
2021-02-18 749a5510a9f014446a3cd6ba57b3cb0cc8148dc1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.lqr.emoji;
 
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RelativeLayout;
 
import androidx.viewpager.widget.PagerAdapter;
 
import java.util.List;
 
import static com.lqr.emoji.EmotionLayout.EMOJI_PER_PAGE;
import static com.lqr.emoji.EmotionLayout.STICKER_PER_PAGE;
 
/**
 * CSDN_LQR
 * 表情控件的ViewPager适配器(emoji + 贴图)
 */
 
public class EmotionViewPagerAdapter extends PagerAdapter {
 
    private int mPageCount = 0;
 
    private int mEmotionLayoutWidth;
    private int mEmotionLayoutHeight;
 
    private IEmotionSelectedListener listener;
 
    public EmotionViewPagerAdapter(int emotionLayoutWidth, int emotionLayoutHeight, boolean stickerVisible, IEmotionSelectedListener listener) {
        mEmotionLayoutWidth = emotionLayoutWidth;
        mEmotionLayoutHeight = emotionLayoutHeight;
        mPageCount = (int) Math.ceil(EmojiManager.getDisplayCount() / (float) EMOJI_PER_PAGE);
        if (stickerVisible) {
            for (int i = 0; i < StickerManager.getInstance().getStickerCategories().size(); i++) {
                mPageCount += StickerManager.getInstance().getStickerCategories().get(i).getStickers().size() / STICKER_PER_PAGE;
            }
        }
        this.listener = listener;
    }
 
    @Override
    public int getCount() {
        return mPageCount == 0 ? 1 : mPageCount;
    }
 
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
 
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
 
        Context context = container.getContext();
        RelativeLayout rl = new RelativeLayout(context);
        rl.setGravity(Gravity.CENTER);
 
        GridView gridView = new GridView(context);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        gridView.setLayoutParams(params);
        gridView.setGravity(Gravity.CENTER);
 
        int tabPosition = positionToCategoryTabIndex(position);
 
        gridView.setTag((tabPosition & 0xFFF) << 12 | (position & 0xFFF)); // category index | pageIndex
 
        if (tabPosition == 0) {
            gridView.setOnItemClickListener(emojiListener);
            gridView.setAdapter(new EmojiAdapter(context, mEmotionLayoutWidth, mEmotionLayoutHeight, position * EMOJI_PER_PAGE));
            gridView.setNumColumns(EmotionLayout.EMOJI_COLUMNS);
        } else {
            int categoryStickerPageIndex = positionToCategoryPageIndex(position);
            StickerCategory category = StickerManager.getInstance().getCategory(StickerManager.getInstance().getStickerCategories().get(tabPosition - 1).getName());
            gridView.setOnItemClickListener(stickerListener);
            gridView.setAdapter(new StickerAdapter(context, category, mEmotionLayoutWidth, mEmotionLayoutHeight, categoryStickerPageIndex * STICKER_PER_PAGE));
            gridView.setNumColumns(EmotionLayout.STICKER_COLUMNS);
        }
 
        rl.addView(gridView);
        container.addView(rl);
        return rl;
    }
 
    /**
     * 根据categoryTabIndex计算page position
     *
     * @param categoryTabIndex
     * @return
     */
    int categoryTabIndexToPagePosition(int categoryTabIndex) {
        int position = (int) Math.ceil(EmojiManager.getDisplayCount() / (float) EMOJI_PER_PAGE);
        if (categoryTabIndex == 0) {
            return 0;
        } else {
            for (int i = 0; i < categoryTabIndex - 1; i++) {
                position += (int) Math.ceil(StickerManager.getInstance().getStickerCategories().get(i).getStickers().size() / (float) EmotionLayout.STICKER_PER_PAGE);
            }
        }
        return position;
    }
 
    /**
     * 根据page position,计算tab index
     *
     * @param position
     * @return
     */
    int positionToCategoryTabIndex(int position) {
        int categoryTabindex = 0;
        int emojiPageCount = (int) Math.ceil(EmojiManager.getDisplayCount() / (float) EMOJI_PER_PAGE);
        if (position >= emojiPageCount) {
            int stickerPageCount = 0;
            for (int i = 0; i < StickerManager.getInstance().getStickerCategories().size(); i++) {
                stickerPageCount += (int) Math.ceil(StickerManager.getInstance().getStickerCategories().get(i).getStickers().size() / (float) EmotionLayout.STICKER_PER_PAGE);
                if (position < emojiPageCount + stickerPageCount) {
                    categoryTabindex = 1 + i;
                    break;
                }
            }
        }
        return categoryTabindex;
    }
 
    /**
     * 根据page position,计算Category内部的pageIndex
     *
     * @param position
     * @return
     */
    int positionToCategoryPageIndex(int position) {
        int emojiPageCount = (int) Math.ceil(EmojiManager.getDisplayCount() / (float) EMOJI_PER_PAGE);
        int categoryPageIndex = -1;
        if (position < emojiPageCount) {
            categoryPageIndex = position;
        } else {
            int stickerPageCount = 0;
            for (int i = 0; i < StickerManager.getInstance().getStickerCategories().size(); i++) {
                categoryPageIndex = position - emojiPageCount - stickerPageCount;
                stickerPageCount += (int) Math.ceil(StickerManager.getInstance().getStickerCategories().get(i).getStickers().size() / (float) EmotionLayout.STICKER_PER_PAGE);
                if (position < emojiPageCount + stickerPageCount) {
                    break;
                }
            }
        }
        return categoryPageIndex;
    }
 
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
 
    public AdapterView.OnItemClickListener emojiListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 
            int index = position + (Integer) parent.getTag() * EmotionLayout.EMOJI_PER_PAGE;
            int count = EmojiManager.getDisplayCount();
            if (position == EmotionLayout.EMOJI_PER_PAGE || index >= count) {
                if (listener != null) {
                    listener.onEmojiSelected("/DEL");
                }
            } else {
                String text = EmojiManager.getDisplayText((int) id);
                if (!TextUtils.isEmpty(text)) {
                    if (listener != null) {
                        listener.onEmojiSelected(text);
                    }
                }
            }
        }
    };
    public AdapterView.OnItemClickListener stickerListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int tag = (int) parent.getTag();
            int pagePosition = tag & 0xFFF;
            int categoryIndex = (tag >> 12) & 0xFFF;
            int index = position;
            if (pagePosition > 0) {
                int categoryStartPagePosition = categoryTabIndexToPagePosition(categoryIndex);
                index += (pagePosition - categoryStartPagePosition) * EmotionLayout.STICKER_PER_PAGE;
            }
            StickerCategory category = StickerManager.getInstance().getStickerCategories().get(categoryIndex - 1);
            List<StickerItem> stickers = category.getStickers();
 
            if (index >= stickers.size()) {
                Log.i("CSDN_LQR", "index " + index + " larger than size " + stickers.size());
                return;
            }
 
            if (listener != null) {
                StickerItem sticker = stickers.get(index);
                StickerCategory real = StickerManager.getInstance().getCategory(sticker.getCategory());
 
                if (real == null) {
                    return;
                }
 
                listener.onStickerSelected(sticker.getCategory(), sticker.getName(), StickerManager.getInstance().getStickerBitmapPath(sticker.getCategory(), sticker.getName()));
            }
        }
    };
}