Recyclerview:扩展文本视图

Recyclerview: expanding a textview

我在 recyclerview 中有一个 textView,目前看起来像这样:

全文说:"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."

如果我单击插入符号(顶部的小向下箭头),我希望它展开以便我可以看到所有行。

在我的 recyclerview 适配器中,我有以下代码:

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

我只是在 textView 中操作 maxLines 以尝试扩展 space 它将在 textView 中使用,以便在我通知 ItemChanged 时视图会自行调整,但是,这不起作用,我的信息文本视图不展开。

谁能告诉我如何让它正常工作?

使用这个库可能是解决这个难题最简单、最快捷的方法:

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

尽管如此,我还是希望以旧的方式完成它,而不是导入新的库,所以如果其他人对如何在没有库的情况下执行此操作有任何想法,请随时 post 您的以下建议。

它不起作用的原因是无论如何都会调用 ((InfoViewHolder) holder).info.setMaxLines(3);

所以,在OnClickListener里面,应该是:

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

还有,关于ExpandableTextView library, currently it doesn't work in RecyclerView. See my comments on the issue