如何在不触发监听器的情况下选择标签?

时间:2018-11-17 07:19:40

标签: java android tabs

我有多个标签,可以这样说:

  1. 用户向下滑动:应根据其中的内容更改标签 类别。
  2. 用户单击选项卡:它应该移动 在该类别所在的视图的顶部。

我的问题是,一旦用户进入某个视图,当我选择选项卡时,它会触发onTabSelected并滑到视图的顶部。

有没有一种方法可以更改选择的选项卡而不触发监听器?

这是我的代码

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

            }

        });




    }
}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,只需通过以下代码对其进行修复。首先,我在课堂上声明TabLayout.OnTabSelectedListener字段-

private TabLayout.OnTabSelectedListener tabSelectedListener;

 tabSelectedListener = 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) {

            }
        });

最后,在选择之前删除tabSelectedListener,然后在选择之后再次添加。

nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY <= firstView.getBottom()){
                    /*First remove it*/
                    tabLayout.removeOnTabSelectedListener(tabSelectedListener);
                    tabLayout.getTabAt(0).select();
                    /*Then add it again*/
                    tabLayout.addOnTabSelectedListener(tabSelectedListener)

                    clicked = false;
                } else if (scrollY >= secondView.getTop() && scrollY <= secondView.getBottom() ){
                     /*First remove it*/
                    tabLayout.removeOnTabSelectedListener(tabSelectedListener);
                    tabLayout.getTabAt(1).select();
                     /*Then add it again*/
                    tabLayout.addOnTabSelectedListener(tabSelectedListener)
                    clicked = false;
                }

            }

        });