如何防止RecyclerView单元的子项在用户滚动时移动

Sud*_*Plz 8 android listview android-layout android-recyclerview recyclerview-layout

我的水平 线上有很长的单元格RecyclerView,我希望它们有一个在用户水平滚动时保持静止的标题.

- Recycler View (A)
-   -   Cell (parent) (B)
-   -   -   Header (C) <-- We want that to be still
-   -   -   Content (D)
Run Code Online (Sandbox Code Playgroud)

这是它在视觉上的样子:

因此,我正在寻找一种方法:

1)当用户在(A)上拖动手指时,停止标题(C)改变位置RecyclerView

要么

2)像正常一样滚动单元格(B),但将其子项(C)的位置改为相反方向,以使标题即使在移动时也显示为静止(在父项的相反方向(B) .

这是我正在尝试构建的内容:

有任何想法吗?

ps 1:我注意到很多SO答案,建议使用ItemDecoration,但所有可能的答案都有VERTICAL实现代码,这与HORIZONTAL实现非常不同.

ps 2我正在以编程方式创建所有视图内容,因此我不会使用布局文件.(那是因为内容将是反应原生视图,我无法创建具有布局文件的内容).

ps 3:我也注意到这ItemDecoration是旧策略,最近的第三方库扩展了LayoutManager.

请稍微说清楚,谢谢.

Sud*_*Plz 0

我最终做了以下事情(感谢 Cheticamp 给我的灵感):

- Helper Header (C) <-- We now have an extra title view
- Recycler View (A)
-   -   Cell (parent) (B)
-   -   -   Header (C) <-- Plus the typical titles within our cells
-   -   -   Content
Run Code Online (Sandbox Code Playgroud)

如你看到的:

  • 我们现在有一个位于 RecyclerView 之外的辅助标头视图
  • RecyclerView 中的标题视图不断移动,但在它们上方我们放置了辅助视图

下面是一些实际代码,看看会发生什么:

公共类 CalendarView 扩展 LinearLayout { 受保护的 LinearLayoutManager mLayoutManager; 受保护的 HeaderView helperHeaderView;受保护的RecyclerView recyclerView;

public CalendarView(final ReactContext context) {
    super(context);


    setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    setOrientation(LinearLayout.VERTICAL);

    helperHeaderView = new HeaderView(context);
    addView(helperHeaderView);




    final DailyViewAdapter adapter = new DailyViewAdapter(context) {
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            super.onBindViewHolder(holder, position);

            // if our header is not assinged any position yet (we haven't given it any data yet)
            if (helperHeaderView.getLastPosition() == null) {
                updateHeaderData(helperHeaderView, globals.getInitialPosition()); // hydrate it
            }
        }
    };

    recyclerView = new SPRecyclerView(context) {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            if (mLayoutManager == null) {
                mLayoutManager = (LinearLayoutManager) getLayoutManager();
            }

            // the width of any header
            int headerWidth = helperHeaderView.getWidth();

            // get the position of the first visible header in the recyclerview
            int firstVisiblePos = mLayoutManager.findFirstVisibleItemPosition();

            // get a ref of the Cell that contains that header
            DayView firstView = (DayView) mLayoutManager.findViewByPosition(firstVisiblePos);

            // get the X coordinate of the first visible header
            float firstViewX = firstView.getX();



            // get the position of the last visible header in the recyclerview
            int lastVisiblePos = mLayoutManager.findLastVisibleItemPosition();

            // get a ref of the Cell that contains that header
            DayView lastView = (DayView) mLayoutManager.findViewByPosition(lastVisiblePos);

            // get the X coordinate of the last visible header
            float lastViewX = lastView.getX();


            // if the first visible position is not the one our header is set to
            if (helperHeaderView.getLastPosition() != firstVisiblePos) {
                // update the header data
                adapter.updateHeaderData(helperHeaderView, firstVisiblePos);
            }

            // if the first visible is not also the last visible (happens when there's only one Cell on screen)
            if (firstVisiblePos == lastVisiblePos) {
                // reset the X coordinates
                helperHeaderView.setX(0);
            } else { // else if there are more than one cells on screen
                // set the X of the helper header, to whatever the last visible header X was, minus the width of the header
                helperHeaderView.setX(lastViewX - headerWidth);
            }

        }
    };


    // ...
Run Code Online (Sandbox Code Playgroud)
  • 现在剩下要做的就是将父布局转换为 aRelativeLayout以使实际视图重叠(辅助标题视图位于回收器视图的正上方)。

  • 另外,您可能想尝试在需要时将辅助视图 alpha 设置为零,以确保它看起来不错

我希望这对将来的人有帮助。