如何在TabLayout android上的活动中获取标签点击事件

l7mqbcuq  于 2023-03-11  发布在  Android
关注(0)|答案(8)|浏览(414)

这是onCreate方法在我的活动中的实现。我需要动态地创建标签。为了简单起见,我做了2个标签。

public class ActivityProductList extends AppCompatActivity {

    private android.support.v7.widget.SearchView searchView = null;
    private RecyclerView recyclerView;
    MyAdapter adapter;
    List<ParentListItem> parentListItems = new ArrayList<>();
    List<ParentListItem> originalProductList = new ArrayList<>();
    String query = null;
    int categoryId = 0;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("PRODUCTS");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
        tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
        //setupViewPager(viewPager);
        //tabLayout.setupWithViewPager(viewPager);
        //viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        searchView = (android.support.v7.widget.SearchView) findViewById(R.id.searchView);
        searchView.setIconified(false);
        searchView.onActionViewExpanded();
        searchView.clearFocus();
        searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (adapter != null)
                    filterData(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                if (adapter != null)
                    filterData(query);
                return false;
            }
        });

    }
}

我想在单击选项卡时更改EditText的值。该EditText位于同一活动中。
如何在Activity中获取Tab单击事件?

cgfeq70w

cgfeq70w1#

您可以使用onTabSelected侦听器来实现这一点。

tabLayout.addOnTabSelectedListener(new OnTabSelectedListener() {
        @Override
        public void onTabSelected(Tab tab) {
             switch(tab.getPosition()) {
                 case 0:
                  ....
             }
        }

注:setOnTabSelectedListener已弃用,请改用addOnTabSelectedListener

3pmvbmvn

3pmvbmvn2#

您可以像这样在Kotlin中使用onTabSelected侦听器

tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        })
sz81bmfz

sz81bmfz3#

如果你想要的是当一个选项卡被点击,而不是每次一个选项卡被选中,你可以使用自定义视图为每个选项卡,然后处理点击各自的视图,像这样:

for (int i = 0; i < tabLayout.getTabCount(); i++) {
    final TabLayout.Tab tab = tabLayout.getTabAt(i);
    final View tabView = LayoutInflater.from(this).inflate(
            R.layout.item_tab, (ViewGroup) tab.getCustomView(), false);
    tabLayout.setCustomView(tabView);

    final TextView customView = (TextView) tab.getCustomView();
    customView.setText(mAdapter.getPageTitle(i));
    final int index = i;
    customView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (tabLayout.getSelectedTabPosition() == index) {
                // change edittext value
            } else {
                tab.select();
            }
        }
    });
}
3df52oht

3df52oht4#

在Java中,

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                Toast.makeText(this, "position : "+position, Toast.LENGTH_SHORT).show();
            }

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

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

scyqe7ek5#

正如@dgngulcan所提到的,onTabSelected也将作为滑动的结果而被调用,而不仅仅是点击标签。
我们可以直接使用选项卡中的视图作为参数
在这里,我将选项卡与视图分页器结合在一起,并添加了一个单击侦听器,如果单击它,它将调用某个函数

tabLayout = binding.tabLayout
    val tabAll = tabLayout.newTab().setText("All")
    val tabFavourite = tabLayout.newTab().setText("Favourite")
    tabLayout.addTab(tabAll)
    tabLayout.addTab(tabFavourite)
    tabLayout.setSelectedTabIndicatorColor(Color.WHITE)

    tabAll.view.setOnClickListener {
        if(viewPager.currentItem==0)
            allCoinsListFragment.scrollToTop()
        else
        viewPager.currentItem = 0
    }
    tabFavourite.view.setOnClickListener {
        if(viewPager.currentItem==1)
            favouriteCoinsListFragment.scrollToTop()
        else
        viewPager.currentItem = 1
    }
dddzy1tm

dddzy1tm6#

以下是Shashank Udupa的答案的Xamarin.Android/C#端口,对我有效:
首先,您应该实现IOnTabSelectedListener接口,如下所示:

public class MyOnTabSelectedListener : Java.Lang.Object, TabLayout.IOnTabSelectedListener
{
    Activity _activity;

    public MyOnTabSelectedListener(Activity activity)
    {
        _activity = activity;
    }

    public void OnTabReselected(TabLayout.Tab tab)
    {
    }

    public void OnTabSelected(TabLayout.Tab tab)
    {
        switch (tab.Position)
        {
            case 0:
                // your logic goes here!
                break;

            // ...
        }
    }

    public void OnTabUnselected(TabLayout.Tab tab)
    {
    }
}

然后像这样使用上面的类:

tabLayout.AddOnTabSelectedListener(new MyOnTabSelectedListener(this));
qqrboqgw

qqrboqgw7#

这能帮助一些人

TabLayoutMediator(tabLayout, viewPager) { tab, position ->
                tab.text = tabTitle[position]
                if (position == YOUR_TAB_POSITION) {
                    tab.view.isClickable = false
                    tab.view.setOnTouchListener { view, motionEvent ->
                        if(Your_condition){ 
                            false
                        } else {
                            view.performClick()
                        }
                    }
                } else {
                    tab.view.isClickable = true
                }
            }.attach()

我之所以不使用onTouch来点击切换普通标签,是因为它会破坏TabLayout的幻灯片动画,所以我仍然使用clickable = true

bq3bfh9z

bq3bfh9z8#

private fun setupTabLayout() {

    tabLayout?.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {

        override fun onTabSelected(tab: TabLayout.Tab?) {

            when (tab?.position){
                0 -> println(0)
                1 -> println(1)
                2 -> println(2)
            }
        }

        override fun onTabReselected(tab: TabLayout.Tab?) {
            // Write code to handle tab reselect
        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {
            // Write code to handle tab reselect
        }
    })
}

相关问题