您好,我正在处理一个项目。我需要在其中获得文本视图的第一条可见线。这意味着当textview有100行并且用户已滚动到49行时,我需要获取第50行的数据。
这是我的代码,但是它不起作用,我每次都在获取第一行数据:
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(final RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager layoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
textView = ((RewardTransactionsAdapter.MyViewHolder) recyclerView.
findViewHolderForLayoutPosition(firstVisiblePosition))
.itemView.findViewById(R.id.textViewRTTitle);
Rect r1 = new Rect();
recyclerView.getHitRect(r1);//gets visible rect of textview
Layout l = textView.getLayout();
int line = l.getLineForVertical(-1 * r1.top);//first visible line
int start = l.getLineStart(line);//visible line start
int end = l.getLineEnd(line);//visible line end
String displayed = textView.getText().toString().substring(start, end);
Log.i("TAG", displayed);
}
});