思路
这里以? RecyclerView为例,添加监听OnScrollChangeLinstener,通过监听recyclerView当前可见的第一个item的position
添加监听:
recyclerView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int position = layoutManager.findFirstVisibleItemPosition();
//当前是在fragment
MainActivity.mainActivity.updateUp(position > 3);
}
});
调用
public void updateUp(boolean flag) {
if (flag) {
//显示回到顶部按钮
test.setText("回顶部");
} else {
//正常显示
test.setText("首页");
}
}
|