How can I change background image while scrolling the horizontal recyclerview in android












0














I want to change the background image of my layout on the basis of change of [ recyclerview. When I scroll recyclerview then on change state I want to change the background image of my layout smoothly like https://www.pinterest.com/pin/860891284992684260/



Here is my code:



//Updated Code



 @Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

int firstPos = HorizontalLayout.findFirstVisibleItemPosition();
int lastPos = HorizontalLayout.findLastVisibleItemPosition();
int middle = Math.abs(lastPos - firstPos) / 2 + firstPos;
View centerView = snapHelper.findSnapView(HorizontalLayout);
final int pos = HorizontalLayout.getPosition(centerView);
AllStories myStory = Stories.get(HorizontalLayout.getPosition(centerView));
// int selectedPos = -1;
for (int i = 0; i < RecyclerViewHorizontalAdapter.getItemCount(); i++) {
if (i == middle) {
if (myStory != null) {
if(dx>5)
{ background.setBackgroundResource(myStory.BackGroundImage);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);

AnimationSet animation = new AnimationSet(false); //change to false
animation.addAnimation(fadeOut);
background.setAnimation(animation);
}
background.setBackgroundResource(myStory.BackGroundImage);
//selectedPos = i;
LockStories(pos);
}
} else {

}
}



}
});









share|improve this question
























  • Do you have a link to your project so that I can try help?
    – Hudi Ilfeld
    Nov 20 at 11:19










  • Actually problem is that my background image is changing when i scroll the RecyclerView but i want to change the background image smoothly.The code is above.
    – waheed khan
    Nov 20 at 11:23










  • Do you have the project on GitHub?
    – Hudi Ilfeld
    Nov 20 at 12:17










  • No it's in my laptop
    – waheed khan
    Nov 20 at 12:26






  • 1




    @JinsLukose Thanks man it's work for me (Y) You make my day :) – waheed khan 3 hours ago One more thing how can i get the scroll distance of recyclerview because i want to animate my background image using fade out animation but that animation should be start when i scroll the recyclerview. Means i want when i scroll the recyclerview my background image also start fading out and when next image will appear it's start fading in animation
    – waheed khan
    Nov 22 at 8:42
















0














I want to change the background image of my layout on the basis of change of [ recyclerview. When I scroll recyclerview then on change state I want to change the background image of my layout smoothly like https://www.pinterest.com/pin/860891284992684260/



Here is my code:



//Updated Code



 @Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

int firstPos = HorizontalLayout.findFirstVisibleItemPosition();
int lastPos = HorizontalLayout.findLastVisibleItemPosition();
int middle = Math.abs(lastPos - firstPos) / 2 + firstPos;
View centerView = snapHelper.findSnapView(HorizontalLayout);
final int pos = HorizontalLayout.getPosition(centerView);
AllStories myStory = Stories.get(HorizontalLayout.getPosition(centerView));
// int selectedPos = -1;
for (int i = 0; i < RecyclerViewHorizontalAdapter.getItemCount(); i++) {
if (i == middle) {
if (myStory != null) {
if(dx>5)
{ background.setBackgroundResource(myStory.BackGroundImage);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);

AnimationSet animation = new AnimationSet(false); //change to false
animation.addAnimation(fadeOut);
background.setAnimation(animation);
}
background.setBackgroundResource(myStory.BackGroundImage);
//selectedPos = i;
LockStories(pos);
}
} else {

}
}



}
});









share|improve this question
























  • Do you have a link to your project so that I can try help?
    – Hudi Ilfeld
    Nov 20 at 11:19










  • Actually problem is that my background image is changing when i scroll the RecyclerView but i want to change the background image smoothly.The code is above.
    – waheed khan
    Nov 20 at 11:23










  • Do you have the project on GitHub?
    – Hudi Ilfeld
    Nov 20 at 12:17










  • No it's in my laptop
    – waheed khan
    Nov 20 at 12:26






  • 1




    @JinsLukose Thanks man it's work for me (Y) You make my day :) – waheed khan 3 hours ago One more thing how can i get the scroll distance of recyclerview because i want to animate my background image using fade out animation but that animation should be start when i scroll the recyclerview. Means i want when i scroll the recyclerview my background image also start fading out and when next image will appear it's start fading in animation
    – waheed khan
    Nov 22 at 8:42














0












0








0







I want to change the background image of my layout on the basis of change of [ recyclerview. When I scroll recyclerview then on change state I want to change the background image of my layout smoothly like https://www.pinterest.com/pin/860891284992684260/



Here is my code:



//Updated Code



 @Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

int firstPos = HorizontalLayout.findFirstVisibleItemPosition();
int lastPos = HorizontalLayout.findLastVisibleItemPosition();
int middle = Math.abs(lastPos - firstPos) / 2 + firstPos;
View centerView = snapHelper.findSnapView(HorizontalLayout);
final int pos = HorizontalLayout.getPosition(centerView);
AllStories myStory = Stories.get(HorizontalLayout.getPosition(centerView));
// int selectedPos = -1;
for (int i = 0; i < RecyclerViewHorizontalAdapter.getItemCount(); i++) {
if (i == middle) {
if (myStory != null) {
if(dx>5)
{ background.setBackgroundResource(myStory.BackGroundImage);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);

AnimationSet animation = new AnimationSet(false); //change to false
animation.addAnimation(fadeOut);
background.setAnimation(animation);
}
background.setBackgroundResource(myStory.BackGroundImage);
//selectedPos = i;
LockStories(pos);
}
} else {

}
}



}
});









share|improve this question















