我试图在RecyclerView中获取第一个可见项目的位置,因为我需要一些关于它的信息(例如标题)。不幸的是,当我使用LayoutManager.findFirstVisibleItemPosition()
时,它始终返回0。
此外,我的RecyclerView位于NestedScrollView中。所有这些项目都在PageAdapter(TabLayout)中。
这是我设置RecyclerView的两种方法:
private void initializeAdapter() {
NaBiezacoAktualnosciAdapter adapter = new NaBiezacoAktualnosciAdapter(items, getContext());
recyclerView.setAdapter(adapter);
}
private void initializeLayoutManager(final View v) {
final ExpansiveLayoutManager llm = new ExpansiveLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
}
})
);
NestedScrollView nestedScrollView = (NestedScrollView) v.findViewById(R.id.nested);
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
int firstVisibleCard = llm.findFirstCompletelyVisibleItemPosition();
int lastVisibleCard = llm.findLastCompletelyVisibleItemPosition();
Log.d("aaaa", "firstVisibleCard= " + firstVisibleCard);
Log.d("aaaa", "lastVisibleCard= " + lastVisibleCard);
}
});
}
有人知道如何使这项工作?我很高兴任何建议:)