I'm trying to count the number of lines of a TextView
in a RecyclerView adapter.
Following answers from other questions (although not related to RecyclerViews), I tried adding this to my onBindViewHolder
method:
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Message message = messages.get(position);
holder.textView.setText(message.getMessage());
holder.textView.setVisibility(View.VISIBLE);
// Get the number of lines
holder.textView.post(new Runnable() {
@Override
public void run() {
int lineCount = holder.textView.getLineCount();
Log.d("COUNT", String.valueOf(lineCount));
}
});
}
But when I open my app, the first RecyclerView item will initially show COUNT 0
(even though it has 5 lines). But when I scroll down a few items and then scroll back up to the first item, it will show the correct number of lines (COUNT 5
).
So what am I doing wrong?