I want to change the background image of my layout on the basis of change of [ recyclerview. When I scroll recyclerview then on change state I want to change the background image of my layout smoothly like https://www.pinterest.com/pin/860891284992684260/



Here is my code:



//Updated Code



 @Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

int firstPos = HorizontalLayout.findFirstVisibleItemPosition();
int lastPos = HorizontalLayout.findLastVisibleItemPosition();
int middle = Math.abs(lastPos - firstPos) / 2 + firstPos;
View centerView = snapHelper.findSnapView(HorizontalLayout);
final int pos = HorizontalLayout.getPosition(centerView);
AllStories myStory = Stories.get(HorizontalLayout.getPosition(centerView));
// int selectedPos = -1;
for (int i = 0; i < RecyclerViewHorizontalAdapter.getItemCount(); i++) {
if (i == middle) {
if (myStory != null) {
if(dx>5)
{ background.setBackgroundResource(myStory.BackGroundImage);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);

AnimationSet animation = new AnimationSet(false); //change to false
animation.addAnimation(fadeOut);
background.setAnimation(animation);
}
background.setBackgroundResource(myStory.BackGroundImage);
//selectedPos = i;
LockStories(pos);
}
} else {

}
}



}
});






java android android-studio android-layout android-recyclerview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 11:18

























asked Nov 20 at 11:04









waheed khan

64




64












  • Do you have a link to your project so that I can try help?
    – Hudi Ilfeld
    Nov 20 at 11:19










  • Actually problem is that my background image is changing when i scroll the RecyclerView but i want to change the background image smoothly.The code is above.
    – waheed khan
    Nov 20 at 11:23










  • Do you have the project on GitHub?
    – Hudi Ilfeld
    Nov 20 at 12:17










  • No it's in my laptop
    – waheed khan
    Nov 20 at 12:26






  • 1




    @JinsLukose Thanks man it's work for me (Y) You make my day :) – waheed khan 3 hours ago One more thing how can i get the scroll distance of recyclerview because i want to animate my background image using fade out animation but that animation should be start when i scroll the recyclerview. Means i want when i scroll the recyclerview my background image also start fading out and when next image will appear it's start fading in animation
    – waheed khan
    Nov 22 at 8:42


















  • Do you have a link to your project so that I can try help?
    – Hudi Ilfeld
    Nov 20 at 11:19










  • Actually problem is that my background image is changing when i scroll the RecyclerView but i want to change the background image smoothly.The code is above.
    – waheed khan
    Nov 20 at 11:23










  • Do you have the project on GitHub?
    – Hudi Ilfeld
    Nov 20 at 12:17










  • No it's in my laptop
    – waheed khan
    Nov 20 at 12:26






  • 1




    @JinsLukose Thanks man it's work for me (Y) You make my day :) – waheed khan 3 hours ago One more thing how can i get the scroll distance of recyclerview because i want to animate my background image using fade out animation but that animation should be start when i scroll the recyclerview. Means i want when i scroll the recyclerview my background image also start fading out and when next image will appear it's start fading in animation
    – waheed khan
    Nov 22 at 8:42
















Do you have a link to your project so that I can try help?
– Hudi Ilfeld
Nov 20 at 11:19




Do you have a link to your project so that I can try help?
– Hudi Ilfeld
Nov 20 at 11:19












Actually problem is that my background image is changing when i scroll the RecyclerView but i want to change the background image smoothly.The code is above.
– waheed khan
Nov 20 at 11:23




Actually problem is that my background image is changing when i scroll the RecyclerView but i want to change the background image smoothly.The code is above.
– waheed khan
Nov 20 at 11:23












Do you have the project on GitHub?
– Hudi Ilfeld
Nov 20 at 12:17




Do you have the project on GitHub?
– Hudi Ilfeld
Nov 20 at 12:17












No it's in my laptop
– waheed khan
Nov 20 at 12:26




No it's in my laptop
– waheed khan
Nov 20 at 12:26




1




1




@JinsLukose Thanks man it's work for me (Y) You make my day :) – waheed khan 3 hours ago One more thing how can i get the scroll distance of recyclerview because i want to animate my background image using fade out animation but that animation should be start when i scroll the recyclerview. Means i want when i scroll the recyclerview my background image also start fading out and when next image will appear it's start fading in animation
– waheed khan
Nov 22 at 8:42




@JinsLukose Thanks man it's work for me (Y) You make my day :) – waheed khan 3 hours ago One more thing how can i get the scroll distance of recyclerview because i want to animate my background image using fade out animation but that animation should be start when i scroll the recyclerview. Means i want when i scroll the recyclerview my background image also start fading out and when next image will appear it's start fading in animation
– waheed khan
Nov 22 at 8:42












1 Answer
1






active

oldest

votes


















0














Try using fadeIn and fadeout animation when changing image.






share|improve this answer





















  • i try that but it's not working
    – waheed khan
    Nov 20 at 11:56











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%2f53391606%2fhow-can-i-change-background-image-while-scrolling-the-horizontal-recyclerview-in%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try using fadeIn and fadeout animation when changing image.






share|improve this answer





















  • i try that but it's not working
    – waheed khan
    Nov 20 at 11:56
















0














Try using fadeIn and fadeout animation when changing image.






share|improve this answer





















  • i try that but it's not working
    – waheed khan
    Nov 20 at 11:56














0












0








0






Try using fadeIn and fadeout animation when changing image.






share|improve this answer












Try using fadeIn and fadeout animation when changing image.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 11:46









Taha wakeel

896




896












  • i try that but it's not working
    – waheed khan
    Nov 20 at 11:56


















  • i try that but it's not working
    – waheed khan
    Nov 20 at 11:56
















i try that but it's not working
– waheed khan
Nov 20 at 11:56




i try that but it's not working
– waheed khan
Nov 20 at 11:56


















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%2f53391606%2fhow-can-i-change-background-image-while-scrolling-the-horizontal-recyclerview-in%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

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga