How do I show my fab on scroll when the first item in the recycler view is not visible but hide when the...












3














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!










share|improve this question
























  • 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
















3














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!










share|improve this question
























  • 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














3












3








3







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!










share|improve this question















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 kotlin android-recyclerview floating-action-button recyclerview-layout






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 1 at 11:38









aminography

5,29521129




5,29521129










asked Nov 19 at 19:05









Marissa Nicholas

31642872




31642872












  • 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










  • 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












2 Answers
2






active

oldest

votes


















1





+50









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





share|improve this answer





















  • 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





















0














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






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1





+50









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





share|improve this answer





















  • 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


















1





+50









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





share|improve this answer





















  • 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
















1





+50







1





+50



1




+50




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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 15:16









aminography

5,29521129




5,29521129












  • 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






  • 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















0














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






share|improve this answer





















  • 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
















0














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






share|improve this answer





















  • 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














0












0








0






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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 19:56









Har Kal

597314




597314












  • 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










  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...