Hide add contact button when scrolling down the contacts list

This commit is contained in:
Severiano Jaramillo 2018-07-11 16:51:15 -05:00
parent b3501a3a89
commit bd0bbfa802
2 changed files with 21 additions and 2 deletions

View file

@ -26,8 +26,6 @@ import android.view.View;
import android.view.animation.LinearInterpolator; import android.view.animation.LinearInterpolator;
import android.widget.ImageView; import android.widget.ImageView;
import com.sjaramillo10.animatedtablayout.AnimatedTabLayout;
import java.io.File; import java.io.File;
import butterknife.BindView; import butterknife.BindView;

View file

@ -7,6 +7,7 @@ import android.arch.paging.PagedList;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
@ -28,6 +29,8 @@ public class ContactsFragment extends Fragment {
ContactListAdapter adapter; ContactListAdapter adapter;
FloatingActionButton fabAddContact;
public ContactsFragment() { public ContactsFragment() {
// Required empty public constructor // Required empty public constructor
} }
@ -56,6 +59,24 @@ public class ContactsFragment extends Fragment {
adapter = new ContactListAdapter(); adapter = new ContactListAdapter();
rvContacts.setAdapter(adapter); rvContacts.setAdapter(adapter);
fabAddContact = getActivity().findViewById(R.id.fabAddContact);
// Hides the fab when scrolling down
rvContacts.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
// Scroll down
if(dy > 0 && fabAddContact.isShown())
fabAddContact.hide();
// Scroll up
if(dy < 0 && !fabAddContact.isShown())
fabAddContact.show();
}
});
// Gets contacts LiveData instance from ContactsViewModel // Gets contacts LiveData instance from ContactsViewModel
ContactListViewModel contactListViewModel = ContactListViewModel contactListViewModel =
ViewModelProviders.of(this).get(ContactListViewModel.class); ViewModelProviders.of(this).get(ContactListViewModel.class);