Search in sources :

Example 16 with LayoutManager

use of android.support.v7.widget.RecyclerView.LayoutManager in project QuickReturn by lawloretienne.

the class QuickReturnUtils method getScrollY.

public static int getScrollY(RecyclerView rv, int columnCount) {
    View c = rv.getChildAt(0);
    if (c == null) {
        return 0;
    }
    LinearLayoutManager layoutManager = (LinearLayoutManager) rv.getLayoutManager();
    int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
    //        Log.d("", "getScrollY() : firstVisiblePosition - " + firstVisiblePosition);
    int scrollY = -(c.getTop());
    if (columnCount > 1) {
        sRecyclerViewItemHeights.put(firstVisiblePosition, c.getHeight() + QuickReturnUtils.dp2px(rv.getContext(), 8) / columnCount);
    } else {
        sRecyclerViewItemHeights.put(firstVisiblePosition, c.getHeight());
    }
    if (scrollY < 0)
        scrollY = 0;
    for (int i = 0; i < firstVisiblePosition; ++i) {
        if (// (this is a sanity check)
        sRecyclerViewItemHeights.get(i) != null)
            //add all heights of the views that are gone
            scrollY += sRecyclerViewItemHeights.get(i);
    }
    return scrollY;
}
Also used : LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) AbsListView(android.widget.AbsListView) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) ListView(android.widget.ListView)

Example 17 with LayoutManager

use of android.support.v7.widget.RecyclerView.LayoutManager in project TwinklingRefreshLayout by lcodecorex.

the class ScrollingUtil method isRecyclerViewToTop.

public static boolean isRecyclerViewToTop(RecyclerView recyclerView) {
    if (recyclerView != null) {
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager == null) {
            return true;
        }
        if (manager.getItemCount() == 0) {
            return true;
        }
        if (manager instanceof LinearLayoutManager) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) manager;
            int firstChildTop = 0;
            if (recyclerView.getChildCount() > 0) {
                // 处理item高度超过一屏幕时的情况
                View firstVisibleChild = recyclerView.getChildAt(0);
                if (firstVisibleChild != null && firstVisibleChild.getMeasuredHeight() >= recyclerView.getMeasuredHeight()) {
                    if (android.os.Build.VERSION.SDK_INT < 14) {
                        return !(ViewCompat.canScrollVertically(recyclerView, -1) || recyclerView.getScrollY() > 0);
                    } else {
                        return !ViewCompat.canScrollVertically(recyclerView, -1);
                    }
                }
                // 如果RecyclerView的子控件数量不为0,获取第一个子控件的top
                // 解决item的topMargin不为0时不能触发下拉刷新
                View firstChild = recyclerView.getChildAt(0);
                RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) firstChild.getLayoutParams();
                firstChildTop = firstChild.getTop() - layoutParams.topMargin - getRecyclerViewItemTopInset(layoutParams) - recyclerView.getPaddingTop();
            }
            if (layoutManager.findFirstCompletelyVisibleItemPosition() < 1 && firstChildTop == 0) {
                return true;
            }
        } else if (manager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) manager;
            int[] out = layoutManager.findFirstCompletelyVisibleItemPositions(null);
            if (out[0] < 1) {
                return true;
            }
        }
    }
    return false;
}
Also used : RecyclerView(android.support.v7.widget.RecyclerView) StaggeredGridLayoutManager(android.support.v7.widget.StaggeredGridLayoutManager) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) GridView(android.widget.GridView) AbsListView(android.widget.AbsListView) RecyclerView(android.support.v7.widget.RecyclerView) ScrollView(android.widget.ScrollView) View(android.view.View) ListView(android.widget.ListView) WebView(android.webkit.WebView)

Example 18 with LayoutManager

use of android.support.v7.widget.RecyclerView.LayoutManager in project xabber-android by redsolution.

