【问题标题】:Snackbar displaying on top of Fragment Fab buttonSnackbar 显示在 Fragment Fab 按钮的顶部
【发布时间】:2015-10-11 18:35:41
【问题描述】:

我在 Textview 和 viewpager 下面有活动。在视图寻呼机的一个片段中,我有一个 fab 按钮。当我在 Activity 的菜单点击上调用 Snackbar 时,Snackbar 会出现在 fab 按钮上。一切正常(fab 按钮被推到小吃店上),如果 fab 按钮处于活动状态,但我需要它片段。请让我知道我在哪里做错了

片段布局

<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:id="@+id/coordLayoutComments"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/mdtp_white"
   >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvComments"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabComments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="@dimen/btn_fab_margins"
        android:layout_marginRight="@dimen/btn_fab_margins"
        android:elevation="6dp"
        android:src="@drawable/ic_comment_24_5"
        app:borderWidth="0dp"
        app:fabSize="mini"
        app:pressedTranslationZ="12dp" />

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

活动布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="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:orientation="vertical"> <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <include
                android:id="@+id/newtoolbar"
                layout="@layout/toolbar" />

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?attr/actionBarSize"
            android:orientation="vertical">

            <android.support.v7.widget.CardView
                android:id="@+id/cv"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                card_view:cardCornerRadius="5dp"
                card_view:cardElevation="2dp"
                card_view:contentPadding="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="vertical">


                    <TextView
                        android:id="@+id/tvTitle"
                        fontPath="fonts/SourceSansPro-Regular.otf"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:ellipsize="end"                        
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/tvDesc"
                        fontPath="fonts/Roboto-Italic.ttf"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="5dp"
                        android:gravity="center_horizontal"                                         
                        android:textSize="15sp"
                        android:textStyle="normal|italic"
                        tools:ignore="MissingPrefix" />
                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/newtoolbar"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:scrollbars="horizontal" />

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        </LinearLayout>


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

</RelativeLayout>

【问题讨论】:

  • 您能发布您用来显示 Snackbar 的代码吗?此外,在活动布局中,您不需要拥有顶级相对布局。你可以只拥有 CoordinatorLayout。
  • Snackbarsnackbar = Snackbar.make(coordLayout, "没有可用的网络!请稍后再试", Snackbar.LENGTH_LONG);小吃吧.show();这里是Activity的“coordLayout”布局

标签: android android-fragments android-coordinatorlayout android-snackbar


【解决方案1】:

在您的 xml 布局中,有两个 CoordinatorLayouts。一个在活动布局中,另一个在片段布局中。现在 Snackbar.make() 函数的第一个参数是这个 Snackbar 将被放置的父 Coordinator Layout,并且这个 Coordinator Layout 的所有元素都会受到 Sanckbar 的运动/视图变化的影响。尝试使用 Fragment Layout 的 Coordinator Layout 作为 Snackbar 的父级。

【讨论】:

  • 感谢您的回复,如果我只有一个片段,它会正常工作,但因为我有一个带有多个片段的视图寻呼机。单击 Activity 的菜单时,视图寻呼机可能会显示任何片段,不一定是带有 fab 按钮片段的片段。如何处理。
  • @souser:看看herehere。上面的链接将帮助您检索当前在 viewpager 中可见的片段。如果该片段是包含 FAB 的片段,则创建带有片段 CoordinatorLayout 作为父级的小吃吧,否则创建带有活动 CoordinatorLayout 作为父级的小吃吧。希望这会有所帮助
【解决方案2】:

活动代码

public class HomeActivity extends AppCompatActivity
        implements FragmentListener {

    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HomeActivity.this.showSnackbar(view, "Replace with your own action");
            }
        });
    }

    @Override
    public void showSnackBar(View view, String message) {
        Snackbar.make(view,message,Snackbar.LENGTH_LONG).show();
    }
}

片段监听代码

public interface FragmentListener {
    void showSnackBar(View view,String message);    
}

片段代码

public class MyFragment extends Fragment {

    private static final String TAG = MyFragment.class.getSimpleName();

    private FragmentListener fragmentListener;
    private FloatingActionButton floatingActionButton;

    public MyFragment() {
        // Required empty public constructor
    }

    public static MyFragment getInstance(String uniqueId) {
        MyFragment MyFragment = new MyFragment();
        return MyFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_surveys, container, false);
        floatingActionButton = view.findViewById(R.id.fab);

         floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fragmentListner.showSnackBar(floatingActionButton,"hello");
            }
        });
        return view;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            fragmentListener = (FragmentListener) context;
        } catch (Exception e) {
            throw new ClassCastException(" must implement OnListItemSelectedListener");
        }
    }    

}

要使用 FAB 在 Activity 和 Fragment 中显示 Snackbar,您可以使用这种简单的方法。

  • Activity 和 Fragment 父 View 应该是 CoordinatorLayout。

  • 创建FragmentListener并在Activity中实现

【讨论】:

    猜你喜欢
    • 2016-04-18
    • 2021-09-27
    • 1970-01-01
    • 2019-08-03
    • 2015-10-23
    • 2018-04-23
    • 2021-03-15
    • 2016-01-31
    • 2015-10-23
    相关资源
    最近更新 更多