Android 如何使用协调器父级显示/隐藏卷轴回收器视图上的工厂

Android 如何使用协调器父级显示/隐藏卷轴回收器视图上的工厂,android,android-recyclerview,floating-action-button,recyclerview-layout,Android,Android Recyclerview,Floating Action Button,Recyclerview Layout,我有一个具有协调器布局的活动。活动内部有一个带有回收器视图和浮动按钮的片段。在滚动回收器视图时,如何显示/隐藏浮动按钮并避免使用fab行为 在活动布局中: CoordinatorLayout-->AppBarLayout-->工具栏和框架布局以及底部栏视图 在片段布局中: RelativeLayout-->回收器视图和浮动按钮 我想实现Google+主页之类的东西。 我如何实现这个场景 临时我使用此解决方案解决我的问题: 在我的片段中按接口使用活动的协调器布局,并使用fab行为显示/隐藏fab

我有一个具有协调器布局的活动。活动内部有一个带有回收器视图和浮动按钮的片段。在滚动回收器视图时,如何显示/隐藏浮动按钮并避免使用fab行为

在活动布局中: CoordinatorLayout-->AppBarLayout-->工具栏和框架布局以及底部栏视图

在片段布局中: RelativeLayout-->回收器视图和浮动按钮

我想实现Google+主页之类的东西。 我如何实现这个场景


临时我使用此解决方案解决我的问题:
在我的片段中按接口使用活动的协调器布局,并使用fab行为显示/隐藏fab。。。直到我找到更好的解决方案

此代码工作正常:

 mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    if(dy > 0){
                        mFab.hide();
                    } else{
                        mFab.show();
                    }

                    super.onScrolled(recyclerView, dx, dy);
                }
            });
你不能这样做:

app:layout_anchor="@id/listView"
app:layout_anchorGravity="bottom|end"
看:

没有内置的用于协调器布局的支持 ListView根据


我修改了Leondro的方法,使晶圆厂在滚动时隐藏,并在滚动停止时显示

scrollListener = new RecyclerView.OnScrollListener() {  
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        switch (newState) {
            case RecyclerView.SCROLL_STATE_IDLE:
                fab.show();
                break;
            default:
                fab.hide();
                break;
        }
        super.onScrollStateChanged(recyclerView, newState);
    }
}; 

rv.clearOnScrollListeners();
rv.addOnScrollListener(scrollListener);

以下是工作解决方案:

class HideOnScrollFabBehavior(context: Context?, attrs: AttributeSet?) : FloatingActionButton.Behavior() {

    // changes visibility from GONE to INVISIBLE when fab is hidden because
    // due to CoordinatorLayout.onStartNestedScroll() implementation
    // child view's (here, fab) onStartNestedScroll won't be called anymore
    // because it's visibility is GONE
    private val listener = object : FloatingActionButton.OnVisibilityChangedListener() {
        override fun onHidden(fab: FloatingActionButton?) {
            fab?.visibility = INVISIBLE
        }
    }

    override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout, child: FloatingActionButton, directTargetChild: View, target: View, axes: Int, type: Int): Boolean {
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL // Ensure we react to vertical scrolling
                || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type)
    }

    override fun onNestedScroll(coordinatorLayout: CoordinatorLayout, child: FloatingActionButton, target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int, consumed: IntArray) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type, consumed)
        if (!target.canScrollVertically(1) && dyConsumed > 0 && child.visibility == VISIBLE) {
            // hide the FAB when scroll view reached its bottom
            child.hide(listener)
        } else if (dyConsumed < 0 && child.visibility != VISIBLE) {
            // show the FAB on scroll up
            child.show()
        }
    }

}
class-HideOnScrollFabBehavior(context:context?,attrs:AttributeSet?):FloatingActionButton.Behavior(){
//隐藏晶圆厂时,将可见性从“消失”更改为“不可见”,因为
//由于CoordinatorLayout.onStartNestedScroll()实现
//将不再调用StartNestedScroll上的子视图(此处为fab)
//因为它的可见性消失了
private val listener=object:FloatingActionButton.OnVisibilityChangedListener(){
覆盖隐藏的乐趣(fab:FloatingActionButton?){
fab?.visibility=不可见
}
}
开始嵌套滚动时覆盖乐趣(coordinatorLayout:coordinatorLayout,子项:FloatingActionButton,directTargetChild:View,target:View,axes:Int,type:Int):布尔值{
return axes==viewcompt.SCROLL\u AXIS\u VERTICAL//确保我们对垂直滚动做出反应
||super.onStartNestedScroll(坐标布局、子对象、directTargetChild、目标、轴、类型)
}
覆盖有趣的连接点(coordinatorLayout:coordinatorLayout,child:FloatingActionButton,target:View,dxConsumed:Int,dyConsumed:Int,dxUnconsumed:Int,dyUnconsumed:Int,type:Int,consumed:IntArray){
super.onNestedScroll(坐标布局、子项、目标、dxConsumed、dyConsumed、dxUnconsumed、dyUnconsumed、type、consumed)
如果(!target.canScrollVertical(1)&&dyConsumed>0&&child.visibility==可见){
//当滚动视图到达底部时隐藏晶圆厂
hide(侦听器)
}else if(dyconsumered<0&&child.visibility!=可见){
//向上滚动显示晶圆厂
child.show()
}
}
}
将此添加到FAB:


app:layout_behavior=“@string/hide_bottom_view_on_scroll_behavior”

Kotlin中的解决方案

recycler_view = findViewById(R.id.recycler_view)

        recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                if(dy > 0){
                    fab.hide();
                } else{
                    fab.show();
                }
                super.onScrolled(recyclerView, dx, dy)

            }
        })

您可能应该在项目中为FAB实现某种滚动行为,然后将FAB的layout_behavior属性设置为所需的自定义滚动行为<代码>应用程序:layout\u behavior=“com.example.scrollawrefabbehavior”。。。请告诉我您是否需要此功能的代码片段即使协调器布局中不存在fab?我知道您有fab的片段布局。。我建议你把工厂移到活动布局上。。。使用
layout_gravity
小心定位,例如
start | bottom
,因为协调器布局只是我的晶圆上的一个框架布局,所以我的晶圆在我的所有碎片中都会显示!!在当前布局安排中,晶圆厂位于框架布局内,而非协调器布局内…-您可以在将不同片段附加到活动时显示和隐藏它,因为它是一个view@Zahra.HY这个解决方案不符合您的期望吗?@behelit,这不是必需的。除非您已经附加了其他侦听器并希望删除它们。虽然此代码可能会解决问题,但如何以及为什么解决此问题将真正有助于提高您的帖子质量,并可能导致更多的追加投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。