欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SmartTabLayout的简单使用

程序员文章站 2024-03-22 16:36:28
...

SmartTabLayout的简单使用

效果如图所示:

SmartTabLayout的简单使用

代码所做的效果:

SmartTabLayout的简单使用


SmartTabLayout下载地址

https://search.maven.org/到该网站可以下载很多arr或jar包

SmartTabLayout的简单使用

代码

第一步:到GitHub上面找的SmartTabLayout框架的library.aar  添加到libs文件夹下面

第二步:在build.gradle下面添加扫描libs文件夹

        

 //意思是他他回去libs里面加载一些arr包
    repositories {
        flatDir{
            dirs 'libs'
        }
    }

第三步:在build.gradle文件dependencies{ implementation(name: 'library',ext: 'aar') }里面添加一句意思就是依赖该包

第四步:创建一个include_tab.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<com.ogaclejapan.smarttablayout.SmartTabLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="40dp"

    android:id="@+id/smart_tab"
    app:stl_customTabTextLayoutId="@layout/include_title_tab"
    app:stl_customTabTextViewId="@+id/text_4"
    app:stl_indicatorColor="@android:color/holo_red_dark"
    app:stl_indicatorInterpolation="linear"
    app:stl_indicatorThickness="3dp"
    app:stl_underlineThickness="1dp"
    >

</com.ogaclejapan.smarttablayout.SmartTabLayout>

SmartTabLayout的简单使用

第五步:对称创建布局文件如include_title_tab.xml   标题文本

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text_4"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:paddingLeft="24dp"
    android:paddingRight="24dp"
    android:textColor="@color/tab_color"
    android:textSize="16sp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"

    >

</TextView>

第六步:开始写java 代码   注意:我的是在一个FragmentLayout里面写的  自己发挥

NewFragment类
package netease.li.com.wangyiyun.news.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.ogaclejapan.smarttablayout.SmartTabLayout;

import java.util.ArrayList;

import netease.li.com.wangyiyun.R;
import netease.li.com.wangyiyun.news.adapter.NewsAdapter;
import netease.li.com.wangyiyun.news.bean.FragmentInfo;


public class NewFragment extends Fragment {
    ArrayList<FragmentInfo> pages;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_news,container,false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FrameLayout frameLayou = getActivity().findViewById(R.id.tabs);
        pages=new ArrayList<>();
        frameLayou.addView(View.inflate(getActivity(),R.layout.include_tab,null));//绑定数据
        SmartTabLayout smartTabLayout = getActivity().findViewById(R.id.smart_tab);//找到SmartTabLayout类

        ViewPager viewPager = getActivity().findViewById(R.id.viewPageId);

        smartTabLayout.setViewPager(viewPager);//viewPager和SmartTabLayout进行绑定

        String[] title=getResources().getStringArray(R.array.title_name);

        for (int i=0;i<title.length;i++){
            FragmentInfo titleBean;
            if (i==0){
                titleBean=new FragmentInfo(new HotFragment(),title[i]);
            }else{
                titleBean=new FragmentInfo(new EmptyFragment(),title[i]);
            }
            pages.add(titleBean);
        }
        NewsAdapter adapter = new NewsAdapter(getActivity().getSupportFragmentManager(),pages);

        viewPager.setAdapter(adapter);

        //!!!!关键代码,自动绑定数据
        smartTabLayout.setViewPager(viewPager);
    }
}

NewsAdapter类
package netease.li.com.wangyiyun.news.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import java.util.ArrayList;

import netease.li.com.wangyiyun.news.bean.FragmentInfo;

public class NewsAdapter extends FragmentStatePagerAdapter{
    //FragmentStatePagerAdapter
    // FragmentPagerAdapter
    //两者的区别在于一个状态FragmentStatePagerAdapter表示只创建一个PageView
    // FragmentPagerAdapter一次性全部创建
    ArrayList<FragmentInfo> mFragments;
    public NewsAdapter(FragmentManager fm, ArrayList<FragmentInfo> fragments) {
        super(fm);
        this.mFragments=fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position).getmFragment();
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }
    //返回页面标题
    @Override
    public CharSequence getPageTitle(int position) {
        return mFragments.get(position).getmTitle();
    }
}


EmptyFragment类
package netease.li.com.wangyiyun.news.fragment;

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 netease.li.com.wangyiyun.R;

public class EmptyFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_empty,container,false);
        return view;
    }
}

HotFragment类
package netease.li.com.wangyiyun.news.fragment;

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.ListView;

import netease.li.com.wangyiyun.R;

public class HotFragment extends Fragment{

    private ListView mListView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.hot_fragment,container,false);
        mListView = view.findViewById(R.id.newListViewId);//表示站一个位置
        return view;
    }
}

注意:还有一些布局文件没有创建可以自己创建