一旦滚动到可见区域的顶部,就将TextView粘贴在顶部

时间:2017-12-06 09:05:43

标签: android view android-recyclerview android-nestedscrollview

我正在研究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);
                }
            }
        }

实际上,体验并不是非常顺利,因为它涉及改变视图的可见性。有没有人有更好的方法来解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试这个简单的Github库项目。

https://github.com/amarjain07/StickyScrollView

enter image description here