android 学习日记 smartLayout 底部选项卡

这篇android学习日记介绍了如何使用SmartLayout实现底部选项卡功能,通过导入依赖、编写布局样式文件、碎片文件以及activity主页面,实现简洁高效的代码实现。

android 学习日记 smartLayout 底部选项卡


前言

最近在做项目时,用的最原始的底部选项卡的代码,又臭又长,发现了一个比较节省代码的控件,再次分享一下。

一、使用步骤

大致分为以下步骤:
1.导入相应依赖
2.编写smartLayout用到的布局样式文件
3.编写碎片文件
4.编写activity主页面 确定数据源 --> 引用适配器 --> smartLayout装载适配器 

代码如下:

1.导入依赖

 //底部选项卡 依赖类库
    implementation 'com.ogaclejapan.smarttablayout:library:1.6.1@aar'
    implementation 'com.ogaclejapan.smarttablayout:utils-v4:1.6.1@aar'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

2、编写smartLayout用到的布局样式文件 smart_item_style.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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="match_parent">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="30dp"
        android:layout_height="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/tab_ic_home" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="商店"
        android:textColor="#6D6D6D"
        android:textSize="15sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

在这里插入图片描述
另外不要忘了写一个 点击时切换菜单时高亮显示的xml文件
需要两个图片文件,一个是点击时的图片样式 另一个是静态时的图片样式

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_home_normal" android:state_selected="true" />
    <item android:drawable="@drawable/ic_home_selected" android:state_selected="false" />
</selector>

再写一个变换图片下面的字体颜色的xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:color="@color/purple_200" />

    <item
        android:state_selected="false"
        android:color="@color/black" />
</selector>

在这里插入图片描述
在这里插入图片描述

3、编写碎片文件

按照项目需求来编写 ,需要几个就写几个 这里只给出一个示例
package com.example.practice.fragment;

import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.practice.R;

public class ShopFragment extends Fragment {

    View view ;

    @Override
    public View onCreateView(LayoutInflater inflater , ViewGroup Container , Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_shop_fragment,null);
        return view ;
    }
}

4、编写activity主页面

确定数据源 --> 引用适配器 --> smartLayout装载适配器 
    package com.example.practice;

    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.viewpager.widget.PagerAdapter;
    import androidx.viewpager.widget.ViewPager;

    import com.example.practice.fragment.ShopFragment;
    import com.ogaclejapan.smarttablayout.SmartTabLayout;
    import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
    import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;

    public class MainActivity extends AppCompatActivity {

        private ViewPager viewPager;
        private SmartTabLayout smartTabLayout;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        //获取上下文
            final LayoutInflater inflater = LayoutInflater.from(this);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            init();
            //确定数据源
            final int[] tabIcons = {R.drawable.tab_ic_home, R.drawable.tab_ic_orders, R.drawable.tab_ic_me};
            final String[] tabTitles = {"主页", "订单", "个人中心"};
            //添加view碎片
            FragmentPagerItems pages = FragmentPagerItems.with(this)
                    .add("主页", ShopFragment.class)
                    .add("订单", ShopFragment.class)
                    .add("我的", ShopFragment.class)
                    .create();
                    //添加适配器
            FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                    getSupportFragmentManager(),
                    pages);
                    //装配是哦欸其
            viewPager.setOffscreenPageLimit(pages.size());
            viewPager.setAdapter(adapter);
            //加载底部选项卡样式文件
            smartTabLayout.setCustomTabView(new SmartTabLayout.TabProvider() {
                @Override
                public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
                    View view = inflater.inflate(R.layout.smart_item_style, container, false);
                    ImageView iconView = (ImageView) view.findViewById(R.id.imageView);
                    iconView.setImageResource(tabIcons[position % tabIcons.length]);
                    TextView titleView = (TextView) view.findViewById(R.id.textView2);
                    titleView.setText(tabTitles[position % tabTitles.length]);
                    return view;
                }
            });
            //底部选项卡绑定关联的viewpager供滑动变换使用
            smartTabLayout.setViewPager(viewPager);
        }
        private void init() {
            viewPager = findViewById(R.id.viewpager);
            smartTabLayout = findViewById(R.id.smarttablayout);

        }
    }

主界面的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"
    tools:context=".ui.MainActivity">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/divider"
        />

    <com.ogaclejapan.smarttablayout.SmartTabLayout
        android:id="@+id/viewpager_tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/white"
        android:background="@color/dark_gray_bg"
        app:stl_distributeEvenly="true"
        app:stl_dividerColor="@color/transparent"
        app:stl_dividerThickness="0dp"
        app:stl_indicatorColor="@color/transparent"
        app:stl_indicatorThickness="0dp"
        app:stl_underlineColor="@color/transparent"
        app:stl_underlineThickness="0dp"
        />

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值