不同的实现在一个活动的每个片段浮动操作按钮 [英] Different implementation for a floating action button in each fragment of an activity

查看:485
本文介绍了不同的实现在一个活动的每个片段浮动操作按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于协调其中有5片在它的活动一个浮动操作按钮的行为问题。

在我的应用程序,我有拥有一ViewPager和FloatingActionButton定义的XML的活动。该ViewPager包含片段,每个片段里面有一个RecyclerView.On点击浮动的操作按钮,我已经实现scrollToPosition(0)。因为我已经在实施的主要活动的xml文件浮动ActionButton,scrollToPosition()只在点击浮动的行动最后一个片段button.How做我克服这一点,这样的点击任何片段的回收视图滚动到顶部浮动按钮?

我曾尝试实施片段布局文件floatingactionbutton和它的工作原理完全fine.But本是浮动的,当我从一个片段到另一个fragment.So刷卡操作按钮时便会移动的问题是有没有什么办法让我可以实现floatingactionbutton的主要活动的xml文件,仍然能够实现scrollToPosotion(0)上点击FloatingActionButton每一个片段?非常感谢您的任何建议。

下面是XML文件,其中包含浮动操作按钮。

 <?XML版本=1.0编码=UTF-8&GT?;
< android.support.design.widget.CoordinatorLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#f4f4f4
工具:上下文=MainActivity。>< android.support.design.widget.AppBarLayout
    机器人:ID =@ + ID / appbar
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:fitsSystemWindows =真正的>    < android.support.v7.widget.Toolbar
        机器人:ID =@ + ID / tb_main
        机器人:layout_width =match_parent
        机器人:layout_height =?ATTR / actionBarSize
        机器人:ATTR / colorPrimary背景=
        机器人:海拔=0dp
        机器人:fitsSystemWindows =真
        应用:layout_scrollFlags =滚动| enterAlways
        应用:popupTheme =@风格/ ThemeOverlay.AppCompat.Dark/>    < com.myapp.now.extras.SlidingTabLayout
        机器人:ID =@ + ID / sl_tab
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:fitsSystemWindows =真正的>    < /com.myapp.now.extras.SlidingTabLayout>< /android.support.design.widget.AppBarLayout>
< android.support.v4.view.ViewPager
    机器人:ID =@ + ID / vp_tab
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    />
    < android.support.design.widget.FloatingActionButton
    机器人:ID =@ + ID / FAB
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =底部|右|结束
    机器人:layout_marginBottom =@扪/ fab_margin_bottom
    机器人:layout_marginEnd =@扪/ fab_margin_right
    机器人:layout_marginRight =@扪/ fab_margin_right
    机器人:可点击=真
    机器人:SRC =@的mipmap / top_scroll
    应用:backgroundTint =@彩色/ colorFAB
    应用:边框宽度=0dp
    应用:fabSize =正常/>

下面是片段的XML文件

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto>
< android.support.v4.widget.SwipeRefreshLayout
    机器人:ID =@ + ID / srl_swipe
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>
    < android.support.v7.widget.RecyclerView
        机器人:ID =@ + ID / rv_list
        机器人:滚动条=垂直
        机器人:fadeScrollbars =真
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>< /android.support.v4.widget.SwipeRefreshLayout><进度
    机器人:知名度=水涨船高
    机器人:layout_gravity =中心
    机器人:ID =@ + ID / PBAR
    机器人:layout_width =40dp
    机器人:layout_height =40dp/>

以下是浮动的操作按钮在每个片段中实施

  =晶圆厂(android.support.design.widget.FloatingActionButton)view.findViewById(R.id.fab);
    fab.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){            LinearLayoutManager LLM =(LinearLayoutManager)mRecyclerView.getLayoutManager();
            llm.scrollToPositionWithOffset(0,0);
        }
    });


解决方案

不知道这是否会与SlidingTabLayout工作,因为我使用ViewPager。

