其实我想实现这样的效果,但发现我咋都百度不出来,所以还是分享自己的劳动成果把。
效果图
tablayout +viewpage的实现网上有很多,我就不说了,这里主要是实现一下自定义tab并实现上图那样的效果。
难点一:去除tab之间的间距(主要代码是tabpadding的那四行)
<android.support.design.widget.TabLayout
android:id="@+id/comment_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="-1dp"
app:tabIndicatorColor="@color/blue_cloud_scheme"
app:tabPaddingBottom="-1dp"
app:tabPaddingEnd="-1dp"
app:tabPaddingStart="-1dp"
app:tabPaddingTop="-1dp"
app:tabSelectedTextColor="@color/blue_cloud_scheme"
app:tabTextColor="#FF000000"
app1:tabGravity="fill"
app1:tabIndicatorHeight="0dp"
app1:tabMode="fixed" />
难点二:两个tab的圆角位置是不同的,所以我们需要创建两个自定义view。
下面是完整代码
1.创建tablayout的布局(上面难点一那个)。
2.实现Activity。
//一定要继承FragmentActivity
public class Activity extends FragmentActivity {
private List<Fragment> datas;// 数据源
private List<String> titles;
private TabLayout tab;
private ViewPager viewPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
initDatas();
initView();
}
//添加两个tab的名称,和两个子fragment。
private void initDatas() {
datas = new ArrayList<Fragment>();
titles = new ArrayList<String>();
datas.add(new FragmentA(Activity.this));
datas.add(new FragmentB(Activity.this));
titles.add("电桩详情");
titles.add("充电终端");
}
private void initView() {
// TODO Auto-generated method stub
tab = (TabLayout) findViewById(R.id.comment_tab);
viewPage = (ViewPager) findViewById(R.id.comment_viewpager);
tab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab1) {
viewPage.setCurrentItem(tab1.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab1) {
}
@Override
public void onTabReselected(TabLayout.Tab tab1) {
}
});
FragmentPagerAdapter mAdapter = new FragmentPagerAdapter(getSupportFragmentManager(), datas,
titles);
tab.setTabsFromPagerAdapter(mAdapter);
TabLayout.TabLayoutOnPageChangeListener listener = new TabLayout.TabLayoutOnPageChangeListener(tab);
viewPage.addOnPageChangeListener(listener);
viewPage.setAdapter(mAdapter);
tab.setupWithViewPager(viewPage);
//这里就是将两个自定义tab添加到tablayout中
TabLayout.Tab tabs = tab.getTabAt(0);
tabs.setCustomView(mAdapter.getTabView0());
tabs = tab.getTabAt(1);
tabs.setCustomView(mAdapter.getTabView1());
//这一行代码是为了实现从别的页面跳转过来之后可以跳转到不同的tab和viewpage下,而不是老是默认从第一个开始。
viewPage.setCurrentItem(currentTab);
}
3. 实现FragmentPagerAdapter
public class FragmentPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> view;
private List<String> titles;
private LayoutInflater mInflater;
public DetailFragmentPagerAdapter(FragmentManager fragmentManager, List<Fragment> views, List<String> titles) {
super(fragmentManager);
this.titles = titles;
this.view = views;
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
return view.get(position);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return view.size();
}
//没有自定义view的话,这里是显示tab标题的名称,但是自定义之后就需要把这里消除掉
// @Override
// public CharSequence getPageTitle(int position) {
//
// return titles.get(position);
// }
//因为是两个不同的自定义Tab我就分成两个写了,不知道大家有什么更好的处理方法。
public View getTabView0() {
mInflater = LayoutInflater.from(Context);
View view = mInflater.inflate(R.layout.item_tab_left, null);
TextView tv = (TextView) view.findViewById(R.id.tab_text_left);
tv.setText(titles.get(0));
return view;
}
public View getTabView1() {
mInflater = LayoutInflater.from(Context);
View view = mInflater.inflate(R.layout.item_tab_right, null);
TextView tv = (TextView) view.findViewById(R.id.tab_text_right);
tv.setText(titles.get(1));
return view;
}
}
4.item_tab_left的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tab_left_selector"
android:orientation="vertical" >
<TextView
android:id="@+id/tab_text_left"
android:layout_width="80dp"
android:layout_height="30dp"
android:gravity="center"
android:textColor="@color/black" />
</LinearLayout>
5.tab_left_selector的实现
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tab_left_unselect" android:state_selected="false"/>
<item android:drawable="@drawable/tab_left_select" android:state_selected="true"/>
</selector>
6.tab_left_select的实现
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 边缘线的宽度和颜色 -->
<corners
android:bottomLeftRadius="6dp"
android:topLeftRadius="6dp" />
<solid android:color="@color/white" />
<stroke
android:width="1dp"
android:color="@color/blue_title" />
</shape>
7.tab_left_unselect的实现就是改一下这个 再往后应该都会了把
<stroke
android:width="1dp"
android:color="@color/white" />
代码基本没问题,就是改成自己的颜色,图片,还有context就ok了
忘了还有fragment的实现自己百度哈