0

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?

Simon
  • 19,658
  • 27
  • 149
  • 217

2 Answers2

0

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.

Simon
  • 19,658
  • 27
  • 149
  • 217
0

The reason it didn't work because ((InfoViewHolder) holder).info.setMaxLines(3); is called no matter what.

So, inside the OnClickListener, it should be:

int maxLines = expandedInfo ? Integer.MAX_VALUE : 3;
((InfoViewHolder) holder).info.setMaxLines(maxLines);
notifyItemChanged(position);

Also, about the ExpandableTextView library, currently it doesn't work in RecyclerView. See my comments on the issue.

Aaron He
  • 5,509
  • 3
  • 34
  • 44