Snackbar显示在Fragment Fab按钮顶部

时间:2015-10-11 18:35:42

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

我在Textview和viewpager下面有活动。在view pager的一个片段中,我有一个fab按钮。当我在菜单上点击活动时调用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>

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

活动代码

public class HomeActivity extends AppCompatActivity
    implements  FragmentListner {

private LoadinDialog loadinDialog;

@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();
}
}

FragmentListner代码

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

片段代码

public class MyFragment extends Fragment {


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

private FragmentListner fragmentListner;
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 {
        fragmentListner = (FragmentListner) context;
    } catch (Exception e) {
        throw new ClassCastException(" must implement OnListItemSelectedListener");
    }
}    

}

要使用FAB在活动和片段中显示小吃栏,可以使用此简单方法。

  • “活动和片段”父视图应为CoordinatorLayout。

  • 创建FragmentListner并在Activity中实现