TabLayout结合ViewPager使用

上个图吧:




========================================

Java代码:

/**
 * Created by wzl on 2016/4/15.
 * 分享
 */
public class SharesActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private FragmentPagerAdapter adapter;
    private ArrayList<Fragment> fragments = new ArrayList<>(2);

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

    private void init() {
        findViewById(R.id.image_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        TextView right = (TextView) findViewById(R.id.title_right);
        right.setBackgroundResource(R.drawable.default_round_corner_bg_trans_white_stroke);
        right.setPadding(AppUtils.dip2px(5), AppUtils.dip2px(2), AppUtils.dip2px(5), AppUtils.dip2px(2));
        right.setText("输入邀请码");
        right.setTextSize(AppUtils.dip2px(12) / getResources().getDisplayMetrics().scaledDensity + 0.5f);
        right.setTextColor(getResources().getColor(R.color.white));
        findViewById(R.id.layout_title).setBackgroundResource(R.color.title_color);
        ((TextView) findViewById(R.id.title)).setText("分享");
        TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
        tabs.setSelectedTabIndicatorColor(getResources().getColor(R.color.white));
        tabs.addTab(tabs.newTab().setText("Tab1"));
        tabs.addTab(tabs.newTab().setText("Tab2"));
        tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        });
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        TabLayout.TabLayoutOnPageChangeListener listener = new TabLayout.TabLayoutOnPageChangeListener(tabs);
        viewPager.addOnPageChangeListener(listener);
        fragments.add(new OrderItemFragment());
        fragments.add(new AcceptOrderItemFragment());
        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };
        viewPager.setAdapter(adapter);
    }
}



xml布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <include layout="@layout/layout_title" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="-48dp"
            android:background="@color/default_background"
            app:layout_scrollFlags="scroll|enterAlways">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="@drawable/activity_share_header">

                <LinearLayout
                    android:id="@+id/qr_code_container"
                    android:layout_width="wrap_content"
                    android:layout_height="182.5dp"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="@dimen/dimen_30"
                    android:background="@drawable/activity_share_qr_code_bg"
                    android:gravity="center_horizontal">

                    <ImageView
                        android:id="@+id/qr_code"
                        android:layout_width="@dimen/dimen_120"
                        android:layout_height="@dimen/dimen_120"
                        android:layout_marginTop="@dimen/dimen_20" />
                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/qr_code_container"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="-35dp"
                    android:background="@drawable/share_button_bg"
                    android:gravity="center"
                    android:text="点击分享" />

            </RelativeLayout>
        </LinearLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dimen_45"
            android:background="@android:color/transparent"
            android:nestedScrollingEnabled="false"
            android:paddingBottom="@dimen/dimen_3"
            app:tabIndicatorColor="@color/white"
            app:tabSelectedTextColor="@color/white"
            app:tabTextColor="@color/title_color_deep_dark"></android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值