项目预览
新建一个Fragment布局
TabLayout02Fragment 代码如下
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import com.hbj.basicdemo02.Adapter.TabLayout02BaseQueickAdapter;
import com.hbj.basicdemo02.R;
import java.util.ArrayList;
import java.util.List;
public class TabLayout02Fragment extends Fragment {
private String content;
private View view;
private RecyclerView recycler_tablayout02_view;
private List<String> titleList;
public TabLayout02Fragment(String content) {
this.content=content;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_tab_layout02, container, false);
recycler_tablayout02_view = view.findViewById(R.id.recycler_tablayout02_view);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
titleList=new ArrayList<>();
for (int i = 0; i < 30; i++) {
titleList.add(content+i);
}
TabLayout02BaseQueickAdapter queickAdapter = new TabLayout02BaseQueickAdapter(R.layout.item_person_type_rv, titleList);
recycler_tablayout02_view.setAdapter(queickAdapter);
}
}
布局文件fragment_tab_layout02.xml
<?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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".TabLayout.Fragment.TabLayout02Fragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_tablayout02_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/item_person_type_rv"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
/>
</LinearLayout>
主布局activity_tab_layoutdemo02.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabRippleColor="@null"
app:tabIndicatorColor="#2196F3"
app:tabIndicator="@drawable/shape_tablayout"
app:tabMode="scrollable"
/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
TabLayout下划线样式
在 drawable文件夹下新建shape_tablayout.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="20dp"
android:height="5dp"
android:gravity="center">
<shape android:shape="rectangle">
<corners android:radius="15dp" />
</shape>
</item>
</layer-list>
适配器TabLayout02FragmentAdapter.java
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import java.util.List;
public class TabLayout02FragmentAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
private List<String> title;
public TabLayout02FragmentAdapter(@NonNull FragmentManager fm, List<Fragment> fragmentList, List<String> title) {
super(fm);
this.fragmentList = fragmentList;
this.title = title;
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return title.get(position);
}
}
实现代码 TabLayoutdemo02.java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
public class TabLayoutdemo02 extends AppCompatActivity {
private TabLayout tab_layout;
private ViewPager view_pager;
private final List<String> titleName=new ArrayList<>();
private final List<Fragment> fragmentList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_layoutdemo02);
InitView();
setTab_layoutTitle();
setAdapter();
}
private void InitView() {
tab_layout = findViewById(R.id.tab_layout);
view_pager = findViewById(R.id.view_pager);
}
public void setTab_layoutTitle(){
String[] str={"动态","博客","分类专栏","收藏"};
for (int i = 0; i < str.length; i++) {
fragmentList.add(new TabLayout02Fragment(str[i]));
titleName.add(str[i]);
}
}
public void setAdapter(){
TabLayout02FragmentAdapter tabLayout02Adapter = new TabLayout02FragmentAdapter(getSupportFragmentManager(),
fragmentList, titleName);
view_pager.setAdapter(tabLayout02Adapter);
tab_layout.setupWithViewPager(view_pager);
}
}
以上都是一些常用控件的基础使用,就不多解释了