Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add DataSetObserver callback #304

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
Expand Down Expand Up @@ -85,6 +86,7 @@ public class SmartTabLayout extends HorizontalScrollView {
private TabProvider tabProvider;
private InternalTabClickListener internalTabClickListener;
private OnTabClickListener onTabClickListener;
private DataSetObserver mDataSetObserver;
private boolean distributeEvenly;

public SmartTabLayout(Context context) {
Expand Down Expand Up @@ -320,11 +322,16 @@ public void setCustomTabView(TabProvider provider) {
* (number of tabs and tab titles) does not change after this call has been made.
*/
public void setViewPager(ViewPager viewPager) {
tabStrip.removeAllViews();

this.viewPager = viewPager;
if (viewPager != null && viewPager.getAdapter() != null) {
viewPager.addOnPageChangeListener(new InternalViewPagerListener());

if(mDataSetObserver !=null)
viewPager.getAdapter().unregisterDataSetObserver(mDataSetObserver);
if(mDataSetObserver == null){
mDataSetObserver = new TabDataSetObserver();
}
viewPager.getAdapter().registerDataSetObserver(mDataSetObserver);
populateTabStrip();
}
}
Expand Down Expand Up @@ -382,6 +389,8 @@ protected TextView createDefaultTabView(CharSequence title) {
}

private void populateTabStrip() {
tabStrip.removeAllViews();

final PagerAdapter adapter = viewPager.getAdapter();

for (int i = 0; i < adapter.getCount(); i++) {
Expand Down Expand Up @@ -550,6 +559,23 @@ public interface TabProvider {

}

/**
* When adapter data is change,callback invoke
* {@link android.database.DataSetObservable}
*/
private class TabDataSetObserver extends DataSetObserver {

@Override
public void onChanged() {
populateTabStrip();
}

@Override
public void onInvalidated() {
populateTabStrip();
}
}

private static class SimpleTabProvider implements TabProvider {

private final LayoutInflater inflater;
Expand Down