Example usage for android.widget GridView setNumColumns

List of usage examples for android.widget GridView setNumColumns

Introduction

In this page you can find the example usage for android.widget GridView setNumColumns.

Prototype

public void setNumColumns(int numColumns) 

Source Link

Document

Set the number of columns in the grid

Usage

From source file:org.telegraph.ui.Components.EmojiView.java

public EmojiView(boolean needStickers, Context context) {
    super(context);

    showStickers = needStickers;/*ww  w.  j ava  2s .  c om*/

    for (int i = 0; i < Emoji.data.length + 1; i++) {
        GridView gridView = new GridView(context);
        if (AndroidUtilities.isTablet()) {
            gridView.setColumnWidth(AndroidUtilities.dp(60));
        } else {
            gridView.setColumnWidth(AndroidUtilities.dp(45));
        }
        gridView.setNumColumns(-1);
        views.add(gridView);

        EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(i - 1);
        gridView.setAdapter(emojiGridAdapter);
        AndroidUtilities.setListViewEdgeEffectColor(gridView, 0xfff5f6f7);
        adapters.add(emojiGridAdapter);
    }

    if (showStickers) {
        StickersQuery.checkStickers();
        stickersGridView = new GridView(context) {
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    int x = (int) event.getX();
                    int y = (int) event.getY();
                    int count = stickersGridView.getChildCount();
                    for (int a = 0; a < count; a++) {
                        View view = stickersGridView.getChildAt(a);
                        int top = view.getTop();
                        int bottom = view.getBottom();
                        int left = view.getLeft();
                        int right = view.getRight();
                        if (top > y || bottom < y || left > x || right < x) {
                            continue;
                        }
                        if (!(view instanceof StickerEmojiCell) || !((StickerEmojiCell) view).showingBitmap()) {
                            return super.onInterceptTouchEvent(event);
                        }
                        startX = x;
                        startY = y;
                        currentStickerPreviewCell = (StickerEmojiCell) view;
                        openStickerPreviewRunnable = new Runnable() {
                            @Override
                            public void run() {
                                if (openStickerPreviewRunnable == null) {
                                    return;
                                }
                                stickersGridView.setOnItemClickListener(null);
                                stickersGridView.requestDisallowInterceptTouchEvent(true);
                                openStickerPreviewRunnable = null;
                                StickerPreviewViewer.getInstance().setParentActivity((Activity) getContext());
                                StickerPreviewViewer.getInstance()
                                        .setKeyboardHeight(EmojiView.this.getMeasuredHeight());
                                StickerPreviewViewer.getInstance().open(currentStickerPreviewCell.getSticker());
                                currentStickerPreviewCell.setScaled(true);
                            }
                        };
                        AndroidUtilities.runOnUIThread(openStickerPreviewRunnable, 200);
                        return true;
                    }
                }
                return false;
            }
        };
        stickersGridView.setSelector(R.drawable.transparent);
        stickersGridView.setColumnWidth(AndroidUtilities.dp(72));
        stickersGridView.setNumColumns(-1);
        stickersGridView.setPadding(0, AndroidUtilities.dp(4), 0, 0);
        stickersGridView.setClipToPadding(false);
        views.add(stickersGridView);
        stickersGridAdapter = new StickersGridAdapter(context);
        stickersGridView.setAdapter(stickersGridAdapter);
        stickersGridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (openStickerPreviewRunnable != null || StickerPreviewViewer.getInstance().isVisible()) {
                    if (event.getAction() == MotionEvent.ACTION_UP
                            || event.getAction() == MotionEvent.ACTION_CANCEL
                            || event.getAction() == MotionEvent.ACTION_POINTER_UP) {
                        AndroidUtilities.runOnUIThread(new Runnable() {
                            @Override
                            public void run() {
                                stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
                            }
                        }, 150);
                        if (openStickerPreviewRunnable != null) {
                            AndroidUtilities.cancelRunOnUIThread(openStickerPreviewRunnable);
                            openStickerPreviewRunnable = null;
                        } else if (StickerPreviewViewer.getInstance().isVisible()) {
                            StickerPreviewViewer.getInstance().close();
                            if (currentStickerPreviewCell != null) {
                                currentStickerPreviewCell.setScaled(false);
                                currentStickerPreviewCell = null;
                            }
                        }
                    } else if (event.getAction() != MotionEvent.ACTION_DOWN) {
                        if (StickerPreviewViewer.getInstance().isVisible()) {
                            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                                int x = (int) event.getX();
                                int y = (int) event.getY();
                                int count = stickersGridView.getChildCount();
                                for (int a = 0; a < count; a++) {
                                    View view = stickersGridView.getChildAt(a);
                                    int top = view.getTop();
                                    int bottom = view.getBottom();
                                    int left = view.getLeft();
                                    int right = view.getRight();
                                    if (top > y || bottom < y || left > x || right < x) {
                                        continue;
                                    }
                                    if (!(view instanceof StickerEmojiCell)
                                            || view == currentStickerPreviewCell) {
                                        break;
                                    }
                                    if (currentStickerPreviewCell != null) {
                                        currentStickerPreviewCell.setScaled(false);
                                    }
                                    currentStickerPreviewCell = (StickerEmojiCell) view;
                                    StickerPreviewViewer.getInstance()
                                            .setKeyboardHeight(EmojiView.this.getMeasuredHeight());
                                    StickerPreviewViewer.getInstance()
                                            .open(currentStickerPreviewCell.getSticker());
                                    currentStickerPreviewCell.setScaled(true);
                                    return true;
                                }
                            }
                            return true;
                        } else if (openStickerPreviewRunnable != null) {
                            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                                if (Math.hypot(startX - event.getX(), startY - event.getY()) > AndroidUtilities
                                        .dp(10)) {
                                    AndroidUtilities.cancelRunOnUIThread(openStickerPreviewRunnable);
                                    openStickerPreviewRunnable = null;
                                }
                            } else {
                                AndroidUtilities.cancelRunOnUIThread(openStickerPreviewRunnable);
                                openStickerPreviewRunnable = null;
                            }
                        }
                    }
                }
                return false;
            }
        });
        stickersOnItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long i) {
                if (!(view instanceof StickerEmojiCell)) {
                    return;
                }
                if (openStickerPreviewRunnable != null) {
                    AndroidUtilities.cancelRunOnUIThread(openStickerPreviewRunnable);
                    openStickerPreviewRunnable = null;
                }
                if (currentStickerPreviewCell != null) {
                    currentStickerPreviewCell.setScaled(false);
                    currentStickerPreviewCell = null;
                }
                StickerEmojiCell cell = (StickerEmojiCell) view;
                if (cell.isDisabled()) {
                    return;
                }
                cell.disable();
                TLRPC.Document document = cell.getSticker();
                Integer count = stickersUseHistory.get(document.id);
                if (count == null) {
                    count = 0;
                }
                if (count == 0 && stickersUseHistory.size() > 19) {
                    for (int a = recentStickers.size() - 1; a >= 0; a--) {
                        TLRPC.Document sticker = recentStickers.get(a);
                        stickersUseHistory.remove(sticker.id);
                        recentStickers.remove(a);
                        if (stickersUseHistory.size() <= 19) {
                            break;
                        }
                    }
                }
                stickersUseHistory.put(document.id, ++count);

                long id = StickersQuery.getStickerSetId(document);
                if (id != -1) {
                    count = stickerSetsUseCount.get(id);
                    if (count == null) {
                        count = 0;
                    }
                    stickerSetsUseCount.put(id, ++count);
                }

                saveRecentStickers();
                if (listener != null) {
                    listener.onStickerSelected(document);
                }
            }
        };
        stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
        AndroidUtilities.setListViewEdgeEffectColor(stickersGridView, 0xfff5f6f7);

        stickersWrap = new FrameLayout(context);
        stickersWrap.addView(stickersGridView);

        TextView textView = new TextView(context);
        textView.setText(LocaleController.getString("NoStickers", R.string.NoStickers));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        textView.setTextColor(0xff888888);
        stickersWrap.addView(textView,
                LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER));
        stickersGridView.setEmptyView(textView);

        scrollSlidingTabStrip = new ScrollSlidingTabStrip(context) {

            boolean startedScroll;
            float lastX;
            float lastTranslateX;
            boolean first = true;

            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                return super.onInterceptTouchEvent(ev);
            }

            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (Build.VERSION.SDK_INT >= 11) {
                    if (first) {
                        first = false;
                        lastX = ev.getX();
                    }
                    float newTranslationX = ViewProxy.getTranslationX(scrollSlidingTabStrip);
                    if (scrollSlidingTabStrip.getScrollX() == 0 && newTranslationX == 0) {
                        if (!startedScroll && lastX - ev.getX() < 0) {
                            if (pager.beginFakeDrag()) {
                                startedScroll = true;
                                lastTranslateX = ViewProxy.getTranslationX(scrollSlidingTabStrip);
                            }
                        } else if (startedScroll && lastX - ev.getX() > 0) {
                            if (pager.isFakeDragging()) {
                                pager.endFakeDrag();
                                startedScroll = false;
                            }
                        }
                    }
                    if (startedScroll) {
                        int dx = (int) (ev.getX() - lastX + newTranslationX - lastTranslateX);
                        try {
                            pager.fakeDragBy(dx);
                            lastTranslateX = newTranslationX;
                        } catch (Exception e) {
                            try {
                                pager.endFakeDrag();
                            } catch (Exception e2) {
                                //don't promt
                            }
                            startedScroll = false;
                            FileLog.e("tmessages", e);
                        }
                    }
                    lastX = ev.getX();
                    if (ev.getAction() == MotionEvent.ACTION_CANCEL
                            || ev.getAction() == MotionEvent.ACTION_UP) {
                        first = true;
                        if (startedScroll) {
                            pager.endFakeDrag();
                            startedScroll = false;
                        }
                    }
                    return startedScroll || super.onTouchEvent(ev);
                }
                return super.onTouchEvent(ev);
            }
        };
        scrollSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
        scrollSlidingTabStrip.setIndicatorColor(0xffe2e5e7);
        scrollSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
        scrollSlidingTabStrip.setVisibility(INVISIBLE);
        addView(scrollSlidingTabStrip,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.TOP));
        ViewProxy.setTranslationX(scrollSlidingTabStrip, AndroidUtilities.displaySize.x);
        updateStickerTabs();
        scrollSlidingTabStrip.setDelegate(new ScrollSlidingTabStrip.ScrollSlidingTabStripDelegate() {
            @Override
            public void onPageSelected(int page) {
                if (page == 0) {
                    pager.setCurrentItem(0);
                    return;
                } else if (page == 1 && !recentStickers.isEmpty()) {
                    views.get(6).setSelection(0);
                    return;
                }
                int index = page - (recentStickers.isEmpty() ? 1 : 2);
                if (index >= stickerSets.size()) {
                    index = stickerSets.size() - 1;
                }
                views.get(6).setSelection(stickersGridAdapter.getPositionForPack(stickerSets.get(index)));
            }
        });

        stickersGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                int count = view.getChildCount();
                for (int a = 0; a < count; a++) {
                    View child = view.getChildAt(a);
                    if (child.getHeight() + child.getTop() < AndroidUtilities.dp(5)) {
                        firstVisibleItem++;
                    } else {
                        break;
                    }
                }
                scrollSlidingTabStrip
                        .onPageScrolled(stickersGridAdapter.getTabForPosition(firstVisibleItem) + 1, 0);
            }
        });
    }

    setBackgroundColor(0xfff5f6f7);

    pager = new ViewPager(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pager.setAdapter(new EmojiPagesAdapter());

    pagerSlidingTabStripContainer = new LinearLayout(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pagerSlidingTabStripContainer.setOrientation(LinearLayout.HORIZONTAL);
    pagerSlidingTabStripContainer.setBackgroundColor(0xfff5f6f7);
    addView(pagerSlidingTabStripContainer, LayoutHelper.createFrame(LayoutParams.MATCH_PARENT, 48));

    PagerSlidingTabStrip pagerSlidingTabStrip = new PagerSlidingTabStrip(context);
    pagerSlidingTabStrip.setViewPager(pager);
    pagerSlidingTabStrip.setShouldExpand(true);
    pagerSlidingTabStrip.setIndicatorHeight(AndroidUtilities.dp(2));
    pagerSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
    pagerSlidingTabStrip.setIndicatorColor(0xff2b96e2);
    pagerSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
    pagerSlidingTabStripContainer.addView(pagerSlidingTabStrip, LayoutHelper.createLinear(0, 48, 1.0f));
    pagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            EmojiView.this.onPageScrolled(position, getMeasuredWidth(), positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    FrameLayout frameLayout = new FrameLayout(context);
    pagerSlidingTabStripContainer.addView(frameLayout, LayoutHelper.createLinear(52, 48));

    backspaceButton = new ImageView(context) {
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                backspacePressed = true;
                backspaceOnce = false;
                postBackspaceRunnable(350);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL
                    || event.getAction() == MotionEvent.ACTION_UP) {
                backspacePressed = false;
                if (!backspaceOnce) {
                    if (listener != null && listener.onBackspace()) {
                        backspaceButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                    }
                }
            }
            super.onTouchEvent(event);
            return true;
        }
    };
    backspaceButton.setImageResource(R.drawable.ic_smiles_backspace);
    backspaceButton.setBackgroundResource(R.drawable.ic_emoji_backspace);
    backspaceButton.setScaleType(ImageView.ScaleType.CENTER);
    frameLayout.addView(backspaceButton, LayoutHelper.createFrame(52, 48));

    View view = new View(context);
    view.setBackgroundColor(0xffe2e5e7);
    frameLayout.addView(view, LayoutHelper.createFrame(52, 1, Gravity.LEFT | Gravity.BOTTOM));

    recentsWrap = new FrameLayout(context);
    recentsWrap.addView(views.get(0));

    TextView textView = new TextView(context);
    textView.setText(LocaleController.getString("NoRecent", R.string.NoRecent));
    textView.setTextSize(18);
    textView.setTextColor(0xff888888);
    textView.setGravity(Gravity.CENTER);
    recentsWrap.addView(textView);
    views.get(0).setEmptyView(textView);

    addView(pager, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT,
            Gravity.LEFT | Gravity.TOP, 0, 48, 0, 0));

    loadRecents();

    if (Emoji.data[0] == null || Emoji.data[0].length == 0) {
        pager.setCurrentItem(1);
    }
}

From source file:com.example.spencerdepas.translationapp.activities.DMVStudyActivity.java

private void showSelectQuestionDialog() {
    Log.d(TAG, "showAlertDialog");
    // Prepare grid view
    GridView gridView = new GridView(this);

    int questionSize = driverQuestions.getQuestions().size();
    List<Integer> mList = new ArrayList<Integer>();
    for (int i = 1; i < questionSize + 1; i++) {
        mList.add(i);//from   w  w w. j a  v a2 s.  c  o  m
    }

    gridView.setAdapter(new ArrayAdapter<Integer>(this, R.layout.custom_list_item, mList) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            boolean hasBeenAnswered = hasBeenAnswered(position);
            Log.d(TAG, "hasBeenAnswered : " + hasBeenAnswered);
            int color = 0x00FFFFFF; // Transparent
            if (hasBeenAnswered) {

                if (driverQuestions.getQuestions().get(position).isAnsweredCorrectly()) {
                    //answer is correct
                    view.setBackgroundColor(getResources().getColor(R.color.colorForQuestionGrid));
                } else {
                    //answer is incorrect
                    view.setBackgroundColor(getResources().getColor(R.color.red));
                }

            } else {

                view.setBackgroundColor(color);
            }

            return view;
        }
    });
    gridView.setNumColumns(4);

    // Set grid view to alertDialog
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(gridView);
    builder.setTitle(getResources().getString(R.string.select_question));
    builder.setPositiveButton(getResources().getString(R.string.dialog_dismiss),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.d(TAG, getResources().getString(R.string.dialog_dismiss));
                    dialog.dismiss();
                }
            });

    final AlertDialog ad = builder.show();

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // do something here
            Log.d(TAG, "onclick");

            if (position == 0) {
                makePrevousButtonUnclickable();
            } else {
                makePrevousButtonClickable();
            }

            mNextButton.setText(getResources().getString(R.string.next_button));
            Log.d(TAG, "int pos : " + position);
            if (position == 193) {
                mNextButton.setText(getResources().getString(R.string.finish_studying));
            }

            goToSelectedQuestion(position);
            ad.dismiss();
        }
    });

}

From source file:com.goftagram.telegram.ui.Components.EmojiView.java

