How to select a tab without triggering listener?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have multiple tabs and let's say:




  1. The user swipes down: it should change tabs depending on which
    category it is on.

  2. The user clicks on the tab: it should move
    to the the top of the view the category is on.


My problem is when I select the tab once the user gets to a certain view, it triggers onTabSelected and slides to the top of the view.



Is there a way I could change the tab selected without triggering the Listener?



This is my code



public class Activity extends AppCompatActivity {

public TabLayout tabLayout;
public RecyclerView firstView,secondView;
public NestedScrollView nestedScrollView;
public RelativeLayout relativeLayout;
public boolean clicked = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meat_category);

//removed unimportant codes


tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { // I removed the boolean here because I don't know where to change the value of it.
@Override
public void onTabSelected(TabLayout.Tab tab) {
if(tabLayout.getSelectedTabPosition() == 0){
nestedScrollView.scrollTo(0, firstView.getTop());
}else if(tabLayout.getSelectedTabPosition() == 1){
nestedScrollView.smoothScrollTo(0, secondView.getTop());
}
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY <= firstView.getBottom()){
tabLayout.getTabAt(0).select();
clicked = false;
} else if (scrollY >= secondView.getTop() && scrollY <= secondView.getBottom() ){
tabLayout.getTabAt(1).select();
clicked = false;
}

}

});




}
}









share|improve this question

























  • can you share your code for more information.

    – Savin Sharma
    Nov 17 '18 at 7:24






  • 1





    You could temporarily remove the OnTabSelectedListener – i.e., call setOnTabSelectedListener(null) – and then set it back afterwards. Alternatively, you could keep some sort of flag variable – e.g., a boolean – to indicate whether or not you should anything in onTabSelected().

    – Mike M.
    Nov 17 '18 at 7:37











  • hey mike thanks for the suggestion. I had already tried the boolean. I had trouble wrapping my head on how to change the value if the user clicked the tabs instead of scrolling. Ive edit my question.

    – Kristofer
    Nov 17 '18 at 16:35











  • @SavinSharma I've updated it!

    – Kristofer
    Nov 17 '18 at 16:41











  • @Kristofer Why you are scrolling your nested view to the top every time you change the tab? This is definitely going to slide the view to the top.

    – Savin Sharma
    Nov 18 '18 at 6:22


















0















I have multiple tabs and let's say:




  1. The user swipes down: it should change tabs depending on which
    category it is on.

  2. The user clicks on the tab: it should move
    to the the top of the view the category is on.


My problem is when I select the tab once the user gets to a certain view, it triggers onTabSelected and slides to the top of the view.



Is there a way I could change the tab selected without triggering the Listener?



This is my code



public class Activity extends AppCompatActivity {

public TabLayout tabLayout;
public RecyclerView firstView,secondView;
public NestedScrollView nestedScrollView;
public RelativeLayout relativeLayout;
public boolean clicked = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meat_category);

//removed unimportant codes


tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { // I removed the boolean here because I don't know where to change the value of it.
@Override
public void onTabSelected(TabLayout.Tab tab) {
if(tabLayout.getSelectedTabPosition() == 0){
nestedScrollView.scrollTo(0, firstView.getTop());
}else if(tabLayout.getSelectedTabPosition() == 1){
nestedScrollView.smoothScrollTo(0, secondView.getTop());
}
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY <= firstView.getBottom()){
tabLayout.getTabAt(0).select();
clicked = false;
} else if (scrollY >= secondView.getTop() && scrollY <= secondView.getBottom() ){
tabLayout.getTabAt(1).select();
clicked = false;
}

}

});




}
}









share|improve this question

























  • can you share your code for more information.

    – Savin Sharma
    Nov 17 '18 at 7:24






  • 1





    You could temporarily remove the OnTabSelectedListener – i.e., call setOnTabSelectedListener(null) – and then set it back afterwards. Alternatively, you could keep some sort of flag variable – e.g., a boolean – to indicate whether or not you should anything in onTabSelected().

    – Mike M.
    Nov 17 '18 at 7:37











  • hey mike thanks for the suggestion. I had already tried the boolean. I had trouble wrapping my head on how to change the value if the user clicked the tabs instead of scrolling. Ive edit my question.

    – Kristofer
    Nov 17 '18 at 16:35











  • @SavinSharma I've updated it!

    – Kristofer
    Nov 17 '18 at 16:41











  • @Kristofer Why you are scrolling your nested view to the top every time you change the tab? This is definitely going to slide the view to the top.

    – Savin Sharma
    Nov 18 '18 at 6:22














0












0








0


1






I have multiple tabs and let's say:




  1. The user swipes down: it should change tabs depending on which
    category it is on.

  2. The user clicks on the tab: it should move
    to the the top of the view the category is on.


My problem is when I select the tab once the user gets to a certain view, it triggers onTabSelected and slides to the top of the view.



Is there a way I could change the tab selected without triggering the Listener?



This is my code



public class Activity extends AppCompatActivity {

public TabLayout tabLayout;
public RecyclerView firstView,secondView;
public NestedScrollView nestedScrollView;
public RelativeLayout relativeLayout;
public boolean clicked = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meat_category);

//removed unimportant codes


tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { // I removed the boolean here because I don't know where to change the value of it.
@Override
public void onTabSelected(TabLayout.Tab tab) {
if(tabLayout.getSelectedTabPosition() == 0){
nestedScrollView.scrollTo(0, firstView.getTop());
}else if(tabLayout.getSelectedTabPosition() == 1){
nestedScrollView.smoothScrollTo(0, secondView.getTop());
}
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY <= firstView.getBottom()){
tabLayout.getTabAt(0).select();
clicked = false;
} else if (scrollY >= secondView.getTop() && scrollY <= secondView.getBottom() ){
tabLayout.getTabAt(1).select();
clicked = false;
}

}

});




}
}









