FlycoTabLayout
github:GitHub - H07000223/FlycoTabLayout: An Android TabLayout Lib
SlidingTabLayout的用法
1、导入Gradle依赖
implementation 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'
2、布局
布局采用SlidingTabLayout和ViewPager 配合使用,在ViewPager中添加fragment,从而实现左右滑动 具体代码如下:
<?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=".tab_layout.Tab_Sildind_01Activity">
<com.flyco.tablayout.SlidingTabLayout
android:id="@+id/slide"
app:tl_textsize="20dp"
app:tl_indicator_color="@color/black"
android:layout_width="match_parent"
android:background="@android:color/holo_blue_bright"
android:layout_height="40dp"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPage"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
3、MianActivity
在MainActivity通过ViewPage增加Fragment 最后把SlidingTabLayout和ViewPager绑定在一起 具体
代码如下:
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.example.myapplication.R;
import com.example.myapplication.tab_layout.frag_ment.Tab_sild_01Fragment;
import com.example.myapplication.tab_layout.frag_ment.Tab_sild_02Fragment;
import com.example.myapplication.tab_layout.frag_ment.Tab_sild_03Fragment;
import com.example.myapplication.tab_layout.frag_ment.Tab_sild_04Fragment;
import com.example.myapplication.tab_layout.frag_ment.Tab_sild_05Fragment;
import com.example.myapplication.tab_layout.frag_ment.Tab_sild_06Fragment;
import com.flyco.tablayout.SlidingTabLayout;
import java.util.ArrayList;
import java.util.List;
public class Tab_Sildind_01Activity extends AppCompatActivity {
private SlidingTabLayout slide;
private ViewPager viewPager;
private String[] title={"第一个","第二个","第三个","第四个","第五个","第六个"};
private List<Fragment> fragments=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab__sildind_01);
// 初始化
init();
fragments.add(new Tab_sild_01Fragment());
fragments.add(new Tab_sild_02Fragment());
fragments.add(new Tab_sild_03Fragment());
fragments.add(new Tab_sild_04Fragment());
fragments.add(new Tab_sild_05Fragment());
fragments.add(new Tab_sild_06Fragment());
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@NonNull
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
});
slide.setViewPager(viewPager);
}
private void init() {
slide = (SlidingTabLayout) findViewById(R.id.slide);
viewPager = (ViewPager) findViewById(R.id.viewPage);
}
}
注:可以使用更简洁的接口 slide.setViewPager(xxx,xxx,xxx,xxx);
public void setViewPager(ViewPager vp, String[] titles, FragmentActivity fa, ArrayList<Fragment> fragments) {
if (vp == null) {
throw new IllegalStateException("ViewPager can not be NULL !");
} else if (titles != null && titles.length != 0) {
this.mViewPager = vp;
this.mViewPager.setAdapter(new SlidingTabLayout.InnerPagerAdapter(fa.getSupportFragmentManager(), fragments, titles));
this.mViewPager.removeOnPageChangeListener(this);
this.mViewPager.addOnPageChangeListener(this);
this.notifyDataSetChanged();
} else {
throw new IllegalStateException("Titles can not be EMPTY !");
}
}
4、实现效果