ogaclejapan / SmartTabLayout

A custom ViewPager title strip which gives continuous feedback to the user when scrolling
Apache License 2.0
7.09k stars 1.34k forks source link

Updating tab UI with LocalBroadcastManager #132

Closed aemxn closed 8 years ago

aemxn commented 8 years ago

Hi @ogaclejapan, great library you made here.

I'd like to ask a question regarding updating the tab UI with LocalBroadcastManager.

Here's what I'm trying to achieve: I put a counter besides the text in my tab, now whenever a push notification comes in, the tab will update its UI and display a counter. I'm using LocalBroadcastManager to register any broadcast event.

I'm also using a custom layout for the tabs. Here's what I've done:

    viewPagerTab.setCustomTabView(new SmartTabLayout.TabProvider() {

        final LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        final Resources res = getResources();
        private TextView counter;

        @Override
        public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
            View itemView = inflater.inflate(R.layout.custom_tab_impl, container, false);
            TextView text = (TextView) itemView.findViewById(R.id.custom_tab_text);
            counter = (TextView) itemView.findViewById(R.id.custom_tab_counter);
            ImageView icon = (ImageView) itemView.findViewById(R.id.custom_tab_icon);

             // rest my implementation

            return itemView;
        }
    });

But the problem is, the register event occurs outside of this TabProvider class which is in onCreate of current Activity. How can I register the Broadcast Manager inside this TabProvider so that I can update the UI of the tab whenever it listens to any broadcast message?

Is there any other alternative to this to achieve what I'm looking for?

EDIT

More precisely, I want to access TextView counter inside createTabView so I can modify its content.

ogaclejapan commented 8 years ago

Hi, @aimanbaharum I'm sorry late reply. I’ve been busy lately at work :P

Try using the SmartTabLayout#getTabAt(int) The method will be able to get the root view of the Tab

    View tab = viewPagerTab.getTabAt(your update position);
    TextView counter = (TextView) itemView.findViewById(R.id.custom_tab_counter);
shayzalman commented 8 years ago

This could very well relate to my issue.. https://github.com/ogaclejapan/SmartTabLayout/issues/134#issuecomment-195970079