Fragment+SmartTable+SmartRefreshLayout

在build.gradle(APP)中添加依赖包
implementation ‘com.android.support:support-v4:28.0.+’
implementation ‘com.github.huangyanbin:SmartTable:2.0’ (表格)
implementation ‘com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14’ (刷新)
implementation ‘com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-14’ (特殊头部刷新)

在另外一个build.gradle中添加如下内容

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven{ url 'http://central.maven.org/maven2/'}
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
        mavenCentral();
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

ps:代码里基本上都有注释,我就不在文章中再多说了

创建三个布局文件(home_fragment.xml,info_fragment,my_fragment)

home_fragment.xml




    

        

        

            

            

                

                    

                    
                

                

                    

                    
                


                

                    

                    
                

                

                    

                    
                

            

        
        
    


home_fragment.xml



    

        

    


my_fragment.xml




    

        

        
    

创建两个Activity(BaseActivity,MainActivity)

public abstract class BaseActivity extends FragmentActivity {
    protected BaseActivity act;

    /*
    *   Class.getName():以String的形式,返回Class对象的“实体”名称;
        Class.getSimpleName():获取源代码中给出的“底层类”简称。
    * */
    protected final String TAG=getClass().getSimpleName();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        act=this;
        setContentView(getLayoutID());
        initView();
        initData();
        initListener();

    }
    @LayoutRes
    protected abstract int getLayoutID();
    protected abstract void initListener();
    protected abstract void initView();
    protected abstract void initData();

    /*
    @SuppressWarnings("unchecked") 告诉编译器忽略 unchecked 警告信息
    * */
    @SuppressWarnings("unchecked")

    //通过泛型来简化findViewById
    protected E f(int id){
        return (E)findViewById(id);
    }
}

MainActivity

public class MainActivity extends BaseActivity {
    private ViewPager vp;
    private RadioGroup rg;
    //将id定义成整型数组
    private int[] rbs = {R.id.rb_home,R.id.rb_info,R.id.rb_my};

    //定义一个fragment集合
    private List mFragments;


    @Override
    protected int getLayoutID(){
        return R.layout.activity_main;
    }

    @Override
    protected void initView(){
        vp=f(R.id.vp);
        rg=f(R.id.rg);
    }

    @Override
    protected void initData(){
        mFragments=new ArrayList<>();
        HomeFragment home = new HomeFragment();
        InfoFragment info = new InfoFragment();
        MyFragment my = new MyFragment();
        mFragments.add(home);
        mFragments.add(info);
        mFragments.add(my);

        //设置填充器
        vp.setAdapter(new PagerMainAdapter(getSupportFragmentManager(),mFragments));
        //设置缓存页面数
        vp.setOffscreenPageLimit(3);
    }

    @Override
    protected void initListener() {
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                for (int i = 0;i

PagerMainAdapter(自定义适配器)

public class PagerMainAdapter extends FragmentPagerAdapter {
    private final List frags;

    public PagerMainAdapter(FragmentManager fm,List frags){
        super(fm);
        this.frags=frags;
    }

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


    @Override
    public int getCount() {
        return frags.size();
    }
}

三个fragment(代码都一样)
HomeFragment

public class HomeFragment extends Fragment {

    private View view;
    /*
        在写程序的时候你可以定义是否可为空指针。通过使用像@NotNull和@Nullable之类的annotation来声明一个方法是否是空指针安全的。
        如果可以传入NULL值,则标记为@Nullable,如果不可以,则标注为@Nonnull
    * */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.home_fragment,container,false);
        return view;
    }

InfoFragment

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

    }
}

MyFragment

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

现在就可以滑动fragment了

SmartTable表格

table.xml




    

        

        
    


TableActivity

