【问题标题】:Stick TextView on the top once it is scrolled to the top of the visible area将 TextView 滚动到可见区域的顶部后,将其粘贴在顶部
【发布时间】:2017-12-06 09:05:42
【问题描述】:

我正在处理这个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 的滚动监听器,并在 nestedscrollview 之外创建了另一个 textview 并更改了 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);
                }
            }
        }

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

【问题讨论】:

  • 希望this 是您要找的。​​span>
  • 我希望了解它在没有第三方库的情况下的工作方式。但是,是的,我们走了。我将根据我的用例对其进行修改。谢谢!

标签: android view android-recyclerview android-nestedscrollview


【解决方案1】:

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

https://github.com/amarjain07/StickyScrollView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多