the class ChatViewerFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.chat_viewer_fragment, container, false);
    contactTitleView = view.findViewById(R.id.contact_title);
    abstractContact = RosterManager.getInstance().getBestContact(account, user);
    contactTitleView.findViewById(R.id.avatar).setOnClickListener(this);
    toolbar = (Toolbar) view.findViewById(R.id.toolbar_default);
    toolbar.inflateMenu(R.menu.chat);
    toolbar.setOnMenuItemClickListener(this);
    toolbar.setNavigationIcon(R.drawable.ic_arrow_left_white_24dp);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            NavUtils.navigateUpFromSameTask(getActivity());
        }
    });
    setHasOptionsMenu(true);
    sendButton = (ImageButton) view.findViewById(R.id.button_send_message);
    sendButton.setColorFilter(ColorManager.getInstance().getAccountPainter().getGreyMain());
    AbstractChat abstractChat = MessageManager.getInstance().getChat(account, user);
    securityButton = (ImageButton) view.findViewById(R.id.button_security);
    if (abstractChat instanceof RegularChat) {
        securityButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showSecurityMenu();
            }
        });
    } else {
        securityButton.setVisibility(View.GONE);
    }
    chatMessageAdapter = new ChatMessageAdapter(getActivity(), account, user, this, this);
    recyclerView = (RecyclerView) view.findViewById(R.id.chat_messages_recycler_view);
    recyclerView.setAdapter(chatMessageAdapter);
    layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setStackFromEnd(true);
    recyclerView.setLayoutManager(layoutManager);
    // to avoid strange bug on some 4.x androids
    view.findViewById(R.id.input_layout).setBackgroundColor(ColorManager.getInstance().getChatInputBackgroundColor());
    inputView = (EditText) view.findViewById(R.id.chat_input);
    view.findViewById(R.id.button_send_message).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            sendMessage();
        }
    });
    inputView.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View view, int keyCode, KeyEvent event) {
            if (SettingsManager.chatsSendByEnter() && event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
                sendMessage();
                return true;
            }
            return false;
        }
    });
    inputView.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!skipOnTextChanges && stopTypingTimer != null) {
                stopTypingTimer.cancel();
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable text) {
            setUpInputViewButtons();
            if (skipOnTextChanges) {
                return;
            }
            ChatStateManager.getInstance().onComposing(account, user, text);
            stopTypingTimer = new Timer();
            stopTypingTimer.schedule(new TimerTask() {

                @Override
                public void run() {
                    Application.getInstance().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            ChatStateManager.getInstance().onPaused(account, user);
                        }
                    });
                }
            }, STOP_TYPING_DELAY);
        }
    });
    final ImageButton emojiButton = (ImageButton) view.findViewById(R.id.button_emoticon);
    final View rootView = view.findViewById(R.id.root_view);
    // Give the topmost view of your activity layout hierarchy. This will be used to measure soft keyboard height
    final EmojiconsPopup popup = new EmojiconsPopup(rootView, getActivity());
    //Will automatically set size according to the soft keyboard size
    popup.setSizeForSoftKeyboard();
    //If the emoji popup is dismissed, change emojiButton to smiley icon
    popup.setOnDismissListener(new PopupWindow.OnDismissListener() {

        @Override
        public void onDismiss() {
            changeEmojiKeyboardIcon(emojiButton, R.drawable.ic_mood_black_24dp);
        }
    });
    //If the text keyboard closes, also dismiss the emoji popup
    popup.setOnSoftKeyboardOpenCloseListener(new EmojiconsPopup.OnSoftKeyboardOpenCloseListener() {

        @Override
        public void onKeyboardOpen(int keyBoardHeight) {
        }

        @Override
        public void onKeyboardClose() {
            if (popup.isShowing())
                popup.dismiss();
        }
    });
    //On emoji clicked, add it to edittext
    popup.setOnEmojiconClickedListener(new EmojiconGridView.OnEmojiconClickedListener() {

        @Override
        public void onEmojiconClicked(Emojicon emojicon) {
            if (inputView == null || emojicon == null) {
                return;
            }
            int start = inputView.getSelectionStart();
            int end = inputView.getSelectionEnd();
            if (start < 0) {
                inputView.append(emojicon.getEmoji());
            } else {
                inputView.getText().replace(Math.min(start, end), Math.max(start, end), emojicon.getEmoji(), 0, emojicon.getEmoji().length());
            }
        }
    });
    //On backspace clicked, emulate the KEYCODE_DEL key event
    popup.setOnEmojiconBackspaceClickedListener(new EmojiconsPopup.OnEmojiconBackspaceClickedListener() {

        @Override
        public void onEmojiconBackspaceClicked(View v) {
            KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
            inputView.dispatchKeyEvent(event);
        }
    });
    // To toggle between text keyboard and emoji keyboard keyboard(Popup)
    emojiButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //If popup is not showing => emoji keyboard is not visible, we need to show it
            if (!popup.isShowing()) {
                //If keyboard is visible, simply show the emoji popup
                if (popup.isKeyBoardOpen()) {
                    popup.showAtBottom();
                    changeEmojiKeyboardIcon(emojiButton, R.drawable.ic_keyboard_black_24dp);
                } else //else, open the text keyboard first and immediately after that show the emoji popup
                {
                    inputView.setFocusableInTouchMode(true);
                    inputView.requestFocus();
                    popup.showAtBottomPending();
                    final InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(inputView, InputMethodManager.SHOW_IMPLICIT);
                    changeEmojiKeyboardIcon(emojiButton, R.drawable.ic_keyboard_black_24dp);
                }
            } else //If popup is showing, simply dismiss it to show the undelying text keyboard
            {
                popup.dismiss();
            }
        }
    });
    attachButton = (ImageButton) view.findViewById(R.id.button_attach);
    attachButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            onAttachButtonPressed();
        }
    });
    return view;
}
Also used : EmojiconGridView(github.ankushsachdeva.emojicon.EmojiconGridView) PopupWindow(android.widget.PopupWindow) ChatMessageAdapter(com.xabber.android.ui.adapter.ChatMessageAdapter) InputMethodManager(android.view.inputmethod.InputMethodManager) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RegularChat(com.xabber.android.data.message.RegularChat) KeyEvent(android.view.KeyEvent) ImageButton(android.widget.ImageButton) TimerTask(java.util.TimerTask) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) EmojiconsPopup(github.ankushsachdeva.emojicon.EmojiconsPopup) AbstractChat(com.xabber.android.data.message.AbstractChat) ImageView(android.widget.ImageView) View(android.view.View) EmojiconGridView(github.ankushsachdeva.emojicon.EmojiconGridView) RecyclerView(android.support.v7.widget.RecyclerView) Timer(java.util.Timer) Emojicon(github.ankushsachdeva.emojicon.emoji.Emojicon)