public EmojiView(boolean needStickers, Context context) {
    super(context);

    showStickers = needStickers;//w ww  . jav a 2s .  c  o m

    for (int i = 0; i < EmojiData.dataColored.length + 1; i++) {
        GridView gridView = new GridView(context);
        if (AndroidUtilities.isTablet()) {
            gridView.setColumnWidth(AndroidUtilities.dp(60));
        } else {
            gridView.setColumnWidth(AndroidUtilities.dp(45));
        }
        gridView.setNumColumns(-1);
        views.add(gridView);

        EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(i - 1);
        gridView.setAdapter(emojiGridAdapter);
        AndroidUtilities.setListViewEdgeEffectColor(gridView, 0xfff5f6f7);
        adapters.add(emojiGridAdapter);
    }

    if (showStickers) {
        StickersQuery.checkStickers();
        stickersGridView = new GridView(context) {
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    int x = (int) event.getX();
                    int y = (int) event.getY();
                    int count = stickersGridView.getChildCount();
                    for (int a = 0; a < count; a++) {
                        View view = stickersGridView.getChildAt(a);
                        int top = view.getTop();
                        int bottom = view.getBottom();
                        int left = view.getLeft();
                        int right = view.getRight();
                        if (top > y || bottom < y || left > x || right < x) {
                            continue;
                        }
                        if (!(view instanceof StickerEmojiCell) || !((StickerEmojiCell) view).showingBitmap()) {
                            return super.onInterceptTouchEvent(event);
                        }
                        startX = x;
                        startY = y;
                        currentStickerPreviewCell = (StickerEmojiCell) view;
                        openStickerPreviewRunnable = new Runnable() {
                            @Override
                            public void run() {
                                if (openStickerPreviewRunnable == null) {
                                    return;
                                }
                                stickersGridView.setOnItemClickListener(null);
                                stickersGridView.requestDisallowInterceptTouchEvent(true);
                                openStickerPreviewRunnable = null;
                                StickerPreviewViewer.getInstance().setParentActivity((Activity) getContext());
                                StickerPreviewViewer.getInstance()
                                        .setKeyboardHeight(EmojiView.this.getMeasuredHeight());
                                StickerPreviewViewer.getInstance().open(currentStickerPreviewCell.getSticker());
                                currentStickerPreviewCell.setScaled(true);
                            }
                        };
                        AndroidUtilities.runOnUIThread(openStickerPreviewRunnable, 200);
                        return true;
                    }
                }
                return false;
            }
        };
        stickersGridView.setSelector(R.drawable.transparent);
        stickersGridView.setColumnWidth(AndroidUtilities.dp(72));
        stickersGridView.setNumColumns(-1);
        stickersGridView.setPadding(0, AndroidUtilities.dp(4), 0, 0);
        stickersGridView.setClipToPadding(false);
        views.add(stickersGridView);
        stickersGridAdapter = new StickersGridAdapter(context);
        stickersGridView.setAdapter(stickersGridAdapter);
        stickersGridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (openStickerPreviewRunnable != null || StickerPreviewViewer.getInstance().isVisible()) {
                    if (event.getAction() == MotionEvent.ACTION_UP
                            || event.getAction() == MotionEvent.ACTION_CANCEL
                            || event.getAction() == MotionEvent.ACTION_POINTER_UP) {
                        AndroidUtilities.runOnUIThread(new Runnable() {
                            @Override
                            public void run() {
                                stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
                            }
                        }, 150);
                        if (openStickerPreviewRunnable != null) {
                            AndroidUtilities.cancelRunOnUIThread(openStickerPreviewRunnable);
                            openStickerPreviewRunnable = null;
                        } else if (StickerPreviewViewer.getInstance().isVisible()) {
                            StickerPreviewViewer.getInstance().close();
                            if (currentStickerPreviewCell != null) {
                                currentStickerPreviewCell.setScaled(false);
                                currentStickerPreviewCell = null;
                            }
                        }
                    } else if (event.getAction() != MotionEvent.ACTION_DOWN) {
                        if (StickerPreviewViewer.getInstance().isVisible()) {
                            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                                int x = (int) event.getX();
                                int y = (int) event.getY();
                                int count = stickersGridView.getChildCount();
                                for (int a = 0; a < count; a++) {
                                    View view = stickersGridView.getChildAt(a);
                                    int top = view.getTop();
                                    int bottom = view.getBottom();
                                    int left = view.getLeft();
                                    int right = view.getRight();
                                    if (top > y || bottom < y || left > x || right < x) {
                                        continue;
                                    }
                                    if (!(view instanceof StickerEmojiCell)
                                            || view == currentStickerPreviewCell) {
                                        break;
                                    }
                                    if (currentStickerPreviewCell != null) {
                                        currentStickerPreviewCell.setScaled(false);
                                    }
                                    currentStickerPreviewCell = (StickerEmojiCell) view;
                                    StickerPreviewViewer.getInstance()
                                            .setKeyboardHeight(EmojiView.this.getMeasuredHeight());
                                    StickerPreviewViewer.getInstance()
                                            .open(currentStickerPreviewCell.getSticker());
                                    currentStickerPreviewCell.setScaled(true);
                                    return true;
                                }
                            }
                            return true;
                        } else if (openStickerPreviewRunnable != null) {
                            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                                if (Math.hypot(startX - event.getX(), startY - event.getY()) > AndroidUtilities
                                        .dp(10)) {
                                    AndroidUtilities.cancelRunOnUIThread(openStickerPreviewRunnable);
                                    openStickerPreviewRunnable = null;
                                }
                            } else {
                                AndroidUtilities.cancelRunOnUIThread(openStickerPreviewRunnable);
                                openStickerPreviewRunnable = null;
                            }
                        }
                    }
                }
                return false;
            }
        });
        stickersOnItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long i) {
                if (!(view instanceof StickerEmojiCell)) {
                    return;
                }
                if (openStickerPreviewRunnable != null) {
                    AndroidUtilities.cancelRunOnUIThread(openStickerPreviewRunnable);
                    openStickerPreviewRunnable = null;
                }
                if (currentStickerPreviewCell != null) {
                    currentStickerPreviewCell.setScaled(false);
                    currentStickerPreviewCell = null;
                }
                StickerEmojiCell cell = (StickerEmojiCell) view;
                if (cell.isDisabled()) {
                    return;
                }
                cell.disable();
                TLRPC.Document document = cell.getSticker();
                Integer count = stickersUseHistory.get(document.id);
                if (count == null) {
                    count = 0;
                }
                if (count == 0 && stickersUseHistory.size() > 19) {
                    for (int a = recentStickers.size() - 1; a >= 0; a--) {
                        TLRPC.Document sticker = recentStickers.get(a);
                        stickersUseHistory.remove(sticker.id);
                        recentStickers.remove(a);
                        if (stickersUseHistory.size() <= 19) {
                            break;
                        }
                    }
                }
                stickersUseHistory.put(document.id, ++count);

                saveRecentStickers();
                if (listener != null) {
                    listener.onStickerSelected(document);
                }
            }
        };
        stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
        AndroidUtilities.setListViewEdgeEffectColor(stickersGridView, 0xfff5f6f7);

        stickersWrap = new FrameLayout(context);
        stickersWrap.addView(stickersGridView);

        TextView textView = new TextView(context);
        textView.setText(LocaleController.getString("NoStickers", R.string.NoStickers));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        textView.setTextColor(0xff888888);
        stickersWrap.addView(textView,
                LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER));
        stickersGridView.setEmptyView(textView);

        scrollSlidingTabStrip = new ScrollSlidingTabStrip(context) {

            boolean startedScroll;
            float lastX;
            float lastTranslateX;
            boolean first = true;

            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                return super.onInterceptTouchEvent(ev);
            }

            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (Build.VERSION.SDK_INT >= 11) {
                    if (first) {
                        first = false;
                        lastX = ev.getX();
                    }
                    float newTranslationX = ViewProxy.getTranslationX(scrollSlidingTabStrip);
                    if (scrollSlidingTabStrip.getScrollX() == 0 && newTranslationX == 0) {
                        if (!startedScroll && lastX - ev.getX() < 0) {
                            if (pager.beginFakeDrag()) {
                                startedScroll = true;
                                lastTranslateX = ViewProxy.getTranslationX(scrollSlidingTabStrip);
                            }
                        } else if (startedScroll && lastX - ev.getX() > 0) {
                            if (pager.isFakeDragging()) {
                                pager.endFakeDrag();
                                startedScroll = false;
                            }
                        }
                    }
                    if (startedScroll) {
                        int dx = (int) (ev.getX() - lastX + newTranslationX - lastTranslateX);
                        try {
                            pager.fakeDragBy(dx);
                            lastTranslateX = newTranslationX;
                        } catch (Exception e) {
                            try {
                                pager.endFakeDrag();
                            } catch (Exception e2) {
                                //don't promt
                            }
                            startedScroll = false;
                            FileLog.e("tmessages", e);
                        }
                    }
                    lastX = ev.getX();
                    if (ev.getAction() == MotionEvent.ACTION_CANCEL
                            || ev.getAction() == MotionEvent.ACTION_UP) {
                        first = true;
                        if (startedScroll) {
                            pager.endFakeDrag();
                            startedScroll = false;
                        }
                    }
                    return startedScroll || super.onTouchEvent(ev);
                }
                return super.onTouchEvent(ev);
            }
        };
        scrollSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
        scrollSlidingTabStrip.setIndicatorColor(0xffe2e5e7);
        scrollSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
        scrollSlidingTabStrip.setVisibility(INVISIBLE);
        addView(scrollSlidingTabStrip,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.TOP));
        ViewProxy.setTranslationX(scrollSlidingTabStrip, AndroidUtilities.displaySize.x);
        updateStickerTabs();
        scrollSlidingTabStrip.setDelegate(new ScrollSlidingTabStrip.ScrollSlidingTabStripDelegate() {
            @Override
            public void onPageSelected(int page) {
                if (page == 0) {
                    pager.setCurrentItem(0);
                    return;
                } else if (page == 1 && !recentStickers.isEmpty()) {
                    views.get(6).setSelection(0);
                    return;
                }
                int index = page - (recentStickers.isEmpty() ? 1 : 2);
                if (index == stickerSets.size()) {
                    if (listener != null) {
                        listener.onStickersSettingsClick();
                    }
                    return;
                }
                if (index >= stickerSets.size()) {
                    index = stickerSets.size() - 1;
                }
                views.get(6).setSelection(stickersGridAdapter.getPositionForPack(stickerSets.get(index)));
            }
        });

        stickersGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                int count = view.getChildCount();
                for (int a = 0; a < count; a++) {
                    View child = view.getChildAt(a);
                    if (child.getHeight() + child.getTop() < AndroidUtilities.dp(5)) {
                        firstVisibleItem++;
                    } else {
                        break;
                    }
                }
                scrollSlidingTabStrip
                        .onPageScrolled(stickersGridAdapter.getTabForPosition(firstVisibleItem) + 1, 0);
            }
        });
    }

    setBackgroundColor(0xfff5f6f7);

    pager = new ViewPager(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pager.setAdapter(new EmojiPagesAdapter());

    pagerSlidingTabStripContainer = new LinearLayout(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pagerSlidingTabStripContainer.setOrientation(LinearLayout.HORIZONTAL);
    pagerSlidingTabStripContainer.setBackgroundColor(0xfff5f6f7);
    addView(pagerSlidingTabStripContainer, LayoutHelper.createFrame(LayoutParams.MATCH_PARENT, 48));

    PagerSlidingTabStrip pagerSlidingTabStrip = new PagerSlidingTabStrip(context);
    pagerSlidingTabStrip.setViewPager(pager);
    pagerSlidingTabStrip.setShouldExpand(true);
    pagerSlidingTabStrip.setIndicatorHeight(AndroidUtilities.dp(2));
    pagerSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
    pagerSlidingTabStrip.setIndicatorColor(0xff2b96e2);
    pagerSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
    pagerSlidingTabStripContainer.addView(pagerSlidingTabStrip, LayoutHelper.createLinear(0, 48, 1.0f));
    pagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            EmojiView.this.onPageScrolled(position, getMeasuredWidth(), positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    FrameLayout frameLayout = new FrameLayout(context);
    pagerSlidingTabStripContainer.addView(frameLayout, LayoutHelper.createLinear(52, 48));

    backspaceButton = new ImageView(context) {
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                backspacePressed = true;
                backspaceOnce = false;
                postBackspaceRunnable(350);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL
                    || event.getAction() == MotionEvent.ACTION_UP) {
                backspacePressed = false;
                if (!backspaceOnce) {
                    if (listener != null && listener.onBackspace()) {
                        backspaceButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                    }
                }
            }
            super.onTouchEvent(event);
            return true;
        }
    };
    backspaceButton.setImageResource(R.drawable.ic_smiles_backspace);
    backspaceButton.setBackgroundResource(R.drawable.ic_emoji_backspace);
    backspaceButton.setScaleType(ImageView.ScaleType.CENTER);
    frameLayout.addView(backspaceButton, LayoutHelper.createFrame(52, 48));

    View view = new View(context);
    view.setBackgroundColor(0xffe2e5e7);
    frameLayout.addView(view, LayoutHelper.createFrame(52, 1, Gravity.LEFT | Gravity.BOTTOM));

    recentsWrap = new FrameLayout(context);
    recentsWrap.addView(views.get(0));

    TextView textView = new TextView(context);
    textView.setText(LocaleController.getString("NoRecent", R.string.NoRecent));
    textView.setTextSize(18);
    textView.setTextColor(0xff888888);
    textView.setGravity(Gravity.CENTER);
    recentsWrap.addView(textView);
    views.get(0).setEmptyView(textView);

    addView(pager, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT,
            Gravity.LEFT | Gravity.TOP, 0, 48, 0, 0));

    emojiSize = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 40 : 32);
    pickerView = new EmojiColorPickerView(context);
    pickerViewPopup = new EmojiPopupWindow(pickerView,
            popupWidth = AndroidUtilities.dp((AndroidUtilities.isTablet() ? 40 : 32) * 6 + 10 + 4 * 5),
            popupHeight = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 64 : 56));
    pickerViewPopup.setOutsideTouchable(true);
    pickerViewPopup.setClippingEnabled(true);
    pickerViewPopup.setInputMethodMode(EmojiPopupWindow.INPUT_METHOD_NOT_NEEDED);
    pickerViewPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
    pickerViewPopup.getContentView().setFocusableInTouchMode(true);
    pickerViewPopup.getContentView().setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
                    && event.getAction() == KeyEvent.ACTION_UP && pickerViewPopup != null
                    && pickerViewPopup.isShowing()) {
                pickerViewPopup.dismiss();
                return true;
            }
            return false;
        }
    });

    loadRecents();
}

From source file:org.telegram.ui.Components.EmojiView.java

