How do I show my fab on scroll when the first item in the recycler view is not visible but hide when the...
up vote
3
down vote
favorite
So I am trying to implement a FAB button such that, when I scroll my recycler view and the first item is not visible (scrolled up etc) , fab button should be visible, else it should be hidden if the first position is showing. Right now I have implemented the code, but it doesn't show the fab button at all, just wanted to know what I am doing wrong?
my xml code is below:
<Linearlayout ....
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pullToRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/emailFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/myRecyclerView"
android:layout_gravity="bottom|end"
app:layout_behavior="com.example.fab.ScrollAwareFABBehavior"
app:layout_anchorGravity="bottom|end"
/>
</FrameLayout>
</LinearLayout>
my Kotlin code is below:
val positionView = (myRecyclerView.getLayoutManager() as LinearLayoutManager).findFirstVisibleItemPosition()
myRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy >0 && positionView > 0) {
emailFab.show();
} else {
emailFab.hide();
}
}
Any ideas how to go about it?
Thanks!
android android-recyclerview android-scrollview floating-action-button recyclerview-layout
add a comment |
up vote
3
down vote
favorite
So I am trying to implement a FAB button such that, when I scroll my recycler view and the first item is not visible (scrolled up etc) , fab button should be visible, else it should be hidden if the first position is showing. Right now I have implemented the code, but it doesn't show the fab button at all, just wanted to know what I am doing wrong?
my xml code is below:
<Linearlayout ....
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pullToRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/emailFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/myRecyclerView"
android:layout_gravity="bottom|end"
app:layout_behavior="com.example.fab.ScrollAwareFABBehavior"
app:layout_anchorGravity="bottom|end"
/>
</FrameLayout>
</LinearLayout>
my Kotlin code is below:
val positionView = (myRecyclerView.getLayoutManager() as LinearLayoutManager).findFirstVisibleItemPosition()
myRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy >0 && positionView > 0) {
emailFab.show();
} else {
emailFab.hide();
}
}
Any ideas how to go about it?
Thanks!
android android-recyclerview android-scrollview floating-action-button recyclerview-layout
Is it FAB in every RecyclerView child or one FAB in activity? (as e.g in Gmail app send button)
– Domin
Nov 19 at 19:10
r u for real ;) well u can make a simple interface for that and track the movement of recyclerview and track if the first child is visible using the linearlayout manager wait leme give you somme code
– Har Kal
Nov 19 at 19:12
@Domin let me update the code to show how I am using fab in xml etc,. hope that'll help
– Marissa Nicholas
Nov 19 at 19:19
updated the code with xml , all there is to it
– Marissa Nicholas
Nov 19 at 19:21
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
So I am trying to implement a FAB button such that, when I scroll my recycler view and the first item is not visible (scrolled up etc) , fab button should be visible, else it should be hidden if the first position is showing. Right now I have implemented the code, but it doesn't show the fab button at all, just wanted to know what I am doing wrong?
my xml code is below:
<Linearlayout ....
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pullToRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/emailFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/myRecyclerView"
android:layout_gravity="bottom|end"
app:layout_behavior="com.example.fab.ScrollAwareFABBehavior"
app:layout_anchorGravity="bottom|end"
/>
</FrameLayout>
</LinearLayout>
my Kotlin code is below:
val positionView = (myRecyclerView.getLayoutManager() as LinearLayoutManager).findFirstVisibleItemPosition()
myRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy >0 && positionView > 0) {
emailFab.show();
} else {
emailFab.hide();
}
}
Any ideas how to go about it?
Thanks!
android android-recyclerview android-scrollview floating-action-button recyclerview-layout
So I am trying to implement a FAB button such that, when I scroll my recycler view and the first item is not visible (scrolled up etc) , fab button should be visible, else it should be hidden if the first position is showing. Right now I have implemented the code, but it doesn't show the fab button at all, just wanted to know what I am doing wrong?
my xml code is below:
<Linearlayout ....
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pullToRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/emailFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/myRecyclerView"
android:layout_gravity="bottom|end"
app:layout_behavior="com.example.fab.ScrollAwareFABBehavior"
app:layout_anchorGravity="bottom|end"
/>
</FrameLayout>
</LinearLayout>
my Kotlin code is below:
val positionView = (myRecyclerView.getLayoutManager() as LinearLayoutManager).findFirstVisibleItemPosition()
myRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy >0 && positionView > 0) {
emailFab.show();
} else {
emailFab.hide();
}
}
Any ideas how to go about it?
Thanks!
android android-recyclerview android-scrollview floating-action-button recyclerview-layout
android android-recyclerview android-scrollview floating-action-button recyclerview-layout
edited Nov 19 at 19:21
asked Nov 19 at 19:05
Marissa Nicholas
33242872
33242872
Is it FAB in every RecyclerView child or one FAB in activity? (as e.g in Gmail app send button)
– Domin
Nov 19 at 19:10
r u for real ;) well u can make a simple interface for that and track the movement of recyclerview and track if the first child is visible using the linearlayout manager wait leme give you somme code
– Har Kal
Nov 19 at 19:12
@Domin let me update the code to show how I am using fab in xml etc,. hope that'll help
– Marissa Nicholas
Nov 19 at 19:19
updated the code with xml , all there is to it
– Marissa Nicholas
Nov 19 at 19:21
add a comment |
Is it FAB in every RecyclerView child or one FAB in activity? (as e.g in Gmail app send button)
– Domin
Nov 19 at 19:10
r u for real ;) well u can make a simple interface for that and track the movement of recyclerview and track if the first child is visible using the linearlayout manager wait leme give you somme code
– Har Kal
Nov 19 at 19:12
@Domin let me update the code to show how I am using fab in xml etc,. hope that'll help
– Marissa Nicholas
Nov 19 at 19:19
updated the code with xml , all there is to it
– Marissa Nicholas
Nov 19 at 19:21
Is it FAB in every RecyclerView child or one FAB in activity? (as e.g in Gmail app send button)
– Domin
Nov 19 at 19:10
Is it FAB in every RecyclerView child or one FAB in activity? (as e.g in Gmail app send button)
– Domin
Nov 19 at 19:10
r u for real ;) well u can make a simple interface for that and track the movement of recyclerview and track if the first child is visible using the linearlayout manager wait leme give you somme code
– Har Kal
Nov 19 at 19:12
r u for real ;) well u can make a simple interface for that and track the movement of recyclerview and track if the first child is visible using the linearlayout manager wait leme give you somme code
– Har Kal
Nov 19 at 19:12
@Domin let me update the code to show how I am using fab in xml etc,. hope that'll help
– Marissa Nicholas
Nov 19 at 19:19
@Domin let me update the code to show how I am using fab in xml etc,. hope that'll help
– Marissa Nicholas
Nov 19 at 19:19
updated the code with xml , all there is to it
– Marissa Nicholas
Nov 19 at 19:21
updated the code with xml , all there is to it
– Marissa Nicholas
Nov 19 at 19:21
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
I think the problem is that you should pull the findFirstVisibleItemPosition()
into the OnScrollListener
to find visible position in scroll changes:
myRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val positionView = (myRecyclerView.getLayoutManager() as LinearLayoutManager).findFirstVisibleItemPosition()
if (positionView > 0) {
if(!emailFab.isShown) {
emailFab.show();
}
} else {
if(emailFab.isShown) {
emailFab.hide();
}
}
}
})
This is exactly the solution I was looking for, easy and works perfectly! Thanks @aminography!
– Marissa Nicholas
Nov 23 at 17:22
1
You're welcome :-) I'm very glad to see the problem is solved.
– aminography
Nov 23 at 18:07
add a comment |
up vote
0
down vote
this code is 100% working
public class TestActivity extends AppCompatActivity {
private static final String TAG = "TestActivity";
private ArrayList<String> datas = new ArrayList<>();
private FloatingActionButton fabButton;
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private DataAdapter dataAdapter;
private boolean isFabVisible = false;
private OnFabVisibilityCallback onFabVisibilityCallback;
public void setOnFabVisibilityCallback(OnFabVisibilityCallback onFabVisibilityCallback) {
this.onFabVisibilityCallback = onFabVisibilityCallback;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_contacts);
datas.add("one");
datas.add("two");
datas.add("three");
datas.add("four");
datas.add("five");
datas.add("six");
datas.add("seven");
datas.add("eight");
datas.add("nine");
datas.add("ten");
fabButton = (FloatingActionButton)findViewById(R.id.fabButton);
fabButton.setVisibility(View.GONE);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
dataAdapter = new DataAdapter(this, datas);
recyclerView.setAdapter(dataAdapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(linearLayoutManager.findFirstVisibleItemPosition() == 0){
//hide
if(onFabVisibilityCallback != null){
onFabVisibilityCallback.onChange(false);
}
}else{
//show
if(onFabVisibilityCallback != null){
onFabVisibilityCallback.onChange(true);
}
}
}
});
setOnFabVisibilityCallback(new OnFabVisibilityCallback() {
@Override
public void onChange(boolean isVisible) {
if(isVisible){
if(!isFabVisible){
isFabVisible = true;
fabButton.setVisibility(View.VISIBLE);
}
}else{
if(isFabVisible){
isFabVisible = false;
fabButton.setVisibility(View.GONE);
}
}
}
});
}
private class DataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private Activity activity;
private ArrayList<String> datas;
private LayoutInflater layoutInflater;
public DataAdapter(Activity activity, ArrayList<String> datas) {
this.activity = activity;
this.datas = datas;
this.layoutInflater = LayoutInflater.from(activity);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new SimpleViewHolder(layoutInflater.inflate(R.layout.unit_test, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((SimpleViewHolder)holder).init(datas.get(position));
}
@Override
public int getItemCount() {
return datas.size();
}
private class SimpleViewHolder extends RecyclerView.ViewHolder{
private TextView text;
public SimpleViewHolder(View itemView) {
super(itemView);
initViews(itemView);
}
private void initViews(View v){
text = (TextView)v.findViewById(R.id.text);
}
public void init(String data){
text.setText(data);
}
}
}
public interface OnFabVisibilityCallback{
void onChange(boolean isVisible);
}
}
the activity layout files
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="@drawable/ic_add_black_24dp"
android:tint="@color/white" />
</RelativeLayout>
this is recyclerview child layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:gravity="center"
android:background="@color/yellow"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">
<TextView
android:text="data"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
if some problem occurs leme know :)
happy coding
is there no easier way to do this? this seems to be unnecessarily complex. Is there an easier way to detect if the first row is visible without having to create an interface for change fallback and then using that as a callback and then using that callback within the scroll listener?
– Marissa Nicholas
Nov 19 at 20:25
well this is just for the sake of demonstration you can take another approach in which you can create your custom recyclerview which gives you an overridden method to set FAB visibility in that case code will be divided into files and will be lot easier but the basic remains the same you will have to listen for the scroll change in linearlayoutmanager and set an interface to call the method. hope you undertand that
– Har Kal
Nov 19 at 20:33
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I think the problem is that you should pull the findFirstVisibleItemPosition()
into the OnScrollListener
to find visible position in scroll changes:
myRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val positionView = (myRecyclerView.getLayoutManager() as LinearLayoutManager).findFirstVisibleItemPosition()
if (positionView > 0) {
if(!emailFab.isShown) {
emailFab.show();
}
} else {
if(emailFab.isShown) {
emailFab.hide();
}
}
}
})
This is exactly the solution I was looking for, easy and works perfectly! Thanks @aminography!
– Marissa Nicholas
Nov 23 at 17:22
1
You're welcome :-) I'm very glad to see the problem is solved.
– aminography
Nov 23 at 18:07
add a comment |
up vote
1
down vote
accepted
I think the problem is that you should pull the findFirstVisibleItemPosition()
into the OnScrollListener
to find visible position in scroll changes:
myRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val positionView = (myRecyclerView.getLayoutManager() as LinearLayoutManager).findFirstVisibleItemPosition()
if (positionView > 0) {
if(!emailFab.isShown) {
emailFab.show();
}
} else {
if(emailFab.isShown) {
emailFab.hide();
}
}
}
})
This is exactly the solution I was looking for, easy and works perfectly! Thanks @aminography!
– Marissa Nicholas
Nov 23 at 17:22
1
You're welcome :-) I'm very glad to see the problem is solved.
– aminography
Nov 23 at 18:07
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I think the problem is that you should pull the findFirstVisibleItemPosition()
into the OnScrollListener
to find visible position in scroll changes:
myRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val positionView = (myRecyclerView.getLayoutManager() as LinearLayoutManager).findFirstVisibleItemPosition()
if (positionView > 0) {
if(!emailFab.isShown) {
emailFab.show();
}
} else {
if(emailFab.isShown) {
emailFab.hide();
}
}
}
})
I think the problem is that you should pull the findFirstVisibleItemPosition()
into the OnScrollListener
to find visible position in scroll changes:
myRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val positionView = (myRecyclerView.getLayoutManager() as LinearLayoutManager).findFirstVisibleItemPosition()
if (positionView > 0) {
if(!emailFab.isShown) {
emailFab.show();
}
} else {
if(emailFab.isShown) {
emailFab.hide();
}
}
}
})
answered Nov 22 at 15:16
aminography
3,83821129
3,83821129
This is exactly the solution I was looking for, easy and works perfectly! Thanks @aminography!
– Marissa Nicholas
Nov 23 at 17:22
1
You're welcome :-) I'm very glad to see the problem is solved.
– aminography
Nov 23 at 18:07
add a comment |
This is exactly the solution I was looking for, easy and works perfectly! Thanks @aminography!
– Marissa Nicholas
Nov 23 at 17:22
1
You're welcome :-) I'm very glad to see the problem is solved.
– aminography
Nov 23 at 18:07
This is exactly the solution I was looking for, easy and works perfectly! Thanks @aminography!
– Marissa Nicholas
Nov 23 at 17:22
This is exactly the solution I was looking for, easy and works perfectly! Thanks @aminography!
– Marissa Nicholas
Nov 23 at 17:22
1
1
You're welcome :-) I'm very glad to see the problem is solved.
– aminography
Nov 23 at 18:07
You're welcome :-) I'm very glad to see the problem is solved.
– aminography
Nov 23 at 18:07
add a comment |
up vote
0
down vote
this code is 100% working
public class TestActivity extends AppCompatActivity {
private static final String TAG = "TestActivity";
private ArrayList<String> datas = new ArrayList<>();
private FloatingActionButton fabButton;
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private DataAdapter dataAdapter;
private boolean isFabVisible = false;
private OnFabVisibilityCallback onFabVisibilityCallback;
public void setOnFabVisibilityCallback(OnFabVisibilityCallback onFabVisibilityCallback) {
this.onFabVisibilityCallback = onFabVisibilityCallback;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_contacts);
datas.add("one");
datas.add("two");
datas.add("three");
datas.add("four");
datas.add("five");
datas.add("six");
datas.add("seven");
datas.add("eight");
datas.add("nine");
datas.add("ten");
fabButton = (FloatingActionButton)findViewById(R.id.fabButton);
fabButton.setVisibility(View.GONE);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
dataAdapter = new DataAdapter(this, datas);
recyclerView.setAdapter(dataAdapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(linearLayoutManager.findFirstVisibleItemPosition() == 0){
//hide
if(onFabVisibilityCallback != null){
onFabVisibilityCallback.onChange(false);
}
}else{
//show
if(onFabVisibilityCallback != null){
onFabVisibilityCallback.onChange(true);
}
}
}
});
setOnFabVisibilityCallback(new OnFabVisibilityCallback() {
@Override
public void onChange(boolean isVisible) {
if(isVisible){
if(!isFabVisible){
isFabVisible = true;
fabButton.setVisibility(View.VISIBLE);
}
}else{
if(isFabVisible){
isFabVisible = false;
fabButton.setVisibility(View.GONE);
}
}
}
});
}
private class DataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private Activity activity;
private ArrayList<String> datas;
private LayoutInflater layoutInflater;
public DataAdapter(Activity activity, ArrayList<String> datas) {
this.activity = activity;
this.datas = datas;
this.layoutInflater = LayoutInflater.from(activity);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new SimpleViewHolder(layoutInflater.inflate(R.layout.unit_test, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((SimpleViewHolder)holder).init(datas.get(position));
}
@Override
public int getItemCount() {
return datas.size();
}
private class SimpleViewHolder extends RecyclerView.ViewHolder{
private TextView text;
public SimpleViewHolder(View itemView) {
super(itemView);
initViews(itemView);
}
private void initViews(View v){
text = (TextView)v.findViewById(R.id.text);
}
public void init(String data){
text.setText(data);
}
}
}
public interface OnFabVisibilityCallback{
void onChange(boolean isVisible);
}
}
the activity layout files
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="@drawable/ic_add_black_24dp"
android:tint="@color/white" />
</RelativeLayout>
this is recyclerview child layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:gravity="center"
android:background="@color/yellow"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">
<TextView
android:text="data"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
if some problem occurs leme know :)
happy coding
is there no easier way to do this? this seems to be unnecessarily complex. Is there an easier way to detect if the first row is visible without having to create an interface for change fallback and then using that as a callback and then using that callback within the scroll listener?
– Marissa Nicholas
Nov 19 at 20:25
well this is just for the sake of demonstration you can take another approach in which you can create your custom recyclerview which gives you an overridden method to set FAB visibility in that case code will be divided into files and will be lot easier but the basic remains the same you will have to listen for the scroll change in linearlayoutmanager and set an interface to call the method. hope you undertand that
– Har Kal
Nov 19 at 20:33
add a comment |
up vote
0
down vote
this code is 100% working
public class TestActivity extends AppCompatActivity {
private static final String TAG = "TestActivity";
private ArrayList<String> datas = new ArrayList<>();
private FloatingActionButton fabButton;
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private DataAdapter dataAdapter;
private boolean isFabVisible = false;
private OnFabVisibilityCallback onFabVisibilityCallback;
public void setOnFabVisibilityCallback(OnFabVisibilityCallback onFabVisibilityCallback) {
this.onFabVisibilityCallback = onFabVisibilityCallback;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_contacts);
datas.add("one");
datas.add("two");
datas.add("three");
datas.add("four");
datas.add("five");
datas.add("six");
datas.add("seven");
datas.add("eight");
datas.add("nine");
datas.add("ten");
fabButton = (FloatingActionButton)findViewById(R.id.fabButton);
fabButton.setVisibility(View.GONE);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
dataAdapter = new DataAdapter(this, datas);
recyclerView.setAdapter(dataAdapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(linearLayoutManager.findFirstVisibleItemPosition() == 0){
//hide
if(onFabVisibilityCallback != null){
onFabVisibilityCallback.onChange(false);
}
}else{
//show
if(onFabVisibilityCallback != null){
onFabVisibilityCallback.onChange(true);
}
}
}
});
setOnFabVisibilityCallback(new OnFabVisibilityCallback() {
@Override
public void onChange(boolean isVisible) {
if(isVisible){
if(!isFabVisible){
isFabVisible = true;
fabButton.setVisibility(View.VISIBLE);
}
}else{
if(isFabVisible){
isFabVisible = false;
fabButton.setVisibility(View.GONE);
}
}
}
});
}
private class DataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private Activity activity;
private ArrayList<String> datas;
private LayoutInflater layoutInflater;
public DataAdapter(Activity activity, ArrayList<String> datas) {
this.activity = activity;
this.datas = datas;
this.layoutInflater = LayoutInflater.from(activity);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new SimpleViewHolder(layoutInflater.inflate(R.layout.unit_test, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((SimpleViewHolder)holder).init(datas.get(position));
}
@Override
public int getItemCount() {
return datas.size();
}
private class SimpleViewHolder extends RecyclerView.ViewHolder{
private TextView text;
public SimpleViewHolder(View itemView) {
super(itemView);
initViews(itemView);
}
private void initViews(View v){
text = (TextView)v.findViewById(R.id.text);
}
public void init(String data){
text.setText(data);
}
}
}
public interface OnFabVisibilityCallback{
void onChange(boolean isVisible);
}
}
the activity layout files
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="@drawable/ic_add_black_24dp"
android:tint="@color/white" />
</RelativeLayout>
this is recyclerview child layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:gravity="center"
android:background="@color/yellow"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">
<TextView
android:text="data"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
if some problem occurs leme know :)
happy coding
is there no easier way to do this? this seems to be unnecessarily complex. Is there an easier way to detect if the first row is visible without having to create an interface for change fallback and then using that as a callback and then using that callback within the scroll listener?
– Marissa Nicholas
Nov 19 at 20:25
well this is just for the sake of demonstration you can take another approach in which you can create your custom recyclerview which gives you an overridden method to set FAB visibility in that case code will be divided into files and will be lot easier but the basic remains the same you will have to listen for the scroll change in linearlayoutmanager and set an interface to call the method. hope you undertand that
– Har Kal
Nov 19 at 20:33
add a comment |
up vote
0
down vote
up vote
0
down vote
this code is 100% working
public class TestActivity extends AppCompatActivity {
private static final String TAG = "TestActivity";
private ArrayList<String> datas = new ArrayList<>();
private FloatingActionButton fabButton;
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private DataAdapter dataAdapter;
private boolean isFabVisible = false;
private OnFabVisibilityCallback onFabVisibilityCallback;
public void setOnFabVisibilityCallback(OnFabVisibilityCallback onFabVisibilityCallback) {
this.onFabVisibilityCallback = onFabVisibilityCallback;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_contacts);
datas.add("one");
datas.add("two");
datas.add("three");
datas.add("four");
datas.add("five");
datas.add("six");
datas.add("seven");
datas.add("eight");
datas.add("nine");
datas.add("ten");
fabButton = (FloatingActionButton)findViewById(R.id.fabButton);
fabButton.setVisibility(View.GONE);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
dataAdapter = new DataAdapter(this, datas);
recyclerView.setAdapter(dataAdapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(linearLayoutManager.findFirstVisibleItemPosition() == 0){
//hide
if(onFabVisibilityCallback != null){
onFabVisibilityCallback.onChange(false);
}
}else{
//show
if(onFabVisibilityCallback != null){
onFabVisibilityCallback.onChange(true);
}
}
}
});
setOnFabVisibilityCallback(new OnFabVisibilityCallback() {
@Override
public void onChange(boolean isVisible) {
if(isVisible){
if(!isFabVisible){
isFabVisible = true;
fabButton.setVisibility(View.VISIBLE);
}
}else{
if(isFabVisible){
isFabVisible = false;
fabButton.setVisibility(View.GONE);
}
}
}
});
}
private class DataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private Activity activity;
private ArrayList<String> datas;
private LayoutInflater layoutInflater;
public DataAdapter(Activity activity, ArrayList<String> datas) {
this.activity = activity;
this.datas = datas;
this.layoutInflater = LayoutInflater.from(activity);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new SimpleViewHolder(layoutInflater.inflate(R.layout.unit_test, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((SimpleViewHolder)holder).init(datas.get(position));
}
@Override
public int getItemCount() {
return datas.size();
}
private class SimpleViewHolder extends RecyclerView.ViewHolder{
private TextView text;
public SimpleViewHolder(View itemView) {
super(itemView);
initViews(itemView);
}
private void initViews(View v){
text = (TextView)v.findViewById(R.id.text);
}
public void init(String data){
text.setText(data);
}
}
}
public interface OnFabVisibilityCallback{
void onChange(boolean isVisible);
}
}
the activity layout files
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="@drawable/ic_add_black_24dp"
android:tint="@color/white" />
</RelativeLayout>
this is recyclerview child layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:gravity="center"
android:background="@color/yellow"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">
<TextView
android:text="data"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
if some problem occurs leme know :)
happy coding
this code is 100% working
public class TestActivity extends AppCompatActivity {
private static final String TAG = "TestActivity";
private ArrayList<String> datas = new ArrayList<>();
private FloatingActionButton fabButton;
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private DataAdapter dataAdapter;
private boolean isFabVisible = false;
private OnFabVisibilityCallback onFabVisibilityCallback;
public void setOnFabVisibilityCallback(OnFabVisibilityCallback onFabVisibilityCallback) {
this.onFabVisibilityCallback = onFabVisibilityCallback;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_contacts);
datas.add("one");
datas.add("two");
datas.add("three");
datas.add("four");
datas.add("five");
datas.add("six");
datas.add("seven");
datas.add("eight");
datas.add("nine");
datas.add("ten");
fabButton = (FloatingActionButton)findViewById(R.id.fabButton);
fabButton.setVisibility(View.GONE);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
dataAdapter = new DataAdapter(this, datas);
recyclerView.setAdapter(dataAdapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(linearLayoutManager.findFirstVisibleItemPosition() == 0){
//hide
if(onFabVisibilityCallback != null){
onFabVisibilityCallback.onChange(false);
}
}else{
//show
if(onFabVisibilityCallback != null){
onFabVisibilityCallback.onChange(true);
}
}
}
});
setOnFabVisibilityCallback(new OnFabVisibilityCallback() {
@Override
public void onChange(boolean isVisible) {
if(isVisible){
if(!isFabVisible){
isFabVisible = true;
fabButton.setVisibility(View.VISIBLE);
}
}else{
if(isFabVisible){
isFabVisible = false;
fabButton.setVisibility(View.GONE);
}
}
}
});
}
private class DataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private Activity activity;
private ArrayList<String> datas;
private LayoutInflater layoutInflater;
public DataAdapter(Activity activity, ArrayList<String> datas) {
this.activity = activity;
this.datas = datas;
this.layoutInflater = LayoutInflater.from(activity);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new SimpleViewHolder(layoutInflater.inflate(R.layout.unit_test, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((SimpleViewHolder)holder).init(datas.get(position));
}
@Override
public int getItemCount() {
return datas.size();
}
private class SimpleViewHolder extends RecyclerView.ViewHolder{
private TextView text;
public SimpleViewHolder(View itemView) {
super(itemView);
initViews(itemView);
}
private void initViews(View v){
text = (TextView)v.findViewById(R.id.text);
}
public void init(String data){
text.setText(data);
}
}
}
public interface OnFabVisibilityCallback{
void onChange(boolean isVisible);
}
}
the activity layout files
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="@drawable/ic_add_black_24dp"
android:tint="@color/white" />
</RelativeLayout>
this is recyclerview child layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:gravity="center"
android:background="@color/yellow"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">
<TextView
android:text="data"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
if some problem occurs leme know :)
happy coding
answered Nov 19 at 19:56
Har Kal
590212
590212
is there no easier way to do this? this seems to be unnecessarily complex. Is there an easier way to detect if the first row is visible without having to create an interface for change fallback and then using that as a callback and then using that callback within the scroll listener?
– Marissa Nicholas
Nov 19 at 20:25
well this is just for the sake of demonstration you can take another approach in which you can create your custom recyclerview which gives you an overridden method to set FAB visibility in that case code will be divided into files and will be lot easier but the basic remains the same you will have to listen for the scroll change in linearlayoutmanager and set an interface to call the method. hope you undertand that
– Har Kal
Nov 19 at 20:33
add a comment |
is there no easier way to do this? this seems to be unnecessarily complex. Is there an easier way to detect if the first row is visible without having to create an interface for change fallback and then using that as a callback and then using that callback within the scroll listener?
– Marissa Nicholas
Nov 19 at 20:25
well this is just for the sake of demonstration you can take another approach in which you can create your custom recyclerview which gives you an overridden method to set FAB visibility in that case code will be divided into files and will be lot easier but the basic remains the same you will have to listen for the scroll change in linearlayoutmanager and set an interface to call the method. hope you undertand that
– Har Kal
Nov 19 at 20:33
is there no easier way to do this? this seems to be unnecessarily complex. Is there an easier way to detect if the first row is visible without having to create an interface for change fallback and then using that as a callback and then using that callback within the scroll listener?
– Marissa Nicholas
Nov 19 at 20:25
is there no easier way to do this? this seems to be unnecessarily complex. Is there an easier way to detect if the first row is visible without having to create an interface for change fallback and then using that as a callback and then using that callback within the scroll listener?
– Marissa Nicholas
Nov 19 at 20:25
well this is just for the sake of demonstration you can take another approach in which you can create your custom recyclerview which gives you an overridden method to set FAB visibility in that case code will be divided into files and will be lot easier but the basic remains the same you will have to listen for the scroll change in linearlayoutmanager and set an interface to call the method. hope you undertand that
– Har Kal
Nov 19 at 20:33
well this is just for the sake of demonstration you can take another approach in which you can create your custom recyclerview which gives you an overridden method to set FAB visibility in that case code will be divided into files and will be lot easier but the basic remains the same you will have to listen for the scroll change in linearlayoutmanager and set an interface to call the method. hope you undertand that
– Har Kal
Nov 19 at 20:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381083%2fhow-do-i-show-my-fab-on-scroll-when-the-first-item-in-the-recycler-view-is-not-v%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is it FAB in every RecyclerView child or one FAB in activity? (as e.g in Gmail app send button)
– Domin
Nov 19 at 19:10
r u for real ;) well u can make a simple interface for that and track the movement of recyclerview and track if the first child is visible using the linearlayout manager wait leme give you somme code
– Har Kal
Nov 19 at 19:12
@Domin let me update the code to show how I am using fab in xml etc,. hope that'll help
– Marissa Nicholas
Nov 19 at 19:19
updated the code with xml , all there is to it
– Marissa Nicholas
Nov 19 at 19:21