the situation is as follows.
Quick recap, the ExtendedFloatingActionButton is not reacting to the RecyclerView addOnScrollListener but the regular button is working just fine.
Detailed explanation with code:
I have a main activity that is split into two. On the left side is fragment1 and on the right side is fragment2. In fragment one, I have some data that populates recyclerView and ExtendedFloatingActionButton in the corner. On scroll, I want to hide/show the button (or shrink/extend it), and on button click I want to start some background logic. The problem is that the FAB is not reacting on scroll and something weird is going on when I click it ripple effect is activated, so the click is registered but the onClick function is not called. The funny thing is that when I put the regular button instead of FAB everything works as intended.
I have been stuck debugging this for more than a day now, but I just can't seem to find what the problem is.
I have a MainActivity that is split into two fragments (tablet app).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<include
android:id="@+id/frag1"
layout="@layout/frag_one"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
<include
android:id="@+id/frag2"
layout="@layout/frag_two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
Java code for the MainActivity
Frag1 frag1= new Frag1();
Frag2 frag2 = new Frag2();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frag1, frag1);
fragmentTransaction.replace(R.id.frag2, frag2 );
fragmentTransaction.commit();
Java code for Fragment1
extendedFloatingActionButton = view.findViewById(R.id.button);
//helper class that ads addOnScrollListener on RecyclerView
helperClass.scrollHelper(recyclerView, extendedFloatingActionButton);
extendedFloatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(requireContext(), "msg", Toast.LENGTH_SHORT).show();
}
});
XML code for Fragment1
<androidx.constraintlayout.widget.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"
android:background="@color/colorBackground"
tools:context=".fragments.Frag1">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/appBarLayout"/>
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimaryButton"
android:text="ADD"
android:textColor="@color/colorSecondaryButton"
app:icon="@drawable/icon_ok"
app:iconTint="@color/colorSecondaryButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:rippleColor="@color/colorAccent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I'll also include all the dependencies that i use just in case
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'androidx.cardview:cardview:1.0.0'
//material design
implementation 'com.google.android.material:material:1.10.0'
//scalable size unit (support for different screen size)
implementation "com.intuit.sdp:sdp-android:1.0.6"
implementation "com.intuit.ssp:ssp-android:1.0.6"
//room database
implementation "androidx.room:room-runtime:2.6.0"
annotationProcessor "androidx.room:room-compiler:2.6.0"
//preferences
implementation "androidx.preference:preference:1.2.0"
//jetpack navigation component ....
implementation "androidx.navigation:navigation-fragment:2.4.2"
implementation "androidx.navigation:navigation-ui:2.4.2"
implementation 'androidx.navigation:navigation-dynamic-features-fragment:2.4.2'
//livedata
implementation 'androidx.lifecycle:lifecycle-livedata:2.6.2'
//recycler view decoration on swipe
implementation 'it.xabaras.android:recyclerview-swipedecorator:1.4'
//Gson dependency
implementation 'com.google.code.gson:gson:2.9.0'
//barcode scanner
implementation 'com.journeyapps:zxing-android-embedded:4.2.0'
// volley http library
implementation 'com.android.volley:volley:1.2.1'
//facebook shimmer
implementation 'com.facebook.shimmer:shimmer:0.5.0@aar'
Scroll helper
public void scrollHelper(RecyclerView layoutView, ExtendedFloatingActionButton button) {
layoutView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
// Scrolling down - visibility or shrkink
button.setVisibility(View.INVISIBLE);
} else {
// Scrolling up
button.setVisibility(View.VISIBLE);
}
}
});
}
I'm open to implementing any suggestions you might have. Ignore my naming in the example above, since it's just an example.
Cheers.