public EmojiView(boolean needStickers, Context context) {
    super(context);

    showStickers = needStickers;//from ww w .  j a  v a  2s  .  c o m

    //Creates 5 grid views for each emoji set +
    //for (int i = 0; i < EmojiData.dataColored.length; i++) {
    GridView gridView = new GridView(context);
    if (AndroidUtilities.isTablet()) {
        gridView.setColumnWidth(AndroidUtilities.dp(60));
    } else {
        gridView.setColumnWidth(AndroidUtilities.dp(45));
    }
    gridView.setNumColumns(-1);
    views.add(gridView);

    //EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(i-1);
    EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(-1);
    gridView.setAdapter(emojiGridAdapter);
    AndroidUtilities.setListViewEdgeEffectColor(gridView, 0xfff5f6f7);
    adapters.add(emojiGridAdapter);
    //}

    if (showStickers) {
        StickersQuery.checkStickers();
        stickersGridView = new GridView(context) {
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    int x = (int) event.getX();
                    int y = (int) event.getY();
                    int count = stickersGridView.getChildCount();
                    for (int a = 0; a < count; a++) {
                        View view = stickersGridView.getChildAt(a);
                        int top = view.getTop();
                        int bottom = view.getBottom();
                        int left = view.getLeft();
                        int right = view.getRight();
                        if (top > y || bottom < y || left > x || right < x) {
                            continue;
                        }
                        if (!(view instanceof StickerEmojiCell) || !((StickerEmojiCell) view).showingBitmap()) {
                            return super.onInterceptTouchEvent(event);
                        }
                        startX = x;
                        startY = y;
                        currentStickerPreviewCell = (StickerEmojiCell) view;
                        openStickerPreviewRunnable = new Runnable() {
                            @Override
                            public void run() {
                                if (openStickerPreviewRunnable == null) {
                                    return;
                                }
                                stickersGridView.setOnItemClickListener(null);
                                stickersGridView.requestDisallowInterceptTouchEvent(true);
                                openStickerPreviewRunnable = null;
                                StickerPreviewViewer.getInstance().setParentActivity((Activity) getContext());
                                StickerPreviewViewer.getInstance()
                                        .setKeyboardHeight(EmojiView.this.getMeasuredHeight());
                                StickerPreviewViewer.getInstance().open(currentStickerPreviewCell.getSticker());
                                currentStickerPreviewCell.setScaled(true);
                            }
                        };
                        AndroidUtilities.runOnUIThread(openStickerPreviewRunnable, 200);
                        return true;
                    }
                }
                return false;
            }
        };
        stickersGridView.setSelector(R.drawable.transparent);
        stickersGridView.setColumnWidth(AndroidUtilities.dp(72));
        stickersGridView.setNumColumns(-1);
        stickersGridView.setPadding(0, AndroidUtilities.dp(4), 0, 0);
        stickersGridView.setClipToPadding(false);
        views.add(stickersGridView);
        stickersGridAdapter = new StickersGridAdapter(context);
        stickersGridView.setAdapter(stickersGridAdapter);
        stickersGridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (openStickerPreviewRunnable != null || StickerPreviewViewer.getInstance().isVisible()) {
                    if (event.getAction() == MotionEvent.ACTION_UP
                            || event.getAction() == MotionEvent.ACTION_CANCEL
                            || event.getAction() == MotionEvent.ACTION_POINTER_UP) {
                        AndroidUtilities.runOnUIThread(new Runnable() {
                            @Override
                            public void run() {
                                stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
                            }
                        }, 150);
                        if (openStickerPreviewRunnable != null) {
                            AndroidUtilities.cancelRunOnUIThread(openStickerPreviewRunnable);
                            openStickerPreviewRunnable = null;
                        } else if (StickerPreviewViewer.getInstance().isVisible()) {
                            StickerPreviewViewer.getInstance().close();
                            if (currentStickerPreviewCell != null) {
                                currentStickerPreviewCell.setScaled(false);
                                currentStickerPreviewCell = null;
                            }
                        }
                    } else if (event.getAction() != MotionEvent.ACTION_DOWN) {
                        if (StickerPreviewViewer.getInstance().isVisible()) {
                            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                                int x = (int) event.getX();
                                int y = (int) event.getY();
                                int count = stickersGridView.getChildCount();
                                for (int a = 0; a < count; a++) {
                                    View view = stickersGridView.getChildAt(a);
                                    int top = view.getTop();
                                    int bottom = view.getBottom();
                                    int left = view.getLeft();
                                    int right = view.getRight();
                                    if (top > y || bottom < y || left > x || right < x) {
                                        continue;
                                    }
                                    if (!(view instanceof StickerEmojiCell)
                                            || view == currentStickerPreviewCell) {
                                        break;
                                    }
                                    if (currentStickerPreviewCell != null) {
                                        currentStickerPreviewCell.setScaled(false);
                                    }
                                    currentStickerPreviewCell = (StickerEmojiCell) view;
                                    StickerPreviewViewer.getInstance()
                                            .setKeyboardHeight(EmojiView.this.getMeasuredHeight());
                                    StickerPreviewViewer.getInstance()
                                            .open(currentStickerPreviewCell.getSticker());
                                    currentStickerPreviewCell.setScaled(true);
                                    return true;
                                }
                            }
                            return true;
                        } else if (openStickerPreviewRunnable != null) {
                            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                                if (Math.hypot(startX - event.getX(), startY - event.getY()) > AndroidUtilities
                                        .dp(10)) {
                                    AndroidUtilities.cancelRunOnUIThread(openStickerPreviewRunnable);
                                    openStickerPreviewRunnable = null;
                                }
                            } else {
                                AndroidUtilities.cancelRunOnUIThread(openStickerPreviewRunnable);
                                openStickerPreviewRunnable = null;
                            }
                        }
                    }
                }
                return false;
            }
        });
        stickersOnItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long i) {
                if (!(view instanceof StickerEmojiCell)) {
                    return;
                }
                if (openStickerPreviewRunnable != null) {
                    AndroidUtilities.cancelRunOnUIThread(openStickerPreviewRunnable);
                    openStickerPreviewRunnable = null;
                }
                if (currentStickerPreviewCell != null) {
                    currentStickerPreviewCell.setScaled(false);
                    currentStickerPreviewCell = null;
                }
                StickerEmojiCell cell = (StickerEmojiCell) view;
                if (cell.isDisabled()) {
                    return;
                }
                cell.disable();
                TLRPC.Document document = cell.getSticker();
                Integer count = stickersUseHistory.get(document.id);
                if (count == null) {
                    count = 0;
                }
                if (count == 0 && stickersUseHistory.size() > 19) {
                    for (int a = recentStickers.size() - 1; a >= 0; a--) {
                        TLRPC.Document sticker = recentStickers.get(a);
                        stickersUseHistory.remove(sticker.id);
                        recentStickers.remove(a);
                        if (stickersUseHistory.size() <= 19) {
                            break;
                        }
                    }
                }
                stickersUseHistory.put(document.id, ++count);

                long id = StickersQuery.getStickerSetId(document);
                if (id != -1) {
                    count = stickerSetsUseCount.get(id);
                    if (count == null) {
                        count = 0;
                    }
                    stickerSetsUseCount.put(id, ++count);
                }

                saveRecentStickers();
                if (listener != null) {
                    listener.onStickerSelected(document);
                }
            }
        };
        stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
        AndroidUtilities.setListViewEdgeEffectColor(stickersGridView, 0xfff5f6f7);

        stickersWrap = new FrameLayout(context);
        stickersWrap.addView(stickersGridView);

        TextView textView = new TextView(context);
        textView.setText(LocaleController.getString("NoStickers", R.string.NoStickers));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        textView.setTextColor(0xff888888);
        stickersWrap.addView(textView,
                LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER));
        stickersGridView.setEmptyView(textView);

        scrollSlidingTabStrip = new ScrollSlidingTabStrip(context) {

            boolean startedScroll;
            float lastX;
            float lastTranslateX;
            boolean first = true;

            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                return super.onInterceptTouchEvent(ev);
            }

            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (Build.VERSION.SDK_INT >= 11) {
                    if (first) {
                        first = false;
                        lastX = ev.getX();
                    }
                    float newTranslationX = ViewProxy.getTranslationX(scrollSlidingTabStrip);
                    if (scrollSlidingTabStrip.getScrollX() == 0 && newTranslationX == 0) {
                        if (!startedScroll && lastX - ev.getX() < 0) {
                            if (pager.beginFakeDrag()) {
                                startedScroll = true;
                                lastTranslateX = ViewProxy.getTranslationX(scrollSlidingTabStrip);
                            }
                        } else if (startedScroll && lastX - ev.getX() > 0) {
                            if (pager.isFakeDragging()) {
                                pager.endFakeDrag();
                                startedScroll = false;
                            }
                        }
                    }
                    if (startedScroll) {
                        int dx = (int) (ev.getX() - lastX + newTranslationX - lastTranslateX);
                        try {
                            pager.fakeDragBy(dx);
                            lastTranslateX = newTranslationX;
                        } catch (Exception e) {
                            try {
                                pager.endFakeDrag();
                            } catch (Exception e2) {
                                //don't promt
                            }
                            startedScroll = false;
                            FileLog.e("tmessages", e);
                        }
                    }
                    lastX = ev.getX();
                    if (ev.getAction() == MotionEvent.ACTION_CANCEL
                            || ev.getAction() == MotionEvent.ACTION_UP) {
                        first = true;
                        if (startedScroll) {
                            pager.endFakeDrag();
                            startedScroll = false;
                        }
                    }
                    return startedScroll || super.onTouchEvent(ev);
                }
                return super.onTouchEvent(ev);
            }
        };
        scrollSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
        scrollSlidingTabStrip.setIndicatorColor(0xffe2e5e7);
        scrollSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
        scrollSlidingTabStrip.setVisibility(INVISIBLE);
        addView(scrollSlidingTabStrip,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.TOP));
        ViewProxy.setTranslationX(scrollSlidingTabStrip, AndroidUtilities.displaySize.x);
        updateStickerTabs();
        scrollSlidingTabStrip.setDelegate(new ScrollSlidingTabStrip.ScrollSlidingTabStripDelegate() {
            @Override
            public void onPageSelected(int page) {
                if (page == 0) {
                    pager.setCurrentItem(0);
                    return;
                } else if (page == 1 && !recentStickers.isEmpty()) {
                    views.get(6).setSelection(0);
                    return;
                }
                int index = page - (recentStickers.isEmpty() ? 1 : 2);
                if (index >= stickerSets.size()) {
                    index = stickerSets.size() - 1;
                }
                views.get(6).setSelection(stickersGridAdapter.getPositionForPack(stickerSets.get(index)));
            }
        });

        stickersGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                int count = view.getChildCount();
                for (int a = 0; a < count; a++) {
                    View child = view.getChildAt(a);
                    if (child.getHeight() + child.getTop() < AndroidUtilities.dp(5)) {
                        firstVisibleItem++;
                    } else {
                        break;
                    }
                }
                scrollSlidingTabStrip
                        .onPageScrolled(stickersGridAdapter.getTabForPosition(firstVisibleItem) + 1, 0);
            }
        });
    }

    setBackgroundColor(0xfff5f6f7);

    pager = new ViewPager(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pager.setAdapter(new EmojiPagesAdapter());

    pagerSlidingTabStripContainer = new LinearLayout(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pagerSlidingTabStripContainer.setOrientation(LinearLayout.HORIZONTAL);
    pagerSlidingTabStripContainer.setBackgroundColor(0xfff5f6f7);
    addView(pagerSlidingTabStripContainer, LayoutHelper.createFrame(LayoutParams.MATCH_PARENT, 48));

    PagerSlidingTabStrip pagerSlidingTabStrip = new PagerSlidingTabStrip(context);
    pagerSlidingTabStrip.setViewPager(pager);
    pagerSlidingTabStrip.setShouldExpand(true);
    pagerSlidingTabStrip.setIndicatorHeight(AndroidUtilities.dp(2));
    pagerSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
    pagerSlidingTabStrip.setIndicatorColor(0xff2b96e2);
    pagerSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
    pagerSlidingTabStripContainer.addView(pagerSlidingTabStrip, LayoutHelper.createLinear(0, 48, 1.0f));
    pagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            EmojiView.this.onPageScrolled(position, getMeasuredWidth(), positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    FrameLayout frameLayout = new FrameLayout(context);
    pagerSlidingTabStripContainer.addView(frameLayout, LayoutHelper.createLinear(52, 48));

    backspaceButton = new ImageView(context) {
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                backspacePressed = true;
                backspaceOnce = false;
                postBackspaceRunnable(350);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL
                    || event.getAction() == MotionEvent.ACTION_UP) {
                backspacePressed = false;
                if (!backspaceOnce) {
                    if (listener != null && listener.onBackspace()) {
                        backspaceButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                    }
                }
            }
            super.onTouchEvent(event);
            return true;
        }
    };
    backspaceButton.setImageResource(R.drawable.ic_smiles_backspace);
    backspaceButton.setBackgroundResource(R.drawable.ic_emoji_backspace);
    backspaceButton.setScaleType(ImageView.ScaleType.CENTER);
    frameLayout.addView(backspaceButton, LayoutHelper.createFrame(52, 48));

    View view = new View(context);
    view.setBackgroundColor(0xffe2e5e7);
    frameLayout.addView(view, LayoutHelper.createFrame(52, 1, Gravity.LEFT | Gravity.BOTTOM));

    recentsWrap = new FrameLayout(context);
    recentsWrap.addView(views.get(0));

    TextView textView = new TextView(context);
    textView.setText(LocaleController.getString("NoRecent", R.string.NoRecent));
    textView.setTextSize(18);
    textView.setTextColor(0xff888888);
    textView.setGravity(Gravity.CENTER);
    recentsWrap.addView(textView);
    views.get(0).setEmptyView(textView);

    addView(pager, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT,
            Gravity.LEFT | Gravity.TOP, 0, 48, 0, 0));

    emojiSize = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 40 : 32);
    pickerView = new EmojiColorPickerView(context);
    pickerViewPopup = new EmojiPopupWindow(pickerView,
            popupWidth = AndroidUtilities.dp((AndroidUtilities.isTablet() ? 40 : 32) * 6 + 10 + 4 * 5),
            popupHeight = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 64 : 56));
    pickerViewPopup.setOutsideTouchable(true);
    pickerViewPopup.setClippingEnabled(true);
    pickerViewPopup.setInputMethodMode(EmojiPopupWindow.INPUT_METHOD_NOT_NEEDED);
    pickerViewPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
    pickerViewPopup.getContentView().setFocusableInTouchMode(true);
    pickerViewPopup.getContentView().setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
                    && event.getAction() == KeyEvent.ACTION_UP && pickerViewPopup != null
                    && pickerViewPopup.isShowing()) {
                pickerViewPopup.dismiss();
                return true;
            }
            return false;
        }
    });

    loadRecents();
}

From source file:com.panahit.ui.Components.EmojiView.java

public EmojiView(boolean needStickers, boolean needGif, final Context context) {
    super(context);

    showStickers = needStickers;/*w ww.j a  v a 2 s . c  o m*/
    showGifs = needGif;

    for (int i = 0; i < EmojiData.dataColored.length + 1; i++) {
        GridView gridView = new GridView(context);
        if (AndroidUtilities.isTablet()) {
            gridView.setColumnWidth(AndroidUtilities.dp(60));
        } else {
            gridView.setColumnWidth(AndroidUtilities.dp(45));
        }
        gridView.setNumColumns(-1);
        views.add(gridView);

        EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(i - 1);
        gridView.setAdapter(emojiGridAdapter);
        AndroidUtilities.setListViewEdgeEffectColor(gridView, 0xfff5f6f7);
        adapters.add(emojiGridAdapter);
    }

    if (showStickers) {
        StickersQuery.checkStickers();
        stickersGridView = new GridView(context) {
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                boolean result = StickerPreviewViewer.getInstance().onInterceptTouchEvent(event,
                        stickersGridView, EmojiView.this.getMeasuredHeight());
                return super.onInterceptTouchEvent(event) || result;
            }

            @Override
            public void setVisibility(int visibility) {
                if (gifsGridView != null && gifsGridView.getVisibility() == VISIBLE) {
                    super.setVisibility(GONE);
                    return;
                }
                super.setVisibility(visibility);
            }
        };
        stickersGridView.setSelector(R.drawable.transparent);
        stickersGridView.setColumnWidth(AndroidUtilities.dp(72));
        stickersGridView.setNumColumns(-1);
        stickersGridView.setPadding(0, AndroidUtilities.dp(4), 0, 0);
        stickersGridView.setClipToPadding(false);
        views.add(stickersGridView);
        stickersGridAdapter = new StickersGridAdapter(context);
        stickersGridView.setAdapter(stickersGridAdapter);
        stickersGridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return StickerPreviewViewer.getInstance().onTouch(event, stickersGridView,
                        EmojiView.this.getMeasuredHeight(), stickersOnItemClickListener);
            }
        });
        stickersOnItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long i) {
                if (!(view instanceof StickerEmojiCell)) {
                    return;
                }
                StickerPreviewViewer.getInstance().reset();
                StickerEmojiCell cell = (StickerEmojiCell) view;
                if (cell.isDisabled()) {
                    return;
                }
                cell.disable();
                TLRPC.Document document = cell.getSticker();
                int index = newRecentStickers.indexOf(document.id);
                if (index == -1) {
                    newRecentStickers.add(0, document.id);
                    if (newRecentStickers.size() > 20) {
                        newRecentStickers.remove(newRecentStickers.size() - 1);
                    }
                } else if (index != 0) {
                    newRecentStickers.remove(index);
                    newRecentStickers.add(0, document.id);
                }

                saveRecentStickers();
                if (listener != null) {
                    listener.onStickerSelected(document);
                }
            }
        };
        stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
        AndroidUtilities.setListViewEdgeEffectColor(stickersGridView, 0xfff5f6f7);

        stickersWrap = new FrameLayout(context);
        stickersWrap.addView(stickersGridView);

        if (needGif) {
            gifsGridView = new RecyclerListView(context);
            gifsGridView.setLayoutManager(flowLayoutManager = new FlowLayoutManager() {

                private Size size = new Size();

                @Override
                protected Size getSizeForItem(int i) {
                    TLRPC.Document document = recentImages.get(i).document;
                    size.width = document.thumb != null && document.thumb.w != 0 ? document.thumb.w : 100;
                    size.height = document.thumb != null && document.thumb.h != 0 ? document.thumb.h : 100;
                    for (int b = 0; b < document.attributes.size(); b++) {
                        TLRPC.DocumentAttribute attribute = document.attributes.get(b);
                        if (attribute instanceof TLRPC.TL_documentAttributeImageSize
                                || attribute instanceof TLRPC.TL_documentAttributeVideo) {
                            size.width = attribute.w;
                            size.height = attribute.h;
                            break;
                        }
                    }
                    return size;
                }
            });
            if (Build.VERSION.SDK_INT >= 9) {
                gifsGridView.setOverScrollMode(RecyclerListView.OVER_SCROLL_NEVER);
            }
            gifsGridView.setAdapter(gifsAdapter = new GifsAdapter(context));
            gifsGridView.setOnItemClickListener(new RecyclerListView.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if (position < 0 || position >= recentImages.size() || listener == null) {
                        return;
                    }
                    TLRPC.Document document = recentImages.get(position).document;
                    listener.onStickerSelected(document);
                }
            });
            gifsGridView.setOnItemLongClickListener(new RecyclerListView.OnItemLongClickListener() {
                @Override
                public boolean onItemClick(View view, int position) {
                    if (position < 0 || position >= recentImages.size()) {
                        return false;
                    }
                    final MediaController.SearchImage searchImage = recentImages.get(position);
                    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                    builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
                    builder.setMessage(LocaleController.getString("DeleteGif", R.string.DeleteGif));
                    builder.setPositiveButton(LocaleController.getString("OK", R.string.OK).toUpperCase(),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    recentImages.remove(searchImage);
                                    TLRPC.TL_messages_saveGif req = new TLRPC.TL_messages_saveGif();
                                    req.id = new TLRPC.TL_inputDocument();
                                    req.id.id = searchImage.document.id;
                                    req.id.access_hash = searchImage.document.access_hash;
                                    req.unsave = true;
                                    ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
                                        @Override
                                        public void run(TLObject response, TLRPC.TL_error error) {

                                        }
                                    });
                                    MessagesStorage.getInstance().removeWebRecent(searchImage);
                                    if (gifsAdapter != null) {
                                        gifsAdapter.notifyDataSetChanged();
                                    }
                                    if (recentImages.isEmpty()) {
                                        updateStickerTabs();
                                    }
                                }
                            });
                    builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
                    builder.show().setCanceledOnTouchOutside(true);
                    return true;
                }
            });
            gifsGridView.setVisibility(GONE);
            stickersWrap.addView(gifsGridView);
        }

        stickersEmptyView = new TextView(context);
        stickersEmptyView.setText(LocaleController.getString("NoStickers", R.string.NoStickers));
        stickersEmptyView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        stickersEmptyView.setTextColor(0xff888888);
        stickersWrap.addView(stickersEmptyView,
                LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER));
        stickersGridView.setEmptyView(stickersEmptyView);

        scrollSlidingTabStrip = new ScrollSlidingTabStrip(context) {

            boolean startedScroll;
            float lastX;
            float lastTranslateX;
            boolean first = true;

            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                return super.onInterceptTouchEvent(ev);
            }

            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (Build.VERSION.SDK_INT >= 11) {
                    if (first) {
                        first = false;
                        lastX = ev.getX();
                    }
                    float newTranslationX = ViewProxy.getTranslationX(scrollSlidingTabStrip);
                    if (scrollSlidingTabStrip.getScrollX() == 0 && newTranslationX == 0) {
                        if (!startedScroll && lastX - ev.getX() < 0) {
                            if (pager.beginFakeDrag()) {
                                startedScroll = true;
                                lastTranslateX = ViewProxy.getTranslationX(scrollSlidingTabStrip);
                            }
                        } else if (startedScroll && lastX - ev.getX() > 0) {
                            if (pager.isFakeDragging()) {
                                pager.endFakeDrag();
                                startedScroll = false;
                            }
                        }
                    }
                    if (startedScroll) {
                        int dx = (int) (ev.getX() - lastX + newTranslationX - lastTranslateX);
                        try {
                            pager.fakeDragBy(dx);
                            lastTranslateX = newTranslationX;
                        } catch (Exception e) {
                            try {
                                pager.endFakeDrag();
                            } catch (Exception e2) {
                                //don't promt
                            }
                            startedScroll = false;
                            FileLog.e("tmessages", e);
                        }
                    }
                    lastX = ev.getX();
                    if (ev.getAction() == MotionEvent.ACTION_CANCEL
                            || ev.getAction() == MotionEvent.ACTION_UP) {
                        first = true;
                        if (startedScroll) {
                            pager.endFakeDrag();
                            startedScroll = false;
                        }
                    }
                    return startedScroll || super.onTouchEvent(ev);
                }
                return super.onTouchEvent(ev);
            }
        };
        scrollSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
        scrollSlidingTabStrip.setIndicatorColor(0xffe2e5e7);
        scrollSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
        scrollSlidingTabStrip.setVisibility(INVISIBLE);
        addView(scrollSlidingTabStrip,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.TOP));
        ViewProxy.setTranslationX(scrollSlidingTabStrip, AndroidUtilities.displaySize.x);
        updateStickerTabs();
        scrollSlidingTabStrip.setDelegate(new ScrollSlidingTabStrip.ScrollSlidingTabStripDelegate() {
            @Override
            public void onPageSelected(int page) {
                if (gifsGridView != null) {
                    if (page == gifTabBum + 1) {
                        if (gifsGridView.getVisibility() != VISIBLE) {
                            listener.onGifTab(true);
                            showGifTab();
                        }
                    } else {
                        if (gifsGridView.getVisibility() == VISIBLE) {
                            listener.onGifTab(false);
                            gifsGridView.setVisibility(GONE);
                            stickersGridView.setVisibility(VISIBLE);
                            stickersEmptyView
                                    .setVisibility(stickersGridAdapter.getCount() != 0 ? GONE : VISIBLE);
                        }
                    }
                }
                if (page == 0) {
                    pager.setCurrentItem(0);
                    return;
                } else {
                    if (page == gifTabBum + 1) {
                        return;
                    } else {
                        if (page == recentTabBum + 1) {
                            views.get(6).setSelection(0);
                            return;
                        }
                    }
                }
                int index = page - 1 - stickersTabOffset;
                if (index == stickerSets.size()) {
                    if (listener != null) {
                        listener.onStickersSettingsClick();
                    }
                    return;
                }
                if (index >= stickerSets.size()) {
                    index = stickerSets.size() - 1;
                }
                views.get(6).setSelection(stickersGridAdapter.getPositionForPack(stickerSets.get(index)));
            }
        });

        stickersGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                checkStickersScroll(firstVisibleItem);
            }
        });
    }

    setBackgroundColor(0xfff5f6f7);

    pager = new ViewPager(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pager.setAdapter(new EmojiPagesAdapter());

    pagerSlidingTabStripContainer = new LinearLayout(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pagerSlidingTabStripContainer.setOrientation(LinearLayout.HORIZONTAL);
    pagerSlidingTabStripContainer.setBackgroundColor(0xfff5f6f7);
    addView(pagerSlidingTabStripContainer, LayoutHelper.createFrame(LayoutParams.MATCH_PARENT, 48));

    PagerSlidingTabStrip pagerSlidingTabStrip = new PagerSlidingTabStrip(context);
    pagerSlidingTabStrip.setViewPager(pager);
    pagerSlidingTabStrip.setShouldExpand(true);
    pagerSlidingTabStrip.setIndicatorHeight(AndroidUtilities.dp(2));
    pagerSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
    pagerSlidingTabStrip.setIndicatorColor(0xff2b96e2);
    pagerSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
    pagerSlidingTabStripContainer.addView(pagerSlidingTabStrip, LayoutHelper.createLinear(0, 48, 1.0f));
    pagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            EmojiView.this.onPageScrolled(position, getMeasuredWidth(), positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    FrameLayout frameLayout = new FrameLayout(context);
    pagerSlidingTabStripContainer.addView(frameLayout, LayoutHelper.createLinear(52, 48));

    backspaceButton = new ImageView(context) {
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                backspacePressed = true;
                backspaceOnce = false;
                postBackspaceRunnable(350);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL
                    || event.getAction() == MotionEvent.ACTION_UP) {
                backspacePressed = false;
                if (!backspaceOnce) {
                    if (listener != null && listener.onBackspace()) {
                        backspaceButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                    }
                }
            }
            super.onTouchEvent(event);
            return true;
        }
    };
    backspaceButton.setImageResource(R.drawable.ic_smiles_backspace);
    backspaceButton.setBackgroundResource(R.drawable.ic_emoji_backspace);
    backspaceButton.setScaleType(ImageView.ScaleType.CENTER);
    frameLayout.addView(backspaceButton, LayoutHelper.createFrame(52, 48));

    View view = new View(context);
    view.setBackgroundColor(0xffe2e5e7);
    frameLayout.addView(view, LayoutHelper.createFrame(52, 1, Gravity.LEFT | Gravity.BOTTOM));

    recentsWrap = new FrameLayout(context);
    recentsWrap.addView(views.get(0));

    TextView textView = new TextView(context);
    textView.setText(LocaleController.getString("NoRecent", R.string.NoRecent));
    textView.setTextSize(18);
    textView.setTextColor(0xff888888);
    textView.setGravity(Gravity.CENTER);
    recentsWrap.addView(textView);
    views.get(0).setEmptyView(textView);

    addView(pager, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT,
            Gravity.LEFT | Gravity.TOP, 0, 48, 0, 0));

    emojiSize = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 40 : 32);
    pickerView = new EmojiColorPickerView(context);
    pickerViewPopup = new EmojiPopupWindow(pickerView,
            popupWidth = AndroidUtilities.dp((AndroidUtilities.isTablet() ? 40 : 32) * 6 + 10 + 4 * 5),
            popupHeight = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 64 : 56));
    pickerViewPopup.setOutsideTouchable(true);
    pickerViewPopup.setClippingEnabled(true);
    pickerViewPopup.setInputMethodMode(EmojiPopupWindow.INPUT_METHOD_NOT_NEEDED);
    pickerViewPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
    pickerViewPopup.getContentView().setFocusableInTouchMode(true);
    pickerViewPopup.getContentView().setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
                    && event.getAction() == KeyEvent.ACTION_UP && pickerViewPopup != null
                    && pickerViewPopup.isShowing()) {
                pickerViewPopup.dismiss();
                return true;
            }
            return false;
        }
    });

    loadRecents();
}