有FAB 的onClick 调用一个方法(如 onFABClick())在你的活动,并将其定义为:

 公共无效onFABClick(查看视图){
    如果(fragments.get(mViewPager.getCurrentItem())的instanceof碎裂){
      如果(碎裂!= NULL){
        fragmentA.fabOnClick();
      }
    }否则如果(fragments.get(mViewPager.getCurrentItem())的instanceof FragmentB){
      如果(fragmentB!= NULL){
        fragmentB.fabOnClick();
      }
    }
  }

下面片段是持有片段对象数组。它是被传递到 ViewPager 的适配器的阵列。我想你不得不改变 mViewPager.getCurrentItem() SlidingTabLayout 的当前片段方法是等效的GET指数

最后, fabOnClick()方法在每个片段中实现的。 (你需要把所有的片段实现一个接口(例如FABActionInterface),并添加方法签名那里。

在这里,你基本上抓住FAB的onclick并指挥它呼吁当前片段的方法。

This is regarding the issue of coordinating the behaviour of a Floating Action Button in an Activity which has 5 fragments in it.

In my app, I have an Activity which houses a ViewPager and an xml defined with FloatingActionButton. The ViewPager contains Fragments, and inside each Fragment there is a RecyclerView.On clicking floating action button i have implemented scrollToPosition(0). As i have implemented Floating ActionButton in the xml file of main activity ,scrollToPosition() works only in the last fragment on clicking floating action button.How do i overcome this so that on clicking floating button in any fragment the recycler view scrolls to top?

I have tried implementing floatingactionbutton in fragment layout file and it works completely fine.But the problem with this is floating action button wil be moving when i swipe from one fragment to another fragment.So is there any way so that i can implement floatingactionbutton in the main activity xml file and still be able to achieve scrollToPosotion(0) in every fragment on clicking FloatingActionButton? Many thanks for any suggestions.

The following is the xml file which contains floating action button.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f4f4f4"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/tb_main"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="0dp"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />

    <com.myapp.now.extras.SlidingTabLayout
        android:id="@+id/sl_tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

    </com.myapp.now.extras.SlidingTabLayout>

</android.support.design.widget.AppBarLayout>


<android.support.v4.view.ViewPager
    android:id="@+id/vp_tab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />


    <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right|end"
    android:layout_marginBottom="@dimen/fab_margin_bottom"
    android:layout_marginEnd="@dimen/fab_margin_right"
    android:layout_marginRight="@dimen/fab_margin_right"
    android:clickable="true"
    android:src="@mipmap/top_scroll"
    app:backgroundTint="@color/colorFAB"
    app:borderWidth="0dp"
    app:fabSize="normal" />

The following is an xml file of a fragment

<FrameLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/srl_swipe"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_list"
        android:scrollbars="vertical"
        android:fadeScrollbars="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.v4.widget.SwipeRefreshLayout>

<ProgressBar
    android:visibility="gone"
    android:layout_gravity="center"
    android:id="@+id/pbar"
    android:layout_width="40dp"
    android:layout_height="40dp" />

The following is the implementation of floating action button in each fragment

 fab = (android.support.design.widget.FloatingActionButton) view.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LinearLayoutManager llm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
            llm.scrollToPositionWithOffset(0, 0);
        }
    });

解决方案

Not sure if this'll work with SlidingTabLayout since I use ViewPager.

Have the FAB onClick call a method (e.g. onFABClick() ) in your activity and define it as:

public void onFABClick(View view) {
    if (fragments.get(mViewPager.getCurrentItem()) instanceof FragmentA) {
      if (fragmentA!= null) {
        fragmentA.fabOnClick();
      }
    } else if (fragments.get(mViewPager.getCurrentItem()) instanceof FragmentB) {
      if (fragmentB!= null) {
        fragmentB.fabOnClick();
      }
    }
  }

Here fragments is the array that holds fragment objects. It is the array that is passed to the adapter of the ViewPager. I guess you'll have to change mViewPager.getCurrentItem() with the equivalent get index of current fragment method of SlidingTabLayout.

Lastly, the fabOnClick() method is implemented in each fragment. (You'll need to have all your fragments implement an interface (e.g. FABActionInterface) and add the method signature there.

Here you are basically catching the FAB onclick and directing it to call a method on the current fragment.

这篇关于不同的实现在一个活动的每个片段浮动操作按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