Example 19 with LayoutManager

use of android.support.v7.widget.RecyclerView.LayoutManager in project android-advancedrecyclerview by h6ah4i.

the class RecyclerViewDragDropManager method performSwapItems.

private void performSwapItems(RecyclerView rv, @Nullable RecyclerView.ViewHolder draggingItemHolder, @NonNull RecyclerView.ViewHolder swapTargetHolder, Rect swapTargetMargins, int fromPosition, int toPosition) {
    if (LOCAL_LOGD) {
        Log.d(TAG, "item swap (from: " + fromPosition + ", to: " + toPosition + ")");
    }
    if (mItemDragEventListener != null) {
        mItemDragEventListener.onItemDragPositionChanged(fromPosition, toPosition);
    }
    final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
    final int layoutType = CustomRecyclerViewUtils.getLayoutType(mRecyclerView);
    final boolean isVertical = (CustomRecyclerViewUtils.extractOrientation(layoutType) == CustomRecyclerViewUtils.ORIENTATION_VERTICAL);
    final int firstVisible = CustomRecyclerViewUtils.findFirstVisibleItemPosition(mRecyclerView, false);
    final View fromView = (draggingItemHolder != null) ? draggingItemHolder.itemView : null;
    final View toView = swapTargetHolder.itemView;
    final View firstView = CustomRecyclerViewUtils.findViewByPosition(layoutManager, firstVisible);
    final int rootFromPosition = draggingItemHolder.getLayoutPosition();
    final int rootToPosition = swapTargetHolder.getLayoutPosition();
    final Integer fromOrigin = getItemViewOrigin(fromView, isVertical);
    final Integer toOrigin = getItemViewOrigin(toView, isVertical);
    final Integer firstOrigin = getItemViewOrigin(firstView, isVertical);
    // NOTE: This method invokes notifyItemMoved() or notifyDataSetChanged() method internally. Be careful!
    mWrapperAdapter.moveItem(fromPosition, toPosition, layoutType);
    if ((firstVisible == rootFromPosition) && (firstOrigin != null) && (toOrigin != null)) {
        scrollBySpecifiedOrientation(rv, -(toOrigin - firstOrigin), isVertical);
        safeEndAnimations(rv);
    } else if ((firstVisible == rootToPosition) && (fromView != null) && (fromOrigin != null) && (!fromOrigin.equals(toOrigin))) {
        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) fromView.getLayoutParams();
        int amount = (isVertical) ? -(layoutManager.getDecoratedMeasuredHeight(fromView) + lp.topMargin + lp.bottomMargin) : -(layoutManager.getDecoratedMeasuredWidth(fromView) + lp.leftMargin + lp.rightMargin);
        scrollBySpecifiedOrientation(rv, amount, isVertical);
        safeEndAnimations(rv);
    }
}
Also used : ViewGroup(android.view.ViewGroup) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) NestedScrollView(android.support.v4.widget.NestedScrollView) RecyclerView(android.support.v7.widget.RecyclerView)

Example 20 with LayoutManager

use of android.support.v7.widget.RecyclerView.LayoutManager in project TinyDancer by friendlyrobotnyc.

the class FPSRecyclerView method onFinishInflate.

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    ButterKnife.bind(this);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    setLayoutManager(layoutManager);
    addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
    setAdapter(adapter);
}
Also used : LinearLayoutManager(android.support.v7.widget.LinearLayoutManager)

Aggregations

LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)205 RecyclerView (android.support.v7.widget.RecyclerView)202 View (android.view.View)151 GridLayoutManager (android.support.v7.widget.GridLayoutManager)73 TextView (android.widget.TextView)66 StaggeredGridLayoutManager (android.support.v7.widget.StaggeredGridLayoutManager)47 ImageView (android.widget.ImageView)44 Bundle (android.os.Bundle)24 Nullable (android.support.annotation.Nullable)24 BindView (butterknife.BindView)22 ArrayList (java.util.ArrayList)21 ViewGroup (android.view.ViewGroup)20 Context (android.content.Context)17 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)16 Intent (android.content.Intent)15 Handler (android.os.Handler)15 Toolbar (android.support.v7.widget.Toolbar)15 List (java.util.List)12 Point (android.graphics.Point)10 Drawable (android.graphics.drawable.Drawable)10