我正在研究nestedscrollview
一段时间了。它包含一个孩子RelativeLayout
。这是伪代码。
<NestedScrollView>
<RelativeLayout>
<TextView /> //text A
<CalendaerView />
<TextView /> //id = date_string //shows a date string
<RecyclerView /> //list of events
<RelativeLayout>
</NestedScrollView>
这个想法是在向下滚动时,整个视图应该正常滚动,但是一旦日期字符串TextView
到达可见区域的顶部,它就必须保持在顶部以进一步向下滚动。但是,当它向上滚动时,我应该能够看到CalendarView
和文字A。
我尝试了nestedscrollview
的滚动侦听器,并在textview
之外创建了另一个nestedscrollview
并更改了visibility
。
<RelativeLayout>
<TextView /> //to mimic date string
<NestedScrollView>
<RelativeLayout>
<TextView /> //text A
<CalendaerView />
<TextView /> //id = date_string //shows a date string
<RecyclerView /> //list of events
<RelativeLayout>
</NestedScrollView>
</RelativeLayout>
这是滚动侦听器逻辑
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (isDone && scrollY < oldScrollY) {
//scroll up
int firstVisibleItem = ((LinearLayoutManager) mRecycler.getLayoutManager())
.findFirstVisibleItemPosition();
if(0 == firstVisibleItem) {
textA.setVisibility(View.VISIBLE);
calendarView.setVisibility(View.VISIBLE);
dateString.setVisibility(View.VISIBLE);
dateStringCopy.setVisibility(View.GONE);
}
}
if (scrollY > oldScrollY) {
//scroll down
int dateTop = dateString.getTop();
//check if datestring reaches the top of screen
if(0 >= dateTop - scrollY) {
isDone = false;
textA.setVisibility(View.GONE);
calendarView.setVisibility(View.GONE);
dateString.setVisibility(View.GONE);
dateStringCopy.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
isDone = true;
}
}, 50);
}
}
}
实际上,体验并不是非常顺利,因为它涉及改变视图的可见性。有没有人有更好的方法来解决这个问题?