share|improve this question
















I have multiple tabs and let's say:




  1. The user swipes down: it should change tabs depending on which
    category it is on.

  2. The user clicks on the tab: it should move
    to the the top of the view the category is on.


My problem is when I select the tab once the user gets to a certain view, it triggers onTabSelected and slides to the top of the view.



Is there a way I could change the tab selected without triggering the Listener?



This is my code



public class Activity extends AppCompatActivity {

public TabLayout tabLayout;
public RecyclerView firstView,secondView;
public NestedScrollView nestedScrollView;
public RelativeLayout relativeLayout;
public boolean clicked = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meat_category);

//removed unimportant codes


tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { // I removed the boolean here because I don't know where to change the value of it.
@Override
public void onTabSelected(TabLayout.Tab tab) {
if(tabLayout.getSelectedTabPosition() == 0){
nestedScrollView.scrollTo(0, firstView.getTop());
}else if(tabLayout.getSelectedTabPosition() == 1){
nestedScrollView.smoothScrollTo(0, secondView.getTop());
}
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY <= firstView.getBottom()){
tabLayout.getTabAt(0).select();
clicked = false;
} else if (scrollY >= secondView.getTop() && scrollY <= secondView.getBottom() ){
tabLayout.getTabAt(1).select();
clicked = false;
}

}

});




}
}






java android tabs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 17:06









Moralous

81212




81212










asked Nov 17 '18 at 7:19









KristoferKristofer

130112




130112













  • can you share your code for more information.

    – Savin Sharma
    Nov 17 '18 at 7:24






  • 1





    You could temporarily remove the OnTabSelectedListener – i.e., call setOnTabSelectedListener(null) – and then set it back afterwards. Alternatively, you could keep some sort of flag variable – e.g., a boolean – to indicate whether or not you should anything in onTabSelected().

    – Mike M.
    Nov 17 '18 at 7:37











  • hey mike thanks for the suggestion. I had already tried the boolean. I had trouble wrapping my head on how to change the value if the user clicked the tabs instead of scrolling. Ive edit my question.

    – Kristofer
    Nov 17 '18 at 16:35











  • @SavinSharma I've updated it!

    – Kristofer
    Nov 17 '18 at 16:41











  • @Kristofer Why you are scrolling your nested view to the top every time you change the tab? This is definitely going to slide the view to the top.

    – Savin Sharma
    Nov 18 '18 at 6:22



















  • can you share your code for more information.

    – Savin Sharma
    Nov 17 '18 at 7:24






  • 1





    You could temporarily remove the OnTabSelectedListener – i.e., call setOnTabSelectedListener(null) – and then set it back afterwards. Alternatively, you could keep some sort of flag variable – e.g., a boolean – to indicate whether or not you should anything in onTabSelected().

    – Mike M.
    Nov 17 '18 at 7:37











  • hey mike thanks for the suggestion. I had already tried the boolean. I had trouble wrapping my head on how to change the value if the user clicked the tabs instead of scrolling. Ive edit my question.

    – Kristofer
    Nov 17 '18 at 16:35











  • @SavinSharma I've updated it!

    – Kristofer
    Nov 17 '18 at 16:41











  • @Kristofer Why you are scrolling your nested view to the top every time you change the tab? This is definitely going to slide the view to the top.

    – Savin Sharma
    Nov 18 '18 at 6:22

















can you share your code for more information.

– Savin Sharma
Nov 17 '18 at 7:24





can you share your code for more information.

– Savin Sharma
Nov 17 '18 at 7:24




1




1





You could temporarily remove the OnTabSelectedListener – i.e., call setOnTabSelectedListener(null) – and then set it back afterwards. Alternatively, you could keep some sort of flag variable – e.g., a boolean – to indicate whether or not you should anything in onTabSelected().

– Mike M.
Nov 17 '18 at 7:37





You could temporarily remove the OnTabSelectedListener – i.e., call setOnTabSelectedListener(null) – and then set it back afterwards. Alternatively, you could keep some sort of flag variable – e.g., a boolean – to indicate whether or not you should anything in onTabSelected().

– Mike M.
Nov 17 '18 at 7:37













hey mike thanks for the suggestion. I had already tried the boolean. I had trouble wrapping my head on how to change the value if the user clicked the tabs instead of scrolling. Ive edit my question.

– Kristofer
Nov 17 '18 at 16:35





hey mike thanks for the suggestion. I had already tried the boolean. I had trouble wrapping my head on how to change the value if the user clicked the tabs instead of scrolling. Ive edit my question.

– Kristofer
Nov 17 '18 at 16:35













@SavinSharma I've updated it!

– Kristofer
Nov 17 '18 at 16:41





@SavinSharma I've updated it!

– Kristofer
Nov 17 '18 at 16:41













@Kristofer Why you are scrolling your nested view to the top every time you change the tab? This is definitely going to slide the view to the top.

– Savin Sharma
Nov 18 '18 at 6:22





@Kristofer Why you are scrolling your nested view to the top every time you change the tab? This is definitely going to slide the view to the top.

– Savin Sharma
Nov 18 '18 at 6:22












0






active

oldest

votes












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%2f53349124%2fhow-to-select-a-tab-without-triggering-listener%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349124%2fhow-to-select-a-tab-without-triggering-listener%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

Bressuire

Vorschmack

Quarantine