How to detect a click on every single tab in tablayout?

为君一笑 提交于 2019-12-12 02:38:28

问题


I have a viewpager with undetermined pages, because the user can add pages too. Thats okay, but I have no idea how to make it possible, that my user could delete those pages. I have tried to implement a long click listener on every tab with the code below, but it is not working.

Then how to detect which one tab is clicked?

for (tabCounter = 0; tabCounter < DataOfPages.size(); tabCounter++) {
        tabLayout.getTabAt(tabCounter).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                dataSource.open();
                dataSource.deleteById(tabCounter);
                dataSource.close();

                setupViewPager(viewPager);
                return true;
            }
        });
    }

回答1:


Implementation of LongClick listener to each TAB:

LinearLayout tabStrip = (LinearLayout) tabLayout.getChildAt(0);

for (int i = 0; i < tabStrip.getChildCount(); i++) {

    // Set LongClick listener to each Tab        
    tabStrip.getChildAt(i).setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            Toast.makeText(getApplicationContext(), "Tab clicked" , Toast.LENGTH_SHORT).show();
            return true;
        }
    });
}

Hope this will help~



来源:https://stackoverflow.com/questions/43520804/how-to-detect-a-click-on-every-single-tab-in-tablayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!