Multiple line TextView getLineForVertical returning wrong line order in Android

a 夏天 提交于 2020-06-26 12:08:14

问题


The TextViews in a RecyclerView and, running following code when the Recyclerview onDown event happen:

@Override
public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
    int action = e.getAction() & MotionEvent.ACTION_MASK;
    TextView childTextView = (TextView) rv.findChildViewUnder(e.getX(), e.getY());
    
    if(action == MotionEvent.ACTION_DOWN) {
        // y-coordinate of clicked point. 
        int y = (int) e.getY() - childTextView.getTotalPaddingTop();
        // line order of clicked textview
        int line = childTextView.getLayout().getLineForVertical(y);
        // character offset of clicked line.
        int offset = childTextView.getLayout().getOffsetForHorizontal(line, e.getX());
    }
}

If the TextView is single line no problem the line order and offset of clicked TextView is happening true values but within multiple line, the line order is happening total line number + 1 of the TextView. If the TextView is multiple lines how to return touched line order of the TextView? (The TextViews has spanners and text size of lines different)

来源:https://stackoverflow.com/questions/62528990/multiple-line-textview-getlineforvertical-returning-wrong-line-order-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!