TabLayout + Fragment
效果
向左滑动切换Fragment
代码
layout:ac_sliding
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#155e10"
app:tabIndicatorColor="#ffffff"
app:tabSelectedTextColor="#999999"
app:tabTextColor="#ffffff"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
Activity:SlidingActivity
/**
* Created by kangfh on 2016/9/12.
* 滑动界面
*/
public class SlidingActivity extends BaseActivity {
private TabLayout tablayout;
private ViewPager viewpager;
private List<String> list_title;
private List<Fragment> list_fragment;
private TabLayoutAdapter tabLayoutAdapter ;
private void assignViews() {
tablayout = (TabLayout) findViewById(R.id.tablayout);
viewpager = (ViewPager) findViewById(R.id.viewpager);
}
@Override
public void initViews() {
setContentView(R.layout.ac_sliding);
assignViews();
}
@Override
public void initData() {
list_title = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list_title.add("第" + i + "个");
}
list_fragment = new ArrayList<>();
for (int i = 0; i < list_title.size(); i++) {
SlidingFragment slidingFragment = new SlidingFragment();
Bundle bundle = new Bundle();
bundle.putString("content", "第" + i + "个界面");
slidingFragment.setArguments(bundle);
list_fragment.add(slidingFragment);
}
//TabLayout滑动模式
tablayout.setTabMode(TabLayout.MODE_SCROLLABLE);//滑动
for(int i=0;i<list_title.size();i++){
tablayout.addTab(tablayout.newTab().setText(list_title.get(i)));
}
tabLayoutAdapter=new TabLayoutAdapter(getSupportFragmentManager(),list_title,list_fragment) ;
viewpager.setAdapter(tabLayoutAdapter);
tablayout.setupWithViewPager(viewpager);
}