Search code examples
androidandroid-recyclerviewnotifydatasetchangedexpandable

Recyclerview: expanding a textview


I have a textView inside a recyclerview currently that looks like this:

textview

The full text says: "This is the place where I would put the information about the food item. I want to truncate the line to only show 3 lines and then when I press on the caret, it will expand to the maximum amount of lines."

If I click on the caret (the little down arrow at the top), I want it to expand so that I can see all the lines.

In my recyclerview adapter, I have the following code:

    if (holder instanceof InfoViewHolder) {
        ((InfoViewHolder) holder).more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!expandedInfo) {
                    ((InfoViewHolder) holder).info.setMaxLines(Integer.MAX_VALUE);
                    expandedInfo = true;
                }
                ((InfoViewHolder) holder).info.setMaxLines(3);
                notifyItemChanged(position);
            }
        });
    }

I'm just manipulating the maxLines in the textView to try and expand the space it will take in the textView so that the view will adjust itself when I notifyItemChanged, however, this does not work and my information textview does not expand.

Can anyone tell me how I can get this to work properly?


Solution

  • Using this library was probably the easiest and the quickest way to solve this puzzle:

    https://github.com/Manabu-GT/ExpandableTextView

    Although, I would have like to have done it the old fashion way instead of importing a new library so if anyone else has any ideas on how to do this without a library, feel free to post your suggestions below.