Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access main activity FloatingActionButton from Fragments

I have a an activity with 3 fragments. Each fragment has to have a FAB with different icon an action. The FAB is defined on the main activity layout. Now I saw that I cannot access it from fragments. How can I do it?

Here's my code: main_activity layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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"
    tools:context=".MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        tools:layout_editor_absoluteX="16dp">


        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="fixed" />
    </android.support.v4.view.ViewPager>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="24dp"
        android:src="@android:drawable/ic_dialog_email"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

my fragment layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackgroundFragment">

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

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

</LinearLayout>

inflating the fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    v= inflater.inflate(R.layout.filters_fragment,container,false);

    myrecyclerview = (RecyclerView) v.findViewById(R.id.contact_recycleview);
    recyclerAdapter = new RecyclerViewAdapterCall(getContext(), lstCall);
    myrecyclerview.setLayoutManager(new LinearLayoutManager((getActivity())));
    myrecyclerview.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
    myrecyclerview.setAdapter(recyclerAdapter);

    return v;
}

so here I'll like to change icon an action, how?

like image 677
Paula Perez Avatar asked Jan 27 '23 06:01

Paula Perez


2 Answers

Step 1 create interface

  public interface FabButtonClick {
         void onFabClicked();
         void onFabClickedTwo();
    }

Step 2 : inside activity

 FabButtonClick fabButtonClick ;

inside `oncreate()` of activity

    fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


switch(mViewPager.getCurrentItem()){
case 0: 
fabButtonClick.onFabClicked()
break;
case 1: 
fabButtonClick.onFabClickedTwo()
break;
etc
}
                    
                }
            });

Inside of activity only

private void setListener(FabButtonClick interface){
    fabButtonClick =interface;
}


fragment implement FabButtonClick 

Inside fragments onViewCreated

((YOUR_ACTIVITY_NAME)getActivity()).setListener(this)

Overridden method from interface now let you handle fab clicks in Fragment

like image 150
akshay_shahane Avatar answered Feb 04 '23 02:02

akshay_shahane


Android developer guide recommend the following way to do Activity, Fragment communication.

Fragment access Activity by listener. Activity access Fragment by getSupportFragmentManager().getFragments()

So you have two ways to use the fab.

First, directly access the fab in Fragment

class MyFragment extends Fragment {
    MyFragmentListener listener;

    void myFunction() {
        FloatingActionButton fab = listener.getFab();
        ...
    }
}

interface MyFragmentListener {
    FloatingActionButton getFab();
}

class MyActivity extends AppCompatActivity implements MyFragmentListener {
    FloationActionButton fab;

    @Override
    FloationActionButton getFab() {
        return fab;
    }

    @Override
    void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        if(fragment instanceof MyFragment) {
            ((MyFragment) fragment).listener = this;
        }
    }
}

Second, Call Fragment's function when fab is clicked

//In Activity
fab.setOnClickListener(new OnClickListener() {
    for(fragment : getSupportFragmentManager().getFragments()) {
        if(fragment instanceof MyFragment) {
            ((MyFragment) fragment).someFunction();
        }
    }
});
like image 33
Jeffrey Chen Avatar answered Feb 04 '23 04:02

Jeffrey Chen