From source file:org.cafemember.ui.Components.EmojiView.java

public EmojiView(boolean needStickers, boolean needGif, final Context context) {
    super(context);

    showStickers = needStickers;// w w w . j  a  v  a  2  s  . c o m
    showGifs = needGif;

    for (int i = 0; i < EmojiData.dataColored.length + 1; i++) {
        GridView gridView = new GridView(context);
        if (AndroidUtilities.isTablet()) {
            gridView.setColumnWidth(AndroidUtilities.dp(60));
        } else {
            gridView.setColumnWidth(AndroidUtilities.dp(45));
        }
        gridView.setNumColumns(-1);
        views.add(gridView);

        EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(i - 1);
        gridView.setAdapter(emojiGridAdapter);
        AndroidUtilities.setListViewEdgeEffectColor(gridView, 0xfff5f6f7);
        adapters.add(emojiGridAdapter);
    }

    if (showStickers) {
        StickersQuery.checkStickers();
        stickersGridView = new GridView(context) {
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                boolean result = StickerPreviewViewer.getInstance().onInterceptTouchEvent(event,
                        stickersGridView, EmojiView.this.getMeasuredHeight());
                return super.onInterceptTouchEvent(event) || result;
            }

            @Override
            public void setVisibility(int visibility) {
                if (gifsGridView != null && gifsGridView.getVisibility() == VISIBLE) {
                    super.setVisibility(GONE);
                    return;
                }
                super.setVisibility(visibility);
            }
        };
        stickersGridView.setSelector(R.drawable.transparent);
        stickersGridView.setColumnWidth(AndroidUtilities.dp(72));
        stickersGridView.setNumColumns(-1);
        stickersGridView.setPadding(0, AndroidUtilities.dp(4), 0, 0);
        stickersGridView.setClipToPadding(false);
        views.add(stickersGridView);
        stickersGridAdapter = new StickersGridAdapter(context);
        stickersGridView.setAdapter(stickersGridAdapter);
        stickersGridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return StickerPreviewViewer.getInstance().onTouch(event, stickersGridView,
                        EmojiView.this.getMeasuredHeight(), stickersOnItemClickListener);
            }
        });
        stickersOnItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long i) {
                if (!(view instanceof StickerEmojiCell)) {
                    return;
                }
                StickerPreviewViewer.getInstance().reset();
                StickerEmojiCell cell = (StickerEmojiCell) view;
                if (cell.isDisabled()) {
                    return;
                }
                cell.disable();
                TLRPC.Document document = cell.getSticker();
                int index = newRecentStickers.indexOf(document.id);
                if (index == -1) {
                    newRecentStickers.add(0, document.id);
                    if (newRecentStickers.size() > 20) {
                        newRecentStickers.remove(newRecentStickers.size() - 1);
                    }
                } else if (index != 0) {
                    newRecentStickers.remove(index);
                    newRecentStickers.add(0, document.id);
                }

                saveRecentStickers();
                if (listener != null) {
                    listener.onStickerSelected(document);
                }
            }
        };
        stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
        AndroidUtilities.setListViewEdgeEffectColor(stickersGridView, 0xfff5f6f7);

        stickersWrap = new FrameLayout(context);
        stickersWrap.addView(stickersGridView);

        if (needGif) {
            gifsGridView = new RecyclerListView(context);
            gifsGridView.setLayoutManager(flowLayoutManager = new FlowLayoutManager() {

                private Size size = new Size();

                @Override
                protected Size getSizeForItem(int i) {
                    TLRPC.Document document = recentImages.get(i).document;
                    size.width = document.thumb != null && document.thumb.w != 0 ? document.thumb.w : 100;
                    size.height = document.thumb != null && document.thumb.h != 0 ? document.thumb.h : 100;
                    for (int b = 0; b < document.attributes.size(); b++) {
                        TLRPC.DocumentAttribute attribute = document.attributes.get(b);
                        if (attribute instanceof TLRPC.TL_documentAttributeImageSize
                                || attribute instanceof TLRPC.TL_documentAttributeVideo) {
                            size.width = attribute.w;
                            size.height = attribute.h;
                            break;
                        }
                    }
                    return size;
                }
            });
            if (Build.VERSION.SDK_INT >= 9) {
                gifsGridView.setOverScrollMode(RecyclerListView.OVER_SCROLL_NEVER);
            }
            gifsGridView.setAdapter(gifsAdapter = new GifsAdapter(context));
            gifsGridView.setOnItemClickListener(new RecyclerListView.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if (position < 0 || position >= recentImages.size() || listener == null) {
                        return;
                    }
                    TLRPC.Document document = recentImages.get(position).document;
                    listener.onGifSelected(document);
                }
            });
            gifsGridView.setOnItemLongClickListener(new RecyclerListView.OnItemLongClickListener() {
                @Override
                public boolean onItemClick(View view, int position) {
                    if (position < 0 || position >= recentImages.size()) {
                        return false;
                    }
                    final MediaController.SearchImage searchImage = recentImages.get(position);
                    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                    builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
                    builder.setMessage(LocaleController.getString("DeleteGif", R.string.DeleteGif));
                    builder.setPositiveButton(LocaleController.getString("OK", R.string.OK).toUpperCase(),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    recentImages.remove(searchImage);
                                    TLRPC.TL_messages_saveGif req = new TLRPC.TL_messages_saveGif();
                                    req.id = new TLRPC.TL_inputDocument();
                                    req.id.id = searchImage.document.id;
                                    req.id.access_hash = searchImage.document.access_hash;
                                    req.unsave = true;
                                    ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
                                        @Override
                                        public void run(TLObject response, TLRPC.TL_error error) {

                                        }
                                    });
                                    MessagesStorage.getInstance().removeWebRecent(searchImage);
                                    if (gifsAdapter != null) {
                                        gifsAdapter.notifyDataSetChanged();
                                    }
                                    if (recentImages.isEmpty()) {
                                        updateStickerTabs();
                                    }
                                }
                            });
                    builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
                    builder.show().setCanceledOnTouchOutside(true);
                    return true;
                }
            });
            gifsGridView.setVisibility(GONE);
            stickersWrap.addView(gifsGridView);
        }

        stickersEmptyView = new TextView(context);
        stickersEmptyView.setText(LocaleController.getString("NoStickers", R.string.NoStickers));
        stickersEmptyView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        stickersEmptyView.setTextColor(0xff888888);
        stickersWrap.addView(stickersEmptyView,
                LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER));
        stickersGridView.setEmptyView(stickersEmptyView);

        scrollSlidingTabStrip = new ScrollSlidingTabStrip(context) {

            boolean startedScroll;
            float lastX;
            float lastTranslateX;
            boolean first = true;

            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                return super.onInterceptTouchEvent(ev);
            }

            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (Build.VERSION.SDK_INT >= 11) {
                    if (first) {
                        first = false;
                        lastX = ev.getX();
                    }
                    float newTranslationX = ViewProxy.getTranslationX(scrollSlidingTabStrip);
                    if (scrollSlidingTabStrip.getScrollX() == 0 && newTranslationX == 0) {
                        if (!startedScroll && lastX - ev.getX() < 0) {
                            if (pager.beginFakeDrag()) {
                                startedScroll = true;
                                lastTranslateX = ViewProxy.getTranslationX(scrollSlidingTabStrip);
                            }
                        } else if (startedScroll && lastX - ev.getX() > 0) {
                            if (pager.isFakeDragging()) {
                                pager.endFakeDrag();
                                startedScroll = false;
                            }
                        }
                    }
                    if (startedScroll) {
                        int dx = (int) (ev.getX() - lastX + newTranslationX - lastTranslateX);
                        try {
                            pager.fakeDragBy(dx);
                            lastTranslateX = newTranslationX;
                        } catch (Exception e) {
                            try {
                                pager.endFakeDrag();
                            } catch (Exception e2) {
                                //don't promt
                            }
                            startedScroll = false;
                            FileLog.e("tmessages", e);
                        }
                    }
                    lastX = ev.getX();
                    if (ev.getAction() == MotionEvent.ACTION_CANCEL
                            || ev.getAction() == MotionEvent.ACTION_UP) {
                        first = true;
                        if (startedScroll) {
                            pager.endFakeDrag();
                            startedScroll = false;
                        }
                    }
                    return startedScroll || super.onTouchEvent(ev);
                }
                return super.onTouchEvent(ev);
            }
        };
        scrollSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
        scrollSlidingTabStrip.setIndicatorColor(0xffe2e5e7);
        scrollSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
        scrollSlidingTabStrip.setVisibility(INVISIBLE);
        addView(scrollSlidingTabStrip,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.TOP));
        ViewProxy.setTranslationX(scrollSlidingTabStrip, AndroidUtilities.displaySize.x);
        updateStickerTabs();
        scrollSlidingTabStrip.setDelegate(new ScrollSlidingTabStrip.ScrollSlidingTabStripDelegate() {
            @Override
            public void onPageSelected(int page) {
                if (gifsGridView != null) {
                    if (page == gifTabBum + 1) {
                        if (gifsGridView.getVisibility() != VISIBLE) {
                            listener.onGifTab(true);
                            showGifTab();
                        }
                    } else {
                        if (gifsGridView.getVisibility() == VISIBLE) {
                            listener.onGifTab(false);
                            gifsGridView.setVisibility(GONE);
                            stickersGridView.setVisibility(VISIBLE);
                            stickersEmptyView
                                    .setVisibility(stickersGridAdapter.getCount() != 0 ? GONE : VISIBLE);
                        }
                    }
                }
                if (page == 0) {
                    pager.setCurrentItem(0);
                    return;
                } else {
                    if (page == gifTabBum + 1) {
                        return;
                    } else {
                        if (page == recentTabBum + 1) {
                            views.get(6).setSelection(0);
                            return;
                        }
                    }
                }
                int index = page - 1 - stickersTabOffset;
                if (index == stickerSets.size()) {
                    if (listener != null) {
                        listener.onStickersSettingsClick();
                    }
                    return;
                }
                if (index >= stickerSets.size()) {
                    index = stickerSets.size() - 1;
                }
                views.get(6).setSelection(stickersGridAdapter.getPositionForPack(stickerSets.get(index)));
            }
        });

        stickersGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                checkStickersScroll(firstVisibleItem);
            }
        });
    }

    setBackgroundColor(0xfff5f6f7);

    pager = new ViewPager(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pager.setAdapter(new EmojiPagesAdapter());

    pagerSlidingTabStripContainer = new LinearLayout(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pagerSlidingTabStripContainer.setOrientation(LinearLayout.HORIZONTAL);
    pagerSlidingTabStripContainer.setBackgroundColor(0xfff5f6f7);
    addView(pagerSlidingTabStripContainer, LayoutHelper.createFrame(LayoutParams.MATCH_PARENT, 48));

    PagerSlidingTabStrip pagerSlidingTabStrip = new PagerSlidingTabStrip(context);
    pagerSlidingTabStrip.setViewPager(pager);
    pagerSlidingTabStrip.setShouldExpand(true);
    pagerSlidingTabStrip.setIndicatorHeight(AndroidUtilities.dp(2));
    pagerSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
    pagerSlidingTabStrip.setIndicatorColor(0xff2b96e2);
    pagerSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
    pagerSlidingTabStripContainer.addView(pagerSlidingTabStrip, LayoutHelper.createLinear(0, 48, 1.0f));
    pagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            EmojiView.this.onPageScrolled(position, getMeasuredWidth(), positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    FrameLayout frameLayout = new FrameLayout(context);
    pagerSlidingTabStripContainer.addView(frameLayout, LayoutHelper.createLinear(52, 48));

    backspaceButton = new ImageView(context) {
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                backspacePressed = true;
                backspaceOnce = false;
                postBackspaceRunnable(350);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL
                    || event.getAction() == MotionEvent.ACTION_UP) {
                backspacePressed = false;
                if (!backspaceOnce) {
                    if (listener != null && listener.onBackspace()) {
                        backspaceButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                    }
                }
            }
            super.onTouchEvent(event);
            return true;
        }
    };
    backspaceButton.setImageResource(R.drawable.ic_smiles_backspace);
    backspaceButton.setBackgroundResource(R.drawable.ic_emoji_backspace);
    backspaceButton.setScaleType(ImageView.ScaleType.CENTER);
    frameLayout.addView(backspaceButton, LayoutHelper.createFrame(52, 48));

    View view = new View(context);
    view.setBackgroundColor(0xffe2e5e7);
    frameLayout.addView(view, LayoutHelper.createFrame(52, 1, Gravity.LEFT | Gravity.BOTTOM));

    recentsWrap = new FrameLayout(context);
    recentsWrap.addView(views.get(0));

    TextView textView = new TextView(context);
    textView.setText(LocaleController.getString("NoRecent", R.string.NoRecent));
    textView.setTextSize(18);
    textView.setTextColor(0xff888888);
    textView.setGravity(Gravity.CENTER);
    recentsWrap.addView(textView);
    views.get(0).setEmptyView(textView);

    addView(pager, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT,
            Gravity.LEFT | Gravity.TOP, 0, 48, 0, 0));

    emojiSize = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 40 : 32);
    pickerView = new EmojiColorPickerView(context);
    pickerViewPopup = new EmojiPopupWindow(pickerView,
            popupWidth = AndroidUtilities.dp((AndroidUtilities.isTablet() ? 40 : 32) * 6 + 10 + 4 * 5),
            popupHeight = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 64 : 56));
    pickerViewPopup.setOutsideTouchable(true);
    pickerViewPopup.setClippingEnabled(true);
    pickerViewPopup.setInputMethodMode(EmojiPopupWindow.INPUT_METHOD_NOT_NEEDED);
    pickerViewPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
    pickerViewPopup.getContentView().setFocusableInTouchMode(true);
    pickerViewPopup.getContentView().setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
                    && event.getAction() == KeyEvent.ACTION_UP && pickerViewPopup != null
                    && pickerViewPopup.isShowing()) {
                pickerViewPopup.dismiss();
                return true;
            }
            return false;
        }
    });

    loadRecents();
}

