安卓5.0之后的新控件TabLayout

安卓5.0之后的新控件TabLayout

可以实现类似于viewpager的效果

使用起来更方便

主界面代码:

package my.qq.com.day0715;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

/**
 * TabLayout安卓5.0之后的新控件
 * 上面是标签,下面是viewpager,点击标签,viewpager会跟着切换
 */
public class TabLayoutActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    String[] titles=new String[10];
    String[] contents=new String[10];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
        tabLayout= (TabLayout) findViewById(R.id.TabLayoutActivity_tab);
        viewPager= (ViewPager) findViewById(R.id.TabLayoutActivity_vp);
        addData();
        //给tablayout添加每一个tab
        for (int i=0;i<titles.length;i++){
            tabLayout.addTab(tabLayout.newTab().setText(titles[i]));
        }
        MyAdapter myAdapter=new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(myAdapter);
        //给ViewPager添加TabLayout变化的监听
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        //给TabLay设置点击的监听
        tabLayout.addOnTabSelectedListener(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) {

            }
        });


    }
    public void addData(){

        for (int j=0;j<10;j++){
            titles[j]="title"+j;
            contents[j]="contents"+j;
        }
    }

    class MyAdapter extends FragmentPagerAdapter{
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment f=new TabLayoutFragment();
            Bundle bundle=new Bundle();
            bundle.putString("contents",contents[position]);
            f.setArguments(bundle);
            return f;
        }

        @Override
        public int getCount() {
            return titles.length;
        }
    }
}
主界面布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context="my.qq.com.day0715.TabLayoutActivity">
    <!-- app:tabIndicatorColor="#ff0000"选中标签下划线颜色 ,app:tabTextColor="#00ff00"文字颜色,
    app:tabSelectedTextColor="#0000ff"选中文字颜色
    app:tabMode="scrollable"可划动
    -->
    <android.support.design.widget.TabLayout
        android:id="@+id/TabLayoutActivity_tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="#ff0000"
        app:tabTextColor="#00ff00"
        app:tabSelectedTextColor="#0000ff"
        app:tabMode="scrollable"
        app:tabGravity="center">

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

    <android.support.v4.view.ViewPager
        android:id="@+id/TabLayoutActivity_vp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></android.support.v4.view.ViewPager>

</LinearLayout>
 

碎片代码


package my.qq.com.day0715;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by czw on 2016/7/15  15:01.
 */
public class TabLayoutFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.tabfragment_item,container,false);
        TextView textView= (TextView) view.findViewById(R.id.tab_item_tv);
        Bundle bundle=getArguments();
        String text=bundle.getString("contents");
        textView.setText(text);
        return view;
    }
}

碎片布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tab_item_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="填充碎片"/>

</LinearLayout>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值