关闭

横向滑动Tablayout

66人阅读 评论(0) 收藏 举报

横向滑动只需要在布局用Tablyout控件

在此之前在build.gradle里面添加

compile 'com.android.support:design:25.0.1'

下面是布局文件

<android.support.design.widget.TabLayout
    android:id="@+id/tb"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabIndicatorColor="#f00"
    app:tabMode="scrollable"
    app:tabSelectedTextColor="#f0f9">

</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/vp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

主页代码

public class MainActivity extends AppCompatActivity {

    private TabLayout tb;
    private ViewPager viewp;
    String[] title = new String[]{"初学者", "演员", "认真的雪", "火星人来过", "听说", "我好像在哪见过你", "方圆几里", "绅士"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tb = (TabLayout) findViewById(R.id.tb);
        viewp = (ViewPager) findViewById(R.id.vp);
        //默认选中第一个
        tb.addTab(tb.newTab().setText(title[0]), true);
        addtab();
        tb.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewp.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        });
        viewp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return title.length;
            }

            @Override
            public Fragment getItem(int position) {
                return Fragment.getInstance(title[position]);
            }


        });
        viewp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tb.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void addtab() {
        for (int i = 1; i < title.length; i++) {
            tb.addTab(tb.newTab().setText(title[i]), false);
        }
    }
}
ViewPage的setAdapter里面Fragment.getInstance(title[position])传值到Fragment

需要创建一个Fargment


public class Fragment extends android.support.v4.app.Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        TextView tv = new TextView(getActivity());
        tv.setText(getArguments().getString("title"));
        return tv;

    }

    public static Fragment getInstance(String title) {
        Fragment fragment = new Fragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        fragment.setArguments(args);
        return fragment;
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:518次
    • 积分:71
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档
    最新评论