From source file:ir.irani.telecam.ui.Components.EmojiView.java

public EmojiView(boolean needStickers, boolean needGif, final Context context) {
    super(context);

    showStickers = needStickers;//ww  w .  j  av a  2s . c  o  m
    showGifs = needGif;

    for (int i = 0; i < EmojiData.dataColored.length + 1; i++) {
        GridView gridView = new GridView(context);
        if (AndroidUtilities.isTablet()) {
            gridView.setColumnWidth(AndroidUtilities.dp(60));
        } else {
            gridView.setColumnWidth(AndroidUtilities.dp(45));
        }
        gridView.setNumColumns(-1);
        views.add(gridView);

        EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(i - 1);
        gridView.setAdapter(emojiGridAdapter);
        AndroidUtilities.setListViewEdgeEffectColor(gridView, 0xfff5f6f7);
        adapters.add(emojiGridAdapter);
    }

    if (showStickers) {
        StickersQuery.checkStickers();
        stickersGridView = new GridView(context) {
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                boolean result = StickerPreviewViewer.getInstance().onInterceptTouchEvent(event,
                        stickersGridView, EmojiView.this.getMeasuredHeight());
                return super.onInterceptTouchEvent(event) || result;
            }

            @Override
            public void setVisibility(int visibility) {
                if (gifsGridView != null && gifsGridView.getVisibility() == VISIBLE) {
                    super.setVisibility(GONE);
                    return;
                }
                super.setVisibility(visibility);
            }
        };
        stickersGridView.setSelector(R.drawable.transparent);
        stickersGridView.setColumnWidth(AndroidUtilities.dp(72));
        stickersGridView.setNumColumns(-1);
        stickersGridView.setPadding(0, AndroidUtilities.dp(4), 0, 0);
        stickersGridView.setClipToPadding(false);
        views.add(stickersGridView);
        stickersGridAdapter = new StickersGridAdapter(context);
        stickersGridView.setAdapter(stickersGridAdapter);
        stickersGridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return StickerPreviewViewer.getInstance().onTouch(event, stickersGridView,
                        EmojiView.this.getMeasuredHeight(), stickersOnItemClickListener);
            }
        });
        stickersOnItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long i) {
                if (!(view instanceof StickerEmojiCell)) {
                    return;
                }
                StickerPreviewViewer.getInstance().reset();
                StickerEmojiCell cell = (StickerEmojiCell) view;
                if (cell.isDisabled()) {
                    return;
                }
                cell.disable();
                TLRPC.Document document = cell.getSticker();
                addRecentSticker(document);
                if (listener != null) {
                    listener.onStickerSelected(document);
                }
            }
        };
        stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
        AndroidUtilities.setListViewEdgeEffectColor(stickersGridView, 0xfff5f6f7);

        stickersWrap = new FrameLayout(context);
        stickersWrap.addView(stickersGridView);

        if (needGif) {
            gifsGridView = new RecyclerListView(context);
            gifsGridView.setTag(11);
            gifsGridView.setLayoutManager(flowLayoutManager = new ExtendedGridLayoutManager(context, 100) {

                private Size size = new Size();

                @Override
                protected Size getSizeForItem(int i) {
                    TLRPC.Document document = recentImages.get(i).document;
                    size.width = document.thumb != null && document.thumb.w != 0 ? document.thumb.w : 100;
                    size.height = document.thumb != null && document.thumb.h != 0 ? document.thumb.h : 100;
                    for (int b = 0; b < document.attributes.size(); b++) {
                        TLRPC.DocumentAttribute attribute = document.attributes.get(b);
                        if (attribute instanceof TLRPC.TL_documentAttributeImageSize
                                || attribute instanceof TLRPC.TL_documentAttributeVideo) {
                            size.width = attribute.w;
                            size.height = attribute.h;
                            break;
                        }
                    }
                    return size;
                }
            });
            flowLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return flowLayoutManager.getSpanSizeForItem(position);
                }
            });
            gifsGridView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(android.graphics.Rect outRect, View view, RecyclerView parent,
                        RecyclerView.State state) {
                    outRect.left = 0;
                    outRect.top = 0;
                    outRect.bottom = 0;
                    int position = parent.getChildAdapterPosition(view);
                    if (!flowLayoutManager.isFirstRow(position)) {
                        outRect.top = AndroidUtilities.dp(2);
                    }
                    outRect.right = flowLayoutManager.isLastInRow(position) ? 0 : AndroidUtilities.dp(2);
                }
            });
            gifsGridView.setOverScrollMode(RecyclerListView.OVER_SCROLL_NEVER);
            gifsGridView.setAdapter(gifsAdapter = new GifsAdapter(context));
            gifsGridView.setOnItemClickListener(new RecyclerListView.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if (position < 0 || position >= recentImages.size() || listener == null) {
                        return;
                    }
                    TLRPC.Document document = recentImages.get(position).document;
                    listener.onGifSelected(document);
                }
            });
            gifsGridView.setOnItemLongClickListener(new RecyclerListView.OnItemLongClickListener() {
                @Override
                public boolean onItemClick(View view, int position) {
                    if (position < 0 || position >= recentImages.size()) {
                        return false;
                    }
                    final MediaController.SearchImage searchImage = recentImages.get(position);
                    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                    builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
                    builder.setMessage(LocaleController.getString("DeleteGif", R.string.DeleteGif));
                    builder.setPositiveButton(LocaleController.getString("OK", R.string.OK).toUpperCase(),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    recentImages.remove(searchImage);
                                    TLRPC.TL_messages_saveGif req = new TLRPC.TL_messages_saveGif();
                                    req.id = new TLRPC.TL_inputDocument();
                                    req.id.id = searchImage.document.id;
                                    req.id.access_hash = searchImage.document.access_hash;
                                    req.unsave = true;
                                    ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
                                        @Override
                                        public void run(TLObject response, TLRPC.TL_error error) {

                                        }
                                    });
                                    MessagesStorage.getInstance().removeWebRecent(searchImage);
                                    if (gifsAdapter != null) {
                                        gifsAdapter.notifyDataSetChanged();
                                    }
                                    if (recentImages.isEmpty()) {
                                        updateStickerTabs();
                                    }
                                }
                            });
                    builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
                    builder.show().setCanceledOnTouchOutside(true);
                    return true;
                }
            });
            gifsGridView.setVisibility(GONE);
            stickersWrap.addView(gifsGridView);
        }

        stickersEmptyView = new TextView(context);
        stickersEmptyView.setText(LocaleController.getString("NoStickers", R.string.NoStickers));
        stickersEmptyView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        stickersEmptyView.setTextColor(0xff888888);
        stickersWrap.addView(stickersEmptyView,
                LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER));
        stickersGridView.setEmptyView(stickersEmptyView);

        scrollSlidingTabStrip = new ScrollSlidingTabStrip(context) {

            boolean startedScroll;
            float lastX;
            float lastTranslateX;
            boolean first = true;

            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                return super.onInterceptTouchEvent(ev);
            }

            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (first) {
                    first = false;
                    lastX = ev.getX();
                }
                float newTranslationX = scrollSlidingTabStrip.getTranslationX();
                if (scrollSlidingTabStrip.getScrollX() == 0 && newTranslationX == 0) {
                    if (!startedScroll && lastX - ev.getX() < 0) {
                        if (pager.beginFakeDrag()) {
                            startedScroll = true;
                            lastTranslateX = scrollSlidingTabStrip.getTranslationX();
                        }
                    } else if (startedScroll && lastX - ev.getX() > 0) {
                        if (pager.isFakeDragging()) {
                            pager.endFakeDrag();
                            startedScroll = false;
                        }
                    }
                }
                if (startedScroll) {
                    int dx = (int) (ev.getX() - lastX + newTranslationX - lastTranslateX);
                    try {
                        pager.fakeDragBy(dx);
                        lastTranslateX = newTranslationX;
                    } catch (Exception e) {
                        try {
                            pager.endFakeDrag();
                        } catch (Exception e2) {
                            //don't promt
                        }
                        startedScroll = false;
                        FileLog.e("tmessages", e);
                    }
                }
                lastX = ev.getX();
                if (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP) {
                    first = true;
                    if (startedScroll) {
                        pager.endFakeDrag();
                        startedScroll = false;
                    }
                }
                return startedScroll || super.onTouchEvent(ev);
            }
        };
        scrollSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
        scrollSlidingTabStrip.setIndicatorColor(0xffe2e5e7);
        scrollSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
        scrollSlidingTabStrip.setVisibility(INVISIBLE);
        addView(scrollSlidingTabStrip,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.TOP));
        scrollSlidingTabStrip.setTranslationX(AndroidUtilities.displaySize.x);
        updateStickerTabs();
        scrollSlidingTabStrip.setDelegate(new ScrollSlidingTabStrip.ScrollSlidingTabStripDelegate() {
            @Override
            public void onPageSelected(int page) {
                if (gifsGridView != null) {
                    if (page == gifTabBum + 1) {
                        if (gifsGridView.getVisibility() != VISIBLE) {
                            listener.onGifTab(true);
                            showGifTab();
                        }
                    } else {
                        if (gifsGridView.getVisibility() == VISIBLE) {
                            listener.onGifTab(false);
                            gifsGridView.setVisibility(GONE);
                            stickersGridView.setVisibility(VISIBLE);
                            stickersEmptyView
                                    .setVisibility(stickersGridAdapter.getCount() != 0 ? GONE : VISIBLE);
                        }
                    }
                }
                if (page == 0) {
                    pager.setCurrentItem(0);
                    return;
                } else {
                    if (page == gifTabBum + 1) {
                        return;
                    } else {
                        if (page == recentTabBum + 1) {
                            views.get(6).setSelection(0);
                            return;
                        }
                    }
                }
                int index = page - 1 - stickersTabOffset;
                if (index == stickerSets.size()) {
                    if (listener != null) {
                        listener.onStickersSettingsClick();
                    }
                    return;
                }
                if (index >= stickerSets.size()) {
                    index = stickerSets.size() - 1;
                }
                views.get(6).setSelection(stickersGridAdapter.getPositionForPack(stickerSets.get(index)));
            }
        });

        stickersGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                checkStickersScroll(firstVisibleItem);
            }
        });
    }

    setBackgroundColor(0xfff5f6f7);

    pager = new ViewPager(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pager.setAdapter(new EmojiPagesAdapter());

    pagerSlidingTabStripContainer = new LinearLayout(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pagerSlidingTabStripContainer.setOrientation(LinearLayout.HORIZONTAL);
    pagerSlidingTabStripContainer.setBackgroundColor(0xfff5f6f7);
    addView(pagerSlidingTabStripContainer, LayoutHelper.createFrame(LayoutParams.MATCH_PARENT, 48));

    PagerSlidingTabStrip pagerSlidingTabStrip = new PagerSlidingTabStrip(context);
    pagerSlidingTabStrip.setViewPager(pager);
    pagerSlidingTabStrip.setShouldExpand(true);
    pagerSlidingTabStrip.setIndicatorHeight(AndroidUtilities.dp(2));
    pagerSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
    pagerSlidingTabStrip.setIndicatorColor(0xff2b96e2);
    pagerSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
    pagerSlidingTabStripContainer.addView(pagerSlidingTabStrip, LayoutHelper.createLinear(0, 48, 1.0f));
    pagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            EmojiView.this.onPageScrolled(position, getMeasuredWidth(), positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    FrameLayout frameLayout = new FrameLayout(context);
    pagerSlidingTabStripContainer.addView(frameLayout, LayoutHelper.createLinear(52, 48));

    backspaceButton = new ImageView(context) {
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                backspacePressed = true;
                backspaceOnce = false;
                postBackspaceRunnable(350);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL
                    || event.getAction() == MotionEvent.ACTION_UP) {
                backspacePressed = false;
                if (!backspaceOnce) {
                    if (listener != null && listener.onBackspace()) {
                        backspaceButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                    }
                }
            }
            super.onTouchEvent(event);
            return true;
        }
    };
    backspaceButton.setImageResource(R.drawable.ic_smiles_backspace);
    backspaceButton.setBackgroundResource(R.drawable.ic_emoji_backspace);
    backspaceButton.setScaleType(ImageView.ScaleType.CENTER);
    frameLayout.addView(backspaceButton, LayoutHelper.createFrame(52, 48));

    View view = new View(context);
    view.setBackgroundColor(0xffe2e5e7);
    frameLayout.addView(view, LayoutHelper.createFrame(52, 1, Gravity.LEFT | Gravity.BOTTOM));

    recentsWrap = new FrameLayout(context);
    recentsWrap.addView(views.get(0));

    TextView textView = new TextView(context);
    textView.setText(LocaleController.getString("NoRecent", R.string.NoRecent));
    textView.setTextSize(18);
    textView.setTextColor(0xff888888);
    textView.setGravity(Gravity.CENTER);
    recentsWrap.addView(textView);
    views.get(0).setEmptyView(textView);

    addView(pager, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT,
            Gravity.LEFT | Gravity.TOP, 0, 48, 0, 0));

    emojiSize = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 40 : 32);
    pickerView = new EmojiColorPickerView(context);
    pickerViewPopup = new EmojiPopupWindow(pickerView,
            popupWidth = AndroidUtilities.dp((AndroidUtilities.isTablet() ? 40 : 32) * 6 + 10 + 4 * 5),
            popupHeight = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 64 : 56));
    pickerViewPopup.setOutsideTouchable(true);
    pickerViewPopup.setClippingEnabled(true);
    pickerViewPopup.setInputMethodMode(EmojiPopupWindow.INPUT_METHOD_NOT_NEEDED);
    pickerViewPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
    pickerViewPopup.getContentView().setFocusableInTouchMode(true);
    pickerViewPopup.getContentView().setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
                    && event.getAction() == KeyEvent.ACTION_UP && pickerViewPopup != null
                    && pickerViewPopup.isShowing()) {
                pickerViewPopup.dismiss();
                return true;
            }
            return false;
        }
    });

    loadRecents();
}

From source file:ir.besteveryeverapp.ui.Components.EmojiView.java