public class Table extends AppCompatActivity {
    private SmartTable table;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table);
        setStyle();
    }

    private void setStyle(){

        //普通列
        Column id = new Column<>("无人机编号", "id");
        Column longitude = new Column<>("经度", "longitude");
        Column latitude = new Column<>("纬度", "latitude");
        Column altitude = new Column<>("高度", "altitude");
        Column status = new Column<>("状态", "status");
        Column speed = new Column<>("飞行速度", "speed");
        Column scope = new Column<>("飞行范围", "scope");
        Column time = new Column<>("入侵时间", "time");
        //设置该列当字段相同时自动合并
        //id.setAutoMerge(true);

        ArrayList list = new ArrayList<>();
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));
        list.add(new UserInfo("001",100, 150, 50, "已捕获", "100m/s", "100-200", "12:00:00"));

        //表格数据 datas 是需要填充的数据
        PageTableData pageTableData = new PageTableData<>("无人机信息表", list, id, longitude, latitude, altitude, status, speed, scope, time);
        //设置每页的行数
        pageTableData.setPageSize(10);

        //设置数据
        table = findViewById(R.id.table);
        table.setTableData(pageTableData);

        //设置是否显示顶部序列号,默认显示ABCD...
        table.getConfig().setShowXSequence(false);

        //设置是否显示左侧序列号(行号)
        table.getConfig().setShowYSequence(true);

        //去除标题
        /*table.getConfig().setShowTableTitle(false);*/
        //标题样式
        table.getConfig().setTableTitleStyle(new FontStyle(50, Color.BLACK));



        /*设置字体和字体颜色*/
        table.getConfig().setContentStyle(new FontStyle(50, Color.BLACK));

        //设置表格偶数行的背景色
        ICellBackgroundFormat backgroundFormat = new BaseCellBackgroundFormat() {
            @Override
            public int getBackGroundColor(CellInfo cellInfo) {
                if (cellInfo.row % 2 == 0){
                    return ContextCompat.getColor(getApplicationContext(),R.color.ou);
                }
                return TableConfig.INVALID_COLOR;
            }
        };


        //设置行号的颜色
        ICellBackgroundFormat backgroundFormat1 = new BaseCellBackgroundFormat() {
            @Override
            public int getBackGroundColor(Integer position) {
                if (position % 2 == 0){
                    return ContextCompat.getColor(getApplicationContext(),R.color.line);
                }
                return TableConfig.INVALID_COLOR;
            }

            //设置行号的字体颜色
            @Override
            public int getTextColor(Integer position) {
                if (position%2==0){
                    return ContextCompat.getColor(getApplicationContext(),R.color.colorAccent);
                }
                return TableConfig.INVALID_COLOR;
            }
        };

        table.getConfig().setContentCellBackgroundFormat(backgroundFormat).setYSequenceCellBgFormat(backgroundFormat1);



        //固定指定列
        id.setFixed(true);

        //Y,X,列标题,统计行序号列
        table.getConfig().setFixedYSequence(true);
        table.getConfig().setFixedYSequence(true);
        table.getConfig().setFixedCountRow(true);
        table.getConfig().setFixedTitle(true);

        //表格放大缩小
        table.setZoom(true);

        FontStyle fontStyle = new FontStyle();
        
        table.setSortColumn(id,true);


        //点击事件
        table.setOnColumnClickListener(new OnColumnClickListener() {
            @Override
            public void onClick(ColumnInfo columnInfo) {

            }
        });

    }
}

最后加一个java bean就ok了
userInfo.java

public class UserInfo {

    private String id;
    private double longitude;
    private double latitude;
    private double altitude;
    private String status;
    private String speed;
    private String scope;
    private String time;

    public UserInfo(String id, double longitude, double latitude, double altitude, String status, String speed, String scope, String time) {
        this.id = id;
        this.longitude = longitude;
        this.latitude = latitude;
        this.altitude = altitude;
        this.status = status;
        this.speed = speed;
        this.scope = scope;
        this.time = time;

    }
    
}

最好运行的效果如下:

切换fragment时,底部图片跟随变色
在drawable里新建三个资源文件,这里我就只示范了一个,另外两个相同

//一个图片是被选中的状态,一个图片是未选中的

 
    
        
        
    

有兴趣的朋友可以直接去本人的github上或码云上下载源码
https://github.com/yym212/warehouse.git
https://gitee.com/yym0212/fragment.git

另外特别感谢以下博主的文章,建议大家也可以去看看
https://blog.csdn.net/juer2017/article/details/77990215

https://www.jianshu.com/p/09a763213817

https://www.meiwen.com.cn/subject/hjvqgftx.html

你可能感兴趣的:(Fragment+SmartTable+SmartRefreshLayout)