P
P
Pavel2015-05-08 09:06:47
Android
Pavel, 2015-05-08 09:06:47

How to make alphabetical list on left in RecyclerView?

I want to implement such a thing (group name/image on the left)
components_dividers_usage6.png
But I can't figure out how.
All that comes to mind is to put one list into another, but this option is confusing.
Prompt the literature, or an example what.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Paul, 2015-05-12
@PaulTMatik

After reading the RecyclerView documentation for a hundred rows, I came across such a thing RecyclerView.ItemDecoration With it, you can change the list item according to a certain condition. I searched for examples on this topic and came across this . Changed the drawVertical method like this:

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;
        }

and here's what I got in the end:
What I wrote is nonsense, in fact, this approach is not suitable for RecyclerView due to its features. So parent.getChildCount(); will show the number of only visible lines, and while loading new ones, the drawVertical method will be called constantly. As a result, all new lines will be labeled.
Well, now I know where to dig.

E
Elsh Yff, 2015-05-08
@Aznix

This and This will help you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question