您的位置:首页 > 其它

安卓5.0之后的新控件TabLayout

2016-07-16 19:53 274 查看
安卓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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: