Ответы пользователя по тегу Android
  • Как сделать алфавитный список слева в RecyclerView?

    PaulTMatik
    @PaulTMatik Автор вопроса
    Почитав на сто рядов документацию по RecyclerView наткнулся на такую штуку RecyclerView.ItemDecoration С помощью неё можно изменить элемент списка по определённому условию. Поискал примеры на эту тему наткнулся вот на что. Изменил метод drawVertical таким образом:
    public void drawVertical(Canvas c, RecyclerView parent) {
            final int left = this.dpToPx(72);
            final int right = parent.getWidth() - this.dpToPx(16);
    
            String firstLetter = "";
            String previousFirstLetter = "";
            TextView studentName;
            TextView nextStudentName;
            String nextFirstLetter = "";
    
            TextView groupMarkerText;
    
            final int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                final View nextChild = parent.getChildAt(i + 1);
    
                studentName = (TextView) child.findViewById(R.id.studentName);
                groupMarkerText = (TextView) child.findViewById(R.id.groupMarkerText);
                if (nextChild != null) {
                    nextStudentName = (TextView) nextChild.findViewById(R.id.studentName);
                    nextFirstLetter = (String) nextStudentName.getText().subSequence(0,1);
                }
    
                firstLetter = (String) studentName.getText().subSequence(0, 1);
    
                if (!firstLetter.equals(previousFirstLetter)) {
                    groupMarkerText.setText(firstLetter);
                    groupMarkerText.setVisibility(View.VISIBLE);
                }
    
                if (!nextFirstLetter.equals(firstLetter)) {
                    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                            .getLayoutParams();
                    final int top = child.getBottom() + params.bottomMargin;
                    final int bottom = top + mDivider.getIntrinsicHeight();
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(c);
                }
    
                previousFirstLetter = firstLetter;
            }

    и вот что в итоге получил:
    be897cdb34374dfcb7278e0b73c7e8d5.png
    То что я понаписал - это ерунда на самом деле, такой подход не годится для RecyclerView ввиду его особенностей. Так parent.getChildCount(); покажет количество только видимых строк, а во время подгрузки новых, метод drawVertical будет вызываться постоянно. В результате все новые строки окажутся с метками.
    Ну зато я теперь знаю куда копать.
    Ответ написан
    Комментировать
  • Как включить кнопку во время ввода в текстовое поле?

    PaulTMatik
    @PaulTMatik Автор вопроса
    Сам спросил — сам ответил.
    .setOnKeyListener(...)
    Используется как раз таки для обработки нажатий аппаратной клавиатуры. В моём случае нужно было просто отслеживать изменения с помощью textWatcher()
    lessonNameFld.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            addLessonBtn.setEnabled(true);
            if (lessonNameFld.getText().toString().isEmpty()) {
                addLessonBtn.setEnabled(false);
            }
        }
    }
    Ответ написан
    Комментировать