public EmojiView(boolean needStickers, boolean needGif, final Context context) {
    super(context);

    showStickers = needStickers;//  w  w w . j  a v a2s. com
    showGifs = needGif;

    for (int i = 0; i < EmojiData.dataColored.length + 1; i++) {
        GridView gridView = new GridView(context);
        if (AndroidUtilities.isTablet()) {
            gridView.setColumnWidth(AndroidUtilities.dp(60));
        } else {
            gridView.setColumnWidth(AndroidUtilities.dp(45));
        }
        gridView.setNumColumns(-1);
        views.add(gridView);

        EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(i - 1);
        gridView.setAdapter(emojiGridAdapter);
        AndroidUtilities.setListViewEdgeEffectColor(gridView, 0xfff5f6f7);
        adapters.add(emojiGridAdapter);
    }

    if (showStickers) {
        StickersQuery.checkStickers();
        stickersGridView = new GridView(context) {
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                boolean result = StickerPreviewViewer.getInstance().onInterceptTouchEvent(event,
                        stickersGridView, EmojiView.this.getMeasuredHeight());
                return super.onInterceptTouchEvent(event) || result;
            }

            @Override
            public void setVisibility(int visibility) {
                if (gifsGridView != null && gifsGridView.getVisibility() == VISIBLE) {
                    super.setVisibility(GONE);
                    return;
                }
                super.setVisibility(visibility);
            }
        };
        stickersGridView.setSelector(R.drawable.transparent);
        stickersGridView.setColumnWidth(AndroidUtilities.dp(72));
        stickersGridView.setNumColumns(-1);
        stickersGridView.setPadding(0, AndroidUtilities.dp(4), 0, 0);
        stickersGridView.setClipToPadding(false);
        views.add(stickersGridView);
        stickersGridAdapter = new StickersGridAdapter(context);
        stickersGridView.setAdapter(stickersGridAdapter);
        stickersGridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return StickerPreviewViewer.getInstance().onTouch(event, stickersGridView,
                        EmojiView.this.getMeasuredHeight(), stickersOnItemClickListener);
            }
        });
        stickersOnItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long i) {
                if (!(view instanceof StickerEmojiCell)) {
                    return;
                }
                StickerPreviewViewer.getInstance().reset();
                StickerEmojiCell cell = (StickerEmojiCell) view;
                if (cell.isDisabled()) {
                    return;
                }
                cell.disable();
                TLRPC.Document document = cell.getSticker();
                addRecentSticker(document);
                if (listener != null) {
                    listener.onStickerSelected(document);
                }
            }
        };
        stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
        AndroidUtilities.setListViewEdgeEffectColor(stickersGridView, 0xfff5f6f7);

        stickersWrap = new FrameLayout(context);
        stickersWrap.addView(stickersGridView);

        if (needGif) {
            gifsGridView = new RecyclerListView(context);
            gifsGridView.setTag(11);
            gifsGridView.setLayoutManager(flowLayoutManager = new ExtendedGridLayoutManager(context, 100) {

                private Size size = new Size();

                @Override
                protected Size getSizeForItem(int i) {
                    TLRPC.Document document = recentImages.get(i).document;
                    size.width = document.thumb != null && document.thumb.w != 0 ? document.thumb.w : 100;
                    size.height = document.thumb != null && document.thumb.h != 0 ? document.thumb.h : 100;
                    for (int b = 0; b < document.attributes.size(); b++) {
                        TLRPC.DocumentAttribute attribute = document.attributes.get(b);
                        if (attribute instanceof TLRPC.TL_documentAttributeImageSize
                                || attribute instanceof TLRPC.TL_documentAttributeVideo) {
                            size.width = attribute.w;
                            size.height = attribute.h;
                            break;
                        }
                    }
                    return size;
                }
            });
            flowLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return flowLayoutManager.getSpanSizeForItem(position);
                }
            });
            gifsGridView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(android.graphics.Rect outRect, View view, RecyclerView parent,
                        RecyclerView.State state) {
                    outRect.left = 0;
                    outRect.top = 0;
                    outRect.bottom = 0;
                    int position = parent.getChildAdapterPosition(view);
                    if (!flowLayoutManager.isFirstRow(position)) {
                        outRect.top = AndroidUtilities.dp(2);
                    }
                    outRect.right = flowLayoutManager.isLastInRow(position) ? 0 : AndroidUtilities.dp(2);
                }
            });
            gifsGridView.setOverScrollMode(RecyclerListView.OVER_SCROLL_NEVER);
            gifsGridView.setAdapter(gifsAdapter = new GifsAdapter(context));
            gifsGridView.setOnItemClickListener(new RecyclerListView.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if (position < 0 || position >= recentImages.size() || listener == null) {
                        return;
                    }
                    TLRPC.Document document = recentImages.get(position).document;
                    listener.onGifSelected(document);
                }
            });
            gifsGridView.setOnItemLongClickListener(new RecyclerListView.OnItemLongClickListener() {
                @Override
                public boolean onItemClick(View view, int position) {
                    if (position < 0 || position >= recentImages.size()) {
                        return false;
                    }
                    final MediaController.SearchImage searchImage = recentImages.get(position);
                    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                    builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
                    builder.setMessage(LocaleController.getString("DeleteGif", R.string.DeleteGif));
                    builder.setPositiveButton(LocaleController.getString("OK", R.string.OK).toUpperCase(),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    recentImages.remove(searchImage);
                                    TLRPC.TL_messages_saveGif req = new TLRPC.TL_messages_saveGif();
                                    req.id = new TLRPC.TL_inputDocument();
                                    req.id.id = searchImage.document.id;
                                    req.id.access_hash = searchImage.document.access_hash;
                                    req.unsave = true;
                                    ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
                                        @Override
                                        public void run(TLObject response, TLRPC.TL_error error) {

                                        }
                                    });
                                    MessagesStorage.getInstance().removeWebRecent(searchImage);
                                    if (gifsAdapter != null) {
                                        gifsAdapter.notifyDataSetChanged();
                                    }
                                    if (recentImages.isEmpty()) {
                                        updateStickerTabs();
                                    }
                                }
                            });
                    builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
                    builder.show().setCanceledOnTouchOutside(true);
                    return true;
                }
            });
            gifsGridView.setVisibility(GONE);
            stickersWrap.addView(gifsGridView);
        }

        stickersEmptyView = new TextView(context);
        stickersEmptyView.setTypeface(FontManager.instance().getTypeface());
        stickersEmptyView.setText(LocaleController.getString("NoStickers", R.string.NoStickers));
        stickersEmptyView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        stickersEmptyView.setTextColor(0xff888888);
        stickersWrap.addView(stickersEmptyView,
                LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER));
        stickersGridView.setEmptyView(stickersEmptyView);

        scrollSlidingTabStrip = new ScrollSlidingTabStrip(context) {

            boolean startedScroll;
            float lastX;
            float lastTranslateX;
            boolean first = true;

            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                return super.onInterceptTouchEvent(ev);
            }

            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (first) {
                    first = false;
                    lastX = ev.getX();
                }
                float newTranslationX = scrollSlidingTabStrip.getTranslationX();
                if (scrollSlidingTabStrip.getScrollX() == 0 && newTranslationX == 0) {
                    if (!startedScroll && lastX - ev.getX() < 0) {
                        if (pager.beginFakeDrag()) {
                            startedScroll = true;
                            lastTranslateX = scrollSlidingTabStrip.getTranslationX();
                        }
                    } else if (startedScroll && lastX - ev.getX() > 0) {
                        if (pager.isFakeDragging()) {
                            pager.endFakeDrag();
                            startedScroll = false;
                        }
                    }
                }
                if (startedScroll) {
                    int dx = (int) (ev.getX() - lastX + newTranslationX - lastTranslateX);
                    try {
                        pager.fakeDragBy(dx);
                        lastTranslateX = newTranslationX;
                    } catch (Exception e) {
                        try {
                            pager.endFakeDrag();
                        } catch (Exception e2) {
                            //don't promt
                        }
                        startedScroll = false;
                        FileLog.e("tmessages", e);
                    }
                }
                lastX = ev.getX();
                if (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP) {
                    first = true;
                    if (startedScroll) {
                        pager.endFakeDrag();
                        startedScroll = false;
                    }
                }
                return startedScroll || super.onTouchEvent(ev);
            }
        };
        scrollSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
        scrollSlidingTabStrip.setIndicatorColor(0xffe2e5e7);
        scrollSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
        scrollSlidingTabStrip.setVisibility(INVISIBLE);
        addView(scrollSlidingTabStrip,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.TOP));
        scrollSlidingTabStrip.setTranslationX(AndroidUtilities.displaySize.x);
        updateStickerTabs();
        scrollSlidingTabStrip.setDelegate(new ScrollSlidingTabStrip.ScrollSlidingTabStripDelegate() {
            @Override
            public void onPageSelected(int page) {
                if (gifsGridView != null) {
                    if (page == gifTabBum + 1) {
                        if (gifsGridView.getVisibility() != VISIBLE) {
                            listener.onGifTab(true);
                            showGifTab();
                        }
                    } else {
                        if (gifsGridView.getVisibility() == VISIBLE) {
                            listener.onGifTab(false);
                            gifsGridView.setVisibility(GONE);
                            stickersGridView.setVisibility(VISIBLE);
                            stickersEmptyView
                                    .setVisibility(stickersGridAdapter.getCount() != 0 ? GONE : VISIBLE);
                        }
                    }
                }
                if (page == 0) {
                    pager.setCurrentItem(0);
                    return;
                } else {
                    if (page == gifTabBum + 1) {
                        return;
                    } else {
                        if (page == recentTabBum + 1) {
                            views.get(6).setSelection(0);
                            return;
                        }
                    }
                }
                int index = page - 1 - stickersTabOffset;
                if (index == stickerSets.size()) {
                    if (listener != null) {
                        listener.onStickersSettingsClick();
                    }
                    return;
                }
                if (index >= stickerSets.size()) {
                    index = stickerSets.size() - 1;
                }
                views.get(6).setSelection(stickersGridAdapter.getPositionForPack(stickerSets.get(index)));
            }
        });

        stickersGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                checkStickersScroll(firstVisibleItem);
            }
        });
    }

    setBackgroundColor(0xfff5f6f7);

    pager = new ViewPager(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pager.setAdapter(new EmojiPagesAdapter());

    pagerSlidingTabStripContainer = new LinearLayout(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pagerSlidingTabStripContainer.setOrientation(LinearLayout.HORIZONTAL);
    pagerSlidingTabStripContainer.setBackgroundColor(0xfff5f6f7);
    addView(pagerSlidingTabStripContainer, LayoutHelper.createFrame(LayoutParams.MATCH_PARENT, 48));

    PagerSlidingTabStrip pagerSlidingTabStrip = new PagerSlidingTabStrip(context);
    pagerSlidingTabStrip.setViewPager(pager);
    pagerSlidingTabStrip.setShouldExpand(true);
    pagerSlidingTabStrip.setIndicatorHeight(AndroidUtilities.dp(2));
    pagerSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
    pagerSlidingTabStrip.setIndicatorColor(0xff2b96e2);
    pagerSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
    pagerSlidingTabStripContainer.addView(pagerSlidingTabStrip, LayoutHelper.createLinear(0, 48, 1.0f));
    pagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            EmojiView.this.onPageScrolled(position, getMeasuredWidth(), positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    FrameLayout frameLayout = new FrameLayout(context);
    pagerSlidingTabStripContainer.addView(frameLayout, LayoutHelper.createLinear(52, 48));

    backspaceButton = new ImageView(context) {
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                backspacePressed = true;
                backspaceOnce = false;
                postBackspaceRunnable(350);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL
                    || event.getAction() == MotionEvent.ACTION_UP) {
                backspacePressed = false;
                if (!backspaceOnce) {
                    if (listener != null && listener.onBackspace()) {
                        backspaceButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                    }
                }
            }
            super.onTouchEvent(event);
            return true;
        }
    };
    backspaceButton.setImageResource(R.drawable.ic_smiles_backspace);
    backspaceButton.setBackgroundResource(R.drawable.ic_emoji_backspace);
    backspaceButton.setScaleType(ImageView.ScaleType.CENTER);
    frameLayout.addView(backspaceButton, LayoutHelper.createFrame(52, 48));

    View view = new View(context);
    view.setBackgroundColor(0xffe2e5e7);
    frameLayout.addView(view, LayoutHelper.createFrame(52, 1, Gravity.LEFT | Gravity.BOTTOM));

    recentsWrap = new FrameLayout(context);
    recentsWrap.addView(views.get(0));

    TextView textView = new TextView(context);
    textView.setTypeface(FontManager.instance().getTypeface());
    textView.setText(LocaleController.getString("NoRecent", R.string.NoRecent));
    textView.setTextSize(18);
    textView.setTextColor(0xff888888);
    textView.setGravity(Gravity.CENTER);
    recentsWrap.addView(textView);
    views.get(0).setEmptyView(textView);

    addView(pager, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT,
            Gravity.LEFT | Gravity.TOP, 0, 48, 0, 0));

    emojiSize = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 40 : 32);
    pickerView = new EmojiColorPickerView(context);
    pickerViewPopup = new EmojiPopupWindow(pickerView,
            popupWidth = AndroidUtilities.dp((AndroidUtilities.isTablet() ? 40 : 32) * 6 + 10 + 4 * 5),
            popupHeight = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 64 : 56));
    pickerViewPopup.setOutsideTouchable(true);
    pickerViewPopup.setClippingEnabled(true);
    pickerViewPopup.setInputMethodMode(EmojiPopupWindow.INPUT_METHOD_NOT_NEEDED);
    pickerViewPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
    pickerViewPopup.getContentView().setFocusableInTouchMode(true);
    pickerViewPopup.getContentView().setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
                    && event.getAction() == KeyEvent.ACTION_UP && pickerViewPopup != null
                    && pickerViewPopup.isShowing()) {
                pickerViewPopup.dismiss();
                return true;
            }
            return false;
        }
    });

    loadRecents();
}

From source file:com.b44t.ui.Components.EmojiView.java

public EmojiView(boolean needStickers, boolean needGif, final Context context) {
    super(context);

    showStickers = needStickers;/* w w w  . j a v  a 2s .  c o  m*/
    showGifs = needGif;

    for (int i = 0; i < EmojiData.dataColored.length + 1; i++) {
        GridView gridView = new GridView(context);
        if (AndroidUtilities.isTablet()) {
            gridView.setColumnWidth(AndroidUtilities.dp(60));
        } else {
            gridView.setColumnWidth(AndroidUtilities.dp(45));
        }
        gridView.setNumColumns(-1);
        views.add(gridView);

        EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(i - 1);
        gridView.setAdapter(emojiGridAdapter);
        AndroidUtilities.setListViewEdgeEffectColor(gridView, 0xfff5f6f7);
        adapters.add(emojiGridAdapter);
    }

    if (showStickers) {
        //StickersQuery.checkStickers();
        stickersGridView = new GridView(context) {
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                boolean result = StickerPreviewViewer.getInstance().onInterceptTouchEvent(event,
                        stickersGridView, EmojiView.this.getMeasuredHeight());
                return super.onInterceptTouchEvent(event) || result;
            }

            @Override
            public void setVisibility(int visibility) {
                if (gifsGridView != null && gifsGridView.getVisibility() == VISIBLE) {
                    super.setVisibility(GONE);
                    return;
                }
                super.setVisibility(visibility);
            }
        };
        stickersGridView.setSelector(R.drawable.transparent);
        stickersGridView.setColumnWidth(AndroidUtilities.dp(72));
        stickersGridView.setNumColumns(-1);
        stickersGridView.setPadding(0, AndroidUtilities.dp(4), 0, 0);
        stickersGridView.setClipToPadding(false);
        views.add(stickersGridView);
        stickersGridAdapter = new StickersGridAdapter(context);
        stickersGridView.setAdapter(stickersGridAdapter);
        stickersGridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return StickerPreviewViewer.getInstance().onTouch(event, stickersGridView,
                        EmojiView.this.getMeasuredHeight(), stickersOnItemClickListener);
            }
        });
        stickersOnItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long i) {
                if (!(view instanceof StickerEmojiCell)) {
                    return;
                }
                StickerPreviewViewer.getInstance().reset();
                StickerEmojiCell cell = (StickerEmojiCell) view;
                if (cell.isDisabled()) {
                    return;
                }
                cell.disable();
                TLRPC.Document document = cell.getSticker();
                addRecentSticker(document);
                if (listener != null) {
                    listener.onStickerSelected(document);
                }
            }
        };
        stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
        AndroidUtilities.setListViewEdgeEffectColor(stickersGridView, 0xfff5f6f7);

        stickersWrap = new FrameLayout(context);
        stickersWrap.addView(stickersGridView);

        if (needGif) {
            gifsGridView = new RecyclerListView(context);
            gifsGridView.setTag(11);
            gifsGridView.setLayoutManager(flowLayoutManager = new ExtendedGridLayoutManager(context, 100) {

                private Size size = new Size();

                @Override
                protected Size getSizeForItem(int i) {
                    TLRPC.Document document = recentImages.get(i).document;
                    size.width = document.thumb != null && document.thumb.w != 0 ? document.thumb.w : 100;
                    size.height = document.thumb != null && document.thumb.h != 0 ? document.thumb.h : 100;
                    for (int b = 0; b < document.attributes.size(); b++) {
                        TLRPC.DocumentAttribute attribute = document.attributes.get(b);
                        if (attribute instanceof TLRPC.TL_documentAttributeImageSize
                                || attribute instanceof TLRPC.TL_documentAttributeVideo) {
                            size.width = attribute.w;
                            size.height = attribute.h;
                            break;
                        }
                    }
                    return size;
                }
            });
            flowLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return flowLayoutManager.getSpanSizeForItem(position);
                }
            });
            gifsGridView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(android.graphics.Rect outRect, View view, RecyclerView parent,
                        RecyclerView.State state) {
                    outRect.left = 0;
                    outRect.top = 0;
                    outRect.bottom = 0;
                    int position = parent.getChildAdapterPosition(view);
                    if (!flowLayoutManager.isFirstRow(position)) {
                        outRect.top = AndroidUtilities.dp(2);
                    }
                    outRect.right = flowLayoutManager.isLastInRow(position) ? 0 : AndroidUtilities.dp(2);
                }
            });
            gifsGridView.setOverScrollMode(RecyclerListView.OVER_SCROLL_NEVER);
            gifsGridView.setAdapter(gifsAdapter = new GifsAdapter(context));
            gifsGridView.setOnItemClickListener(new RecyclerListView.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if (position < 0 || position >= recentImages.size() || listener == null) {
                        return;
                    }
                    TLRPC.Document document = recentImages.get(position).document;
                    listener.onGifSelected(document);
                }
            });
            gifsGridView.setOnItemLongClickListener(new RecyclerListView.OnItemLongClickListener() {
                @Override
                public boolean onItemClick(View view, int position) {
                    if (position < 0 || position >= recentImages.size()) {
                        return false;
                    }
                    final MediaController.SearchImage searchImage = recentImages.get(position);
                    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                    builder.setMessage(LocaleController.getString("DeleteGif", R.string.DeleteGif));
                    builder.setPositiveButton(LocaleController.getString("OK", R.string.OK).toUpperCase(),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    recentImages.remove(searchImage);
                                    /*TLRPC.TL_messages_saveGif req = new TLRPC.TL_messages_saveGif();
                                    req.id = new TLRPC.TL_inputDocument();
                                    req.id.id = searchImage.document.id;
                                    req.id.access_hash = searchImage.document.access_hash;
                                    req.unsave = true;
                                    ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
                                    @Override
                                    public void run(TLObject response, TLRPC.TL_error error) {
                                            
                                    }
                                    });*/
                                    //MessagesStorage.getInstance().removeWebRecent(searchImage);
                                    if (gifsAdapter != null) {
                                        gifsAdapter.notifyDataSetChanged();
                                    }
                                    if (recentImages.isEmpty()) {
                                        updateStickerTabs();
                                    }
                                }
                            });
                    builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
                    builder.show().setCanceledOnTouchOutside(true);
                    return true;
                }
            });
            gifsGridView.setVisibility(GONE);
            stickersWrap.addView(gifsGridView);
        }

        stickersEmptyView = new TextView(context);
        stickersEmptyView.setText(LocaleController.getString("NoStickers", R.string.NoStickers));
        stickersEmptyView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        stickersEmptyView.setTextColor(0xff888888);
        stickersWrap.addView(stickersEmptyView,
                LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER));
        stickersGridView.setEmptyView(stickersEmptyView);

        scrollSlidingTabStrip = new ScrollSlidingTabStrip(context) {

            boolean startedScroll;
            float lastX;
            float lastTranslateX;
            boolean first = true;

            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                return super.onInterceptTouchEvent(ev);
            }

            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (first) {
                    first = false;
                    lastX = ev.getX();
                }
                float newTranslationX = scrollSlidingTabStrip.getTranslationX();
                if (scrollSlidingTabStrip.getScrollX() == 0 && newTranslationX == 0) {
                    if (!startedScroll && lastX - ev.getX() < 0) {
                        if (pager.beginFakeDrag()) {
                            startedScroll = true;
                            lastTranslateX = scrollSlidingTabStrip.getTranslationX();
                        }
                    } else if (startedScroll && lastX - ev.getX() > 0) {
                        if (pager.isFakeDragging()) {
                            pager.endFakeDrag();
                            startedScroll = false;
                        }
                    }
                }
                if (startedScroll) {
                    int dx = (int) (ev.getX() - lastX + newTranslationX - lastTranslateX);
                    try {
                        pager.fakeDragBy(dx);
                        lastTranslateX = newTranslationX;
                    } catch (Exception e) {
                        try {
                            pager.endFakeDrag();
                        } catch (Exception e2) {
                            //don't promt
                        }
                        startedScroll = false;
                        FileLog.e("messenger", e);
                    }
                }
                lastX = ev.getX();
                if (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP) {
                    first = true;
                    if (startedScroll) {
                        pager.endFakeDrag();
                        startedScroll = false;
                    }
                }
                return startedScroll || super.onTouchEvent(ev);
            }
        };
        scrollSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
        scrollSlidingTabStrip.setIndicatorColor(0xffe2e5e7);
        scrollSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
        scrollSlidingTabStrip.setVisibility(INVISIBLE);
        addView(scrollSlidingTabStrip,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.TOP));
        scrollSlidingTabStrip.setTranslationX(AndroidUtilities.displaySize.x);
        updateStickerTabs();
        scrollSlidingTabStrip.setDelegate(new ScrollSlidingTabStrip.ScrollSlidingTabStripDelegate() {
            @Override
            public void onPageSelected(int page) {
                if (gifsGridView != null) {
                    if (page == gifTabBum + 1) {
                        if (gifsGridView.getVisibility() != VISIBLE) {
                            listener.onGifTab(true);
                            showGifTab();
                        }
                    } else {
                        if (gifsGridView.getVisibility() == VISIBLE) {
                            listener.onGifTab(false);
                            gifsGridView.setVisibility(GONE);
                            stickersGridView.setVisibility(VISIBLE);
                            stickersEmptyView
                                    .setVisibility(stickersGridAdapter.getCount() != 0 ? GONE : VISIBLE);
                        }
                    }
                }
                if (page == 0) {
                    pager.setCurrentItem(0);
                    return;
                } else {
                    if (page == gifTabBum + 1) {
                        return;
                    } else {
                        if (page == recentTabBum + 1) {
                            views.get(6).setSelection(0);
                            return;
                        }
                    }
                }
                int index = page - 1 - stickersTabOffset;
                if (index == stickerSets.size()) {
                    if (listener != null) {
                        listener.onStickersSettingsClick();
                    }
                    return;
                }
                if (index >= stickerSets.size()) {
                    index = stickerSets.size() - 1;
                }
                views.get(6).setSelection(stickersGridAdapter.getPositionForPack(stickerSets.get(index)));
            }
        });

        stickersGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                checkStickersScroll(firstVisibleItem);
            }
        });
    }

    setBackgroundColor(0xfff5f6f7);

    pager = new ViewPager(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pager.setAdapter(new EmojiPagesAdapter());

    pagerSlidingTabStripContainer = new LinearLayout(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pagerSlidingTabStripContainer.setOrientation(LinearLayout.HORIZONTAL);
    pagerSlidingTabStripContainer.setBackgroundColor(0xfff5f6f7);
    addView(pagerSlidingTabStripContainer, LayoutHelper.createFrame(LayoutParams.MATCH_PARENT, 48));

    PagerSlidingTabStrip pagerSlidingTabStrip = new PagerSlidingTabStrip(context);
    pagerSlidingTabStrip.setViewPager(pager);
    pagerSlidingTabStrip.setShouldExpand(true);
    pagerSlidingTabStrip.setIndicatorHeight(AndroidUtilities.dp(2));
    pagerSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
    pagerSlidingTabStrip.setIndicatorColor(0xff2b96e2);
    pagerSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
    pagerSlidingTabStripContainer.addView(pagerSlidingTabStrip, LayoutHelper.createLinear(0, 48, 1.0f));
    pagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            EmojiView.this.onPageScrolled(position, getMeasuredWidth(), positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    FrameLayout frameLayout = new FrameLayout(context);
    pagerSlidingTabStripContainer.addView(frameLayout, LayoutHelper.createLinear(52, 48));

    backspaceButton = new ImageView(context) {
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                backspacePressed = true;
                backspaceOnce = false;
                postBackspaceRunnable(350);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL
                    || event.getAction() == MotionEvent.ACTION_UP) {
                backspacePressed = false;
                if (!backspaceOnce) {
                    if (listener != null && listener.onBackspace()) {
                        backspaceButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                    }
                }
            }
            super.onTouchEvent(event);
            return true;
        }
    };
    backspaceButton.setImageResource(R.drawable.ic_smiles_backspace);
    backspaceButton.setBackgroundResource(R.drawable.ic_emoji_backspace);
    backspaceButton.setScaleType(ImageView.ScaleType.CENTER);
    frameLayout.addView(backspaceButton, LayoutHelper.createFrame(52, 48));

    View view = new View(context);
    view.setBackgroundColor(0xffe2e5e7);
    frameLayout.addView(view, LayoutHelper.createFrame(52, 1, Gravity.LEFT | Gravity.BOTTOM));

    recentsWrap = new FrameLayout(context);
    recentsWrap.addView(views.get(0));

    TextView textView = new TextView(context);
    textView.setText(LocaleController.getString("NoRecent", R.string.NoRecent));
    textView.setTextSize(18);
    textView.setTextColor(0xff888888);
    textView.setGravity(Gravity.CENTER);
    recentsWrap.addView(textView);
    views.get(0).setEmptyView(textView);

    addView(pager, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT,
            Gravity.LEFT | Gravity.TOP, 0, 48, 0, 0));

    emojiSize = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 40 : 32);
    pickerView = new EmojiColorPickerView(context);
    pickerViewPopup = new EmojiPopupWindow(pickerView,
            popupWidth = AndroidUtilities.dp((AndroidUtilities.isTablet() ? 40 : 32) * 6 + 10 + 4 * 5),
            popupHeight = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 64 : 56));
    pickerViewPopup.setOutsideTouchable(true);
    pickerViewPopup.setClippingEnabled(true);
    pickerViewPopup.setInputMethodMode(EmojiPopupWindow.INPUT_METHOD_NOT_NEEDED);
    pickerViewPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
    pickerViewPopup.getContentView().setFocusableInTouchMode(true);
    pickerViewPopup.getContentView().setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
                    && event.getAction() == KeyEvent.ACTION_UP && pickerViewPopup != null
                    && pickerViewPopup.isShowing()) {
                pickerViewPopup.dismiss();
                return true;
            }
            return false;
        }
    });

    loadRecents();
}

From source file:kr.wdream.ui.DialogsActivity.java

public void initView() {

    RelativeLayout relativeLayout = new RelativeLayout(context);
    fragmentView = relativeLayout;//from   w ww . j av a  2  s . co m

    LinearLayout lytTab = new LinearLayout(context);
    lytTab.setId(kr.wdream.storyshop.R.id.lytTab);

    tabParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT, 1);
    tabParams.setMargins(0, 0, 0, 5);

    tab1 = new LinearLayout(context);
    tab1.setGravity(Gravity.CENTER);
    imgTab1 = new ImageView(context);
    imgTab1.setImageResource(R.drawable.m_i_main_flist_n);
    tab1.addView(imgTab1, LayoutHelper.createLinear(21, 20));
    tab1.setOnClickListener(this);

    tab2 = new LinearLayout(context);
    tab2.setGravity(Gravity.CENTER);
    imgTab2 = new ImageView(context);
    imgTab2.setImageResource(R.drawable.m_i_main_clist_n);
    tab2.addView(imgTab2, LayoutHelper.createLinear(21, 20));
    tab2.setOnClickListener(this);

    tab3 = new LinearLayout(context);
    tab3.setGravity(Gravity.CENTER);
    imgTab3 = new ImageView(context);
    imgTab3.setImageResource(R.drawable.m_i_main_content_n);
    tab3.addView(imgTab3, LayoutHelper.createLinear(21, 20));
    tab3.setOnClickListener(this);

    tab4 = new LinearLayout(context);
    tab4.setGravity(Gravity.CENTER);
    imgTab4 = new ImageView(context);
    imgTab4.setImageResource(R.drawable.m_i_main_setting_n);
    tab4.addView(imgTab4, LayoutHelper.createLinear(21, 20));
    tab4.setOnClickListener(this);

    lytDialogs = new RelativeLayout(context);
    RelativeLayout.LayoutParams lytDialogsParams = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    lytDialogsParams.addRule(RelativeLayout.BELOW, lytTab.getId());
    lytDialogs.setLayoutParams(lytDialogsParams);
    lytDialogs.setVisibility(View.GONE);
    listView = new RecyclerListView(context);
    listView.setVerticalScrollBarEnabled(true);
    listView.setItemAnimator(null);
    listView.setInstantClick(true);
    listView.setLayoutAnimation(null);
    listView.setTag(4);
    listView.setVisibility(View.GONE);

    layoutManager = new LinearLayoutManager(context) {
        @Override
        public boolean supportsPredictiveItemAnimations() {
            return false;
        }
    };

    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    listView.setLayoutManager(layoutManager);
    listView.setVerticalScrollbarPosition(
            LocaleController.isRTL ? ListView.SCROLLBAR_POSITION_LEFT : ListView.SCROLLBAR_POSITION_RIGHT);
    listView.setVerticalScrollBarEnabled(false);
    listView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
    listView.setVerticalScrollbarPosition(
            LocaleController.isRTL ? ListView.SCROLLBAR_POSITION_LEFT : ListView.SCROLLBAR_POSITION_RIGHT);
    listView.setOnItemClickListener(new RecyclerListView.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            if (listView == null || listView.getAdapter() == null) {
                return;
            }
            long dialog_id = 0;
            int message_id = 0;
            RecyclerView.Adapter adapter = listView.getAdapter();
            if (adapter == dialogsAdapter) {
                TLRPC.TL_dialog dialog = dialogsAdapter.getItem(position);
                if (dialog == null) {
                    return;
                }
                dialog_id = dialog.id;
            } else if (adapter == dialogsSearchAdapter) {
                Object obj = dialogsSearchAdapter.getItem(position);
                if (obj instanceof TLRPC.User) {
                    dialog_id = ((TLRPC.User) obj).id;
                    if (dialogsSearchAdapter.isGlobalSearch(position)) {
                        ArrayList<TLRPC.User> users = new ArrayList<>();
                        users.add((TLRPC.User) obj);
                        MessagesController.getInstance().putUsers(users, false);
                        MessagesStorage.getInstance().putUsersAndChats(users, null, false, true);
                    }
                    if (!onlySelect) {
                        dialogsSearchAdapter.putRecentSearch(dialog_id, (TLRPC.User) obj);
                    }
                } else if (obj instanceof TLRPC.Chat) {
                    if (dialogsSearchAdapter.isGlobalSearch(position)) {
                        ArrayList<TLRPC.Chat> chats = new ArrayList<>();
                        chats.add((TLRPC.Chat) obj);
                        MessagesController.getInstance().putChats(chats, false);
                        MessagesStorage.getInstance().putUsersAndChats(null, chats, false, true);
                    }
                    if (((TLRPC.Chat) obj).id > 0) {
                        dialog_id = -((TLRPC.Chat) obj).id;
                    } else {
                        dialog_id = AndroidUtilities.makeBroadcastId(((TLRPC.Chat) obj).id);
                    }
                    if (!onlySelect) {
                        dialogsSearchAdapter.putRecentSearch(dialog_id, (TLRPC.Chat) obj);
                    }
                } else if (obj instanceof TLRPC.EncryptedChat) {
                    dialog_id = ((long) ((TLRPC.EncryptedChat) obj).id) << 32;
                    if (!onlySelect) {
                        dialogsSearchAdapter.putRecentSearch(dialog_id, (TLRPC.EncryptedChat) obj);
                    }
                } else if (obj instanceof MessageObject) {
                    MessageObject messageObject = (MessageObject) obj;
                    dialog_id = messageObject.getDialogId();
                    message_id = messageObject.getId();
                    dialogsSearchAdapter.addHashtagsFromMessage(dialogsSearchAdapter.getLastSearchString());
                } else if (obj instanceof String) {
                    Log.d(LOG_TAG, "obj String : openSearchField");
                    actionBar.openSearchField((String) obj);
                }
            }

            if (dialog_id == 0) {
                return;
            }

            if (onlySelect) {
                didSelectResult(dialog_id, true, false);
            } else {
                Bundle args = new Bundle();
                int lower_part = (int) dialog_id;
                int high_id = (int) (dialog_id >> 32);
                if (lower_part != 0) {
                    if (high_id == 1) {
                        args.putInt("chat_id", lower_part);
                    } else {
                        if (lower_part > 0) {
                            args.putInt("user_id", lower_part);
                        } else if (lower_part < 0) {
                            if (message_id != 0) {
                                TLRPC.Chat chat = MessagesController.getInstance().getChat(-lower_part);
                                if (chat != null && chat.migrated_to != null) {
                                    args.putInt("migrated_to", lower_part);
                                    lower_part = -chat.migrated_to.channel_id;
                                }
                            }
                            args.putInt("chat_id", -lower_part);
                        }
                    }
                } else {
                    args.putInt("enc_id", high_id);
                }
                if (message_id != 0) {
                    args.putInt("message_id", message_id);
                } else {
                    if (actionBar != null) {
                        actionBar.closeSearchField();
                    }
                }
                if (AndroidUtilities.isTablet()) {
                    if (openedDialogId == dialog_id && adapter != dialogsSearchAdapter) {
                        return;
                    }
                    if (dialogsAdapter != null) {
                        dialogsAdapter.setOpenedDialogId(openedDialogId = dialog_id);
                        updateVisibleRows(MessagesController.UPDATE_MASK_SELECT_DIALOG);
                    }
                }
                if (searchString != null) {
                    if (MessagesController.checkCanOpenChat(args, DialogsActivity.this)) {
                        NotificationCenter.getInstance().postNotificationName(NotificationCenter.closeChats);
                        presentFragment(new ChatActivity(args));
                    }
                } else {
                    if (MessagesController.checkCanOpenChat(args, DialogsActivity.this)) {
                        presentFragment(new ChatActivity(args));
                    }
                }
            }
        }
    });
    listView.setOnItemLongClickListener(new RecyclerListView.OnItemLongClickListener() {
        @Override
        public boolean onItemClick(View view, int position) {
            if (onlySelect || searching && searchWas || getParentActivity() == null) {
                if (searchWas && searching || dialogsSearchAdapter.isRecentSearchDisplayed()) {
                    RecyclerView.Adapter adapter = listView.getAdapter();
                    if (adapter == dialogsSearchAdapter) {
                        Object item = dialogsSearchAdapter.getItem(position);
                        if (item instanceof String || dialogsSearchAdapter.isRecentSearchDisplayed()) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                            builder.setTitle(LocaleController.getString("AppName",
                                    kr.wdream.storyshop.R.string.AppName));
                            builder.setMessage(LocaleController.getString("ClearSearch",
                                    kr.wdream.storyshop.R.string.ClearSearch));
                            builder.setPositiveButton(LocaleController
                                    .getString("ClearButton", kr.wdream.storyshop.R.string.ClearButton)
                                    .toUpperCase(), new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            if (dialogsSearchAdapter.isRecentSearchDisplayed()) {
                                                dialogsSearchAdapter.clearRecentSearch();
                                            } else {
                                                dialogsSearchAdapter.clearRecentHashtags();
                                            }
                                        }
                                    });
                            builder.setNegativeButton(
                                    LocaleController.getString("Cancel", kr.wdream.storyshop.R.string.Cancel),
                                    null);
                            showDialog(builder.create());
                            return true;
                        }
                    }
                }
                return false;
            }
            TLRPC.TL_dialog dialog;
            ArrayList<TLRPC.TL_dialog> dialogs = getDialogsArray();
            if (position < 0 || position >= dialogs.size()) {
                return false;
            }
            dialog = dialogs.get(position);
            selectedDialog = dialog.id;

            BottomSheet.Builder builder = new BottomSheet.Builder(getParentActivity());
            int lower_id = (int) selectedDialog;
            int high_id = (int) (selectedDialog >> 32);

            if (DialogObject.isChannel(dialog)) {
                final TLRPC.Chat chat = MessagesController.getInstance().getChat(-lower_id);
                CharSequence items[];
                if (chat != null && chat.megagroup) {
                    items = new CharSequence[] {
                            LocaleController.getString("ClearHistoryCache",
                                    kr.wdream.storyshop.R.string.ClearHistoryCache),
                            chat == null || !chat.creator
                                    ? LocaleController.getString("LeaveMegaMenu",
                                            kr.wdream.storyshop.R.string.LeaveMegaMenu)
                                    : LocaleController.getString("DeleteMegaMenu",
                                            kr.wdream.storyshop.R.string.DeleteMegaMenu) };
                } else {
                    items = new CharSequence[] {
                            LocaleController.getString("ClearHistoryCache",
                                    kr.wdream.storyshop.R.string.ClearHistoryCache),
                            chat == null || !chat.creator
                                    ? LocaleController.getString("LeaveChannelMenu",
                                            kr.wdream.storyshop.R.string.LeaveChannelMenu)
                                    : LocaleController.getString("ChannelDeleteMenu",
                                            kr.wdream.storyshop.R.string.ChannelDeleteMenu) };
                }
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, final int which) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                        builder.setTitle(
                                LocaleController.getString("AppName", kr.wdream.storyshop.R.string.AppName));
                        if (which == 0) {
                            if (chat != null && chat.megagroup) {
                                builder.setMessage(LocaleController.getString("AreYouSureClearHistorySuper",
                                        kr.wdream.storyshop.R.string.AreYouSureClearHistorySuper));
                            } else {
                                builder.setMessage(LocaleController.getString("AreYouSureClearHistoryChannel",
                                        kr.wdream.storyshop.R.string.AreYouSureClearHistoryChannel));
                            }
                            builder.setPositiveButton(
                                    LocaleController.getString("OK", kr.wdream.storyshop.R.string.OK),
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            MessagesController.getInstance().deleteDialog(selectedDialog, 2);
                                        }
                                    });
                        } else {
                            if (chat != null && chat.megagroup) {
                                if (!chat.creator) {
                                    builder.setMessage(LocaleController.getString("MegaLeaveAlert",
                                            kr.wdream.storyshop.R.string.MegaLeaveAlert));
                                } else {
                                    builder.setMessage(LocaleController.getString("MegaDeleteAlert",
                                            kr.wdream.storyshop.R.string.MegaDeleteAlert));
                                }
                            } else {
                                if (chat == null || !chat.creator) {
                                    builder.setMessage(LocaleController.getString("ChannelLeaveAlert",
                                            kr.wdream.storyshop.R.string.ChannelLeaveAlert));
                                } else {
                                    builder.setMessage(LocaleController.getString("ChannelDeleteAlert",
                                            kr.wdream.storyshop.R.string.ChannelDeleteAlert));
                                }
                            }
                            builder.setPositiveButton(
                                    LocaleController.getString("OK", kr.wdream.storyshop.R.string.OK),
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            MessagesController.getInstance().deleteUserFromChat(
                                                    (int) -selectedDialog, UserConfig.getCurrentUser(), null);
                                            if (AndroidUtilities.isTablet()) {
                                                NotificationCenter.getInstance().postNotificationName(
                                                        NotificationCenter.closeChats, selectedDialog);
                                            }
                                        }
                                    });
                        }
                        builder.setNegativeButton(
                                LocaleController.getString("Cancel", kr.wdream.storyshop.R.string.Cancel),
                                null);
                        showDialog(builder.create());
                    }
                });
                showDialog(builder.create());
            } else {
                final boolean isChat = lower_id < 0 && high_id != 1;
                TLRPC.User user = null;
                if (!isChat && lower_id > 0 && high_id != 1) {
                    user = MessagesController.getInstance().getUser(lower_id);
                }
                final boolean isBot = user != null && user.bot;
                builder.setItems(
                        new CharSequence[] {
                                LocaleController.getString("ClearHistory",
                                        kr.wdream.storyshop.R.string.ClearHistory),
                                isChat ? LocaleController.getString("DeleteChat",
                                        kr.wdream.storyshop.R.string.DeleteChat)
                                        : isBot ? LocaleController.getString("DeleteAndStop",
                                                kr.wdream.storyshop.R.string.DeleteAndStop)
                                                : LocaleController.getString("Delete",
                                                        kr.wdream.storyshop.R.string.Delete) },
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, final int which) {
                                AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                                builder.setTitle(LocaleController.getString("AppName",
                                        kr.wdream.storyshop.R.string.AppName));
                                if (which == 0) {
                                    builder.setMessage(LocaleController.getString("AreYouSureClearHistory",
                                            kr.wdream.storyshop.R.string.AreYouSureClearHistory));
                                } else {
                                    if (isChat) {
                                        builder.setMessage(LocaleController.getString("AreYouSureDeleteAndExit",
                                                kr.wdream.storyshop.R.string.AreYouSureDeleteAndExit));
                                    } else {
                                        builder.setMessage(
                                                LocaleController.getString("AreYouSureDeleteThisChat",
                                                        kr.wdream.storyshop.R.string.AreYouSureDeleteThisChat));
                                    }
                                }
                                builder.setPositiveButton(
                                        LocaleController.getString("OK", kr.wdream.storyshop.R.string.OK),
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                if (which != 0) {
                                                    if (isChat) {
                                                        TLRPC.Chat currentChat = MessagesController
                                                                .getInstance().getChat((int) -selectedDialog);
                                                        if (currentChat != null
                                                                && ChatObject.isNotInChat(currentChat)) {
                                                            MessagesController.getInstance()
                                                                    .deleteDialog(selectedDialog, 0);
                                                        } else {
                                                            MessagesController.getInstance().deleteUserFromChat(
                                                                    (int) -selectedDialog,
                                                                    MessagesController.getInstance().getUser(
                                                                            UserConfig.getClientUserId()),
                                                                    null);
                                                        }
                                                    } else {
                                                        MessagesController.getInstance()
                                                                .deleteDialog(selectedDialog, 0);
                                                    }
                                                    if (isBot) {
                                                        MessagesController.getInstance()
                                                                .blockUser((int) selectedDialog);
                                                    }
                                                    if (AndroidUtilities.isTablet()) {
                                                        NotificationCenter.getInstance().postNotificationName(
                                                                NotificationCenter.closeChats, selectedDialog);
                                                    }
                                                } else {
                                                    MessagesController.getInstance()
                                                            .deleteDialog(selectedDialog, 1);
                                                }
                                            }
                                        });
                                builder.setNegativeButton(LocaleController.getString("Cancel",
                                        kr.wdream.storyshop.R.string.Cancel), null);
                                showDialog(builder.create());
                            }
                        });
                showDialog(builder.create());
            }
            return true;
        }
    });

    listContacts = new LetterSectionsListView(context);
    RelativeLayout.LayoutParams contactParams = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    contactParams.addRule(RelativeLayout.BELOW, lytTab.getId());
    listContacts.setLayoutParams(contactParams);

    Log.d(LOG_TAG, "contactsAdapter : " + LaunchActivity.contactsAdapter.getItem(0));

    listContacts.setAdapter(LaunchActivity.contactsAdapter);

    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.d(LOG_TAG, "postDelayed Start");
            handler.sendEmptyMessage(0);
        }
    }, 1500);

    listContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            int section = LaunchActivity.contactsAdapter.getSectionForPosition(position);
            int row = LaunchActivity.contactsAdapter.getPositionInSectionForPosition(position);

            Object item = LaunchActivity.contactsAdapter.getItem(section, row);

            if (0 == position) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, ContactsController.getInstance().getInviteText());
                getParentActivity().startActivityForResult(Intent.createChooser(intent, LocaleController
                        .getString("InviteFriends", kr.wdream.storyshop.R.string.InviteFriends)), 500);
            } else if (item instanceof ContactsController.Contact) {
                ContactsController.Contact contact = (ContactsController.Contact) item;
                String usePhone = null;
                if (!contact.phones.isEmpty()) {
                    usePhone = contact.phones.get(0);
                }
                if (usePhone == null || getParentActivity() == null) {
                    return;
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                builder.setMessage(
                        LocaleController.getString("InviteUser", kr.wdream.storyshop.R.string.InviteUser));
                builder.setTitle(LocaleController.getString("AppName", kr.wdream.storyshop.R.string.AppName));
                final String arg1 = usePhone;
                builder.setPositiveButton(LocaleController.getString("OK", kr.wdream.storyshop.R.string.OK),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                try {
                                    Intent intent = new Intent(Intent.ACTION_VIEW,
                                            Uri.fromParts("sms", arg1, null));
                                    intent.putExtra("sms_body", LocaleController.getString("InviteText",
                                            kr.wdream.storyshop.R.string.InviteText));
                                    getParentActivity().startActivityForResult(intent, 500);
                                } catch (Exception e) {
                                    FileLog.e("tmessages", e);
                                }
                            }
                        });
                builder.setNegativeButton(
                        LocaleController.getString("Cancel", kr.wdream.storyshop.R.string.Cancel), null);
                showDialog(builder.create());
            } else {
                TLRPC.User user = (TLRPC.User) LaunchActivity.contactsAdapter.getItem(position);

                if (user == null)
                    return;
                Bundle args = new Bundle();
                args.putInt("user_id", user.id);

                if (MessagesController.checkCanOpenChat(args, DialogsActivity.this)) {
                    presentFragment(new ChatActivity(args), false);
                }

            }
        }
    });

    contentLayout = new LinearLayout(context);
    contentLayout.setOrientation(LinearLayout.VERTICAL);
    contentLayout.setVisibility(View.GONE);

    RelativeLayout.LayoutParams contentParams = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    contentParams.addRule(RelativeLayout.BELOW, lytTab.getId());

    // Contents Layout ?
    GridView gridContents = new GridView(context);
    gridContents.setNumColumns(GridView.AUTO_FIT);
    gridContents.setGravity(Gravity.CENTER);

    ContentsAdapter contentsAdapter = new ContentsAdapter(context);
    gridContents.setAdapter(contentsAdapter);

    contentLayout.addView(gridContents,
            LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));

    gridContents.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
            case 0:

                Log.d("??", "grid0");
                Intent intent = new Intent(context, ShoppingMainActivity.class);
                context.startActivity(intent);
                break;

            case 1:
                Log.d("??", "grid1");
                break;

            case 2:
                Log.d("??", "grid2");
                break;

            case 3:
                Log.d("??", "grid3");
                break;
            }
        }
    });

    // Setting Layout ?
    settingLayout = new LinearLayout(context);
    settingLayout.setOrientation(LinearLayout.VERTICAL);
    settingLayout.setVisibility(View.GONE);

    settingLayout.setBackgroundColor(Color.parseColor("#EEEEEE"));

    createSettingLayout();

    // TabBar 
    lytTab.addView(tab1, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 1));
    lytTab.addView(tab2, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 1));
    lytTab.addView(tab3, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 1));
    lytTab.addView(tab4, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 1));

    relativeLayout.addView(lytTab, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 50));

    RelativeLayout.LayoutParams listParams = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    lytDialogs.addView(listView, listParams);
    relativeLayout.addView(lytDialogs, lytDialogsParams);
    relativeLayout.addView(listContacts, contactParams);
    relativeLayout.addView(contentLayout, contentParams);
    relativeLayout.addView(settingLayout, contentParams);

    searchEmptyView = new EmptyTextProgressView(context);
    searchEmptyView.setVisibility(View.GONE);
    searchEmptyView.setShowAtCenter(true);
    searchEmptyView.setText(LocaleController.getString("NoResult", kr.wdream.storyshop.R.string.NoResult));
    relativeLayout.addView(searchEmptyView,
            LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));

    emptyView = new LinearLayout(context);
    emptyView.setOrientation(LinearLayout.VERTICAL);
    emptyView.setVisibility(View.GONE);
    emptyView.setGravity(Gravity.CENTER);
    //        relativeLayout.addView(emptyView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
    emptyView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });

    TextView textView = new TextView(context);
    textView.setText(LocaleController.getString("NoChats", kr.wdream.storyshop.R.string.NoChats));
    textView.setTextColor(0xff959595);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
    emptyView.addView(textView,
            LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT));

    textView = new TextView(context);
    String help = LocaleController.getString("NoChatsHelp", kr.wdream.storyshop.R.string.NoChatsHelp);
    if (AndroidUtilities.isTablet() && !AndroidUtilities.isSmallTablet()) {
        help = help.replace('\n', ' ');
    }
    textView.setText(help);
    textView.setTextColor(0xff959595);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
    textView.setGravity(Gravity.CENTER);
    textView.setPadding(AndroidUtilities.dp(8), AndroidUtilities.dp(6), AndroidUtilities.dp(8), 0);
    textView.setLineSpacing(AndroidUtilities.dp(2), 1);
    emptyView.addView(textView,
            LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT));

    progressView = new ProgressBar(context);
    progressView.setVisibility(View.GONE);

    RelativeLayout.LayoutParams progParams = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    progParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    progressView.setLayoutParams(progParams);
    //        relativeLayout.addView(progressView);
    //        relativeLayout.addView(progressView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER));

    floatingButton = new ImageView(context);
    floatingButton.setVisibility(onlySelect ? View.GONE : View.VISIBLE);
    floatingButton.setScaleType(ImageView.ScaleType.CENTER);
    floatingButton.setBackgroundResource(kr.wdream.storyshop.R.drawable.floating_states);
    floatingButton.setImageResource(kr.wdream.storyshop.R.drawable.floating_pencil);
    Log.d(LOG_TAG, "setVisibility.VISIBLE_floating : " + floatingButton.getVisibility());
    floatingButton.setVisibility(View.GONE);
    Log.d(LOG_TAG, "setVisibility.GONE_floating : " + floatingButton.getVisibility());

    if (Build.VERSION.SDK_INT >= 21) {
        StateListAnimator animator = new StateListAnimator();
        animator.addState(new int[] { android.R.attr.state_pressed },
                ObjectAnimator
                        .ofFloat(floatingButton, "translationZ", AndroidUtilities.dp(2), AndroidUtilities.dp(4))
                        .setDuration(200));
        animator.addState(new int[] {},
                ObjectAnimator
                        .ofFloat(floatingButton, "translationZ", AndroidUtilities.dp(4), AndroidUtilities.dp(2))
                        .setDuration(200));
        floatingButton.setStateListAnimator(animator);
        floatingButton.setOutlineProvider(new ViewOutlineProvider() {
            @SuppressLint("NewApi")
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setOval(0, 0, AndroidUtilities.dp(56), AndroidUtilities.dp(56));
            }
        });
    }
    RelativeLayout.LayoutParams floatingParams = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    floatingParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    floatingParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    //        relativeLayout.addView(floatingButton, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.BOTTOM, LocaleController.isRTL ? 14 : 0, 0, LocaleController.isRTL ? 0 : 14, 14));
    relativeLayout.addView(floatingButton, floatingParams);
    floatingButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle args = new Bundle();
            args.putBoolean("destroyAfterSelect", true);
            presentFragment(new ContactsActivity(args));
        }
    });

    listView.setOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_DRAGGING && searching && searchWas) {
                AndroidUtilities.hideKeyboard(getParentActivity().getCurrentFocus());
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
            int visibleItemCount = Math.abs(layoutManager.findLastVisibleItemPosition() - firstVisibleItem) + 1;
            int totalItemCount = recyclerView.getAdapter().getItemCount();

            if (searching && searchWas) {
                if (visibleItemCount > 0 && layoutManager.findLastVisibleItemPosition() == totalItemCount - 1
                        && !dialogsSearchAdapter.isMessagesSearchEndReached()) {
                    dialogsSearchAdapter.loadMoreSearchMessages();
                }
                return;
            }
            if (visibleItemCount > 0) {
                if (layoutManager.findLastVisibleItemPosition() >= getDialogsArray().size() - 10) {
                    MessagesController.getInstance().loadDialogs(-1, 100,
                            !MessagesController.getInstance().dialogsEndReached);
                }
            }

            if (floatingButton.getVisibility() != View.GONE) {
                final View topChild = recyclerView.getChildAt(0);
                int firstViewTop = 0;
                if (topChild != null) {
                    firstViewTop = topChild.getTop();
                }
                boolean goingDown;
                boolean changed = true;
                if (prevPosition == firstVisibleItem) {
                    final int topDelta = prevTop - firstViewTop;
                    goingDown = firstViewTop < prevTop;
                    changed = Math.abs(topDelta) > 1;
                } else {
                    goingDown = firstVisibleItem > prevPosition;
                }
                if (changed && scrollUpdated) {
                    hideFloatingButton(goingDown);
                }
                prevPosition = firstVisibleItem;
                prevTop = firstViewTop;
                scrollUpdated = true;
            }
        }
    });

    if (searchString == null) {
        dialogsAdapter = new DialogsAdapter(context, dialogsType);
        Log.d("Dialog", "dialogsSize : " + dialogsAdapter.getItemCount());

        if (AndroidUtilities.isTablet() && openedDialogId != 0) {
            dialogsAdapter.setOpenedDialogId(openedDialogId);
        }
        listView.setAdapter(dialogsAdapter);
    }
    int type = 0;
    if (searchString != null) {
        type = 2;
    } else if (!onlySelect) {
        type = 1;
    }
    dialogsSearchAdapter = new DialogsSearchAdapter(context, type, dialogsType);
    dialogsSearchAdapter.setDelegate(new DialogsSearchAdapter.DialogsSearchAdapterDelegate() {
        @Override
        public void searchStateChanged(boolean search) {
            if (searching && searchWas && searchEmptyView != null) {
                if (search) {
                    searchEmptyView.showProgress();
                } else {
                    searchEmptyView.showTextView();
                }
            }
        }

        @Override
        public void didPressedOnSubDialog(int did) {
            if (onlySelect) {
                didSelectResult(did, true, false);
            } else {
                Bundle args = new Bundle();
                if (did > 0) {
                    args.putInt("user_id", did);
                } else {
                    args.putInt("chat_id", -did);
                }
                if (actionBar != null) {
                    actionBar.closeSearchField();
                }
                if (AndroidUtilities.isTablet()) {
                    if (dialogsAdapter != null) {
                        dialogsAdapter.setOpenedDialogId(openedDialogId = did);
                        updateVisibleRows(MessagesController.UPDATE_MASK_SELECT_DIALOG);
                    }
                }
                if (searchString != null) {
                    if (MessagesController.checkCanOpenChat(args, DialogsActivity.this)) {
                        NotificationCenter.getInstance().postNotificationName(NotificationCenter.closeChats);
                        presentFragment(new ChatActivity(args));
                    }
                } else {
                    if (MessagesController.checkCanOpenChat(args, DialogsActivity.this)) {
                        presentFragment(new ChatActivity(args));
                    }
                }
            }
        }

        @Override
        public void needRemoveHint(final int did) {
            if (getParentActivity() == null) {
                return;
            }
            TLRPC.User user = MessagesController.getInstance().getUser(did);
            if (user == null) {
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
            builder.setTitle(LocaleController.getString("AppName", kr.wdream.storyshop.R.string.AppName));
            builder.setMessage(LocaleController.formatString("ChatHintsDelete",
                    kr.wdream.storyshop.R.string.ChatHintsDelete,
                    ContactsController.formatName(user.first_name, user.last_name)));
            builder.setPositiveButton(LocaleController.getString("OK", kr.wdream.storyshop.R.string.OK),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            SearchQuery.removePeer(did);
                        }
                    });
            builder.setNegativeButton(LocaleController.getString("Cancel", kr.wdream.storyshop.R.string.Cancel),
                    null);
            showDialog(builder.create());
        }
    });

    if (MessagesController.getInstance().loadingDialogs && MessagesController.getInstance().dialogs.isEmpty()) {
        searchEmptyView.setVisibility(View.GONE);
        emptyView.setVisibility(View.GONE);
        listView.setEmptyView(progressView);
    } else {
        searchEmptyView.setVisibility(View.GONE);
        progressView.setVisibility(View.GONE);
        listView.setEmptyView(emptyView);
    }
    if (searchString != null) {
        actionBar.openSearchField(searchString);
    }

    if (!onlySelect && dialogsType == 0) {
        relativeLayout.addView(new PlayerView(context, this), LayoutHelper
                .createFrame(LayoutHelper.MATCH_PARENT, 39, Gravity.TOP | Gravity.LEFT, 0, -36, 0, 0));
    }

    tab1.performClick();
}