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

always in center issue #117

Open nozominull opened 8 years ago

nozominull commented 8 years ago

Click the button(to update data),the first tab appears in the left side;When tap another tab,the new selected tab scroll to left too.

image

this is my code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button" />
    <com.ogaclejapan.smarttablayout.SmartTabLayout
        android:id="@+id/tab_layout"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="72dp"
        app:stl_indicatorAlwaysInCenter="true" />
    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

public class TestActivity extends AppCompatActivity {
    @Bind(R.id.button)
    Button mButtonView;
    @Bind(R.id.tab_layout)
    SmartTabLayout mTabLayoutView;
    @Bind(R.id.view_pager)
    ViewPager mViewPagerView;
    private int mSize = 0;
    private class FragmentAdapter extends FragmentPagerAdapter {
        public FragmentAdapter(final FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(final int position) {
            return "title" + position;
        }

        @Override
        public int getCount() {
            return mSize;
        }

        @Nullable
        @Override
        public Fragment getItem(final int position) {
            return TestFragment.newInstance();
        }
    }

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

        ButterKnife.bind(this);

        final FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        mViewPagerView.setAdapter(fragmentAdapter);

        mButtonView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                mSize = 10;
                fragmentAdapter.notifyDataSetChanged();
                mTabLayoutView.setViewPager(mViewPagerView);
            }
        });
    }
}
ogaclejapan commented 8 years ago

Hi, @xuyangbill Thank you for reporting.

It is a bug...

hyvenzhu commented 7 years ago

how to solve it? i just meet

ogaclejapan commented 7 years ago

Sorry, It hasn't solved yet :(

engkan2kit commented 7 years ago

I think I found the problem.

I had two version of my code using this library in fragment style. The first version adds the pages before setting ViewPager with adapter and before setting the SmartTabLayout in the onCreateView.

The second version loads the pagerTab initially with empty pages.

The second version fails to center.

I tested adding static pages in the on createView before setting the pagerTab. The replace the contents of the adapter later when the real pages are available. The alwaysOnCenter now works.

Therefore the problem occurs when the viewPager and the SmartTabLayout is loaded with empty pages first.

zeroarst commented 6 years ago

call this after setViewPager fixes for me. stl.post({ stl.requestLayout() })