导航栏的学习

TabLayout

再布局中添加此布局
由于是design库中的所以要导入库

<android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

导入库

implementation 'com.android.support:design:28.0.0'

在java代码中

TabLayout tab = findViewById(R..id.tab_layout);
//添加tab item
tab.addTab(tab.newTab().setText("标题1"));
tab.addTab(tab.newTab().setText("标题2"))

在配置文件中设置去掉指示条

app:tabIndicatorHeight="0dp"

常用属性:

app:tabIndicatorColor :指示线的颜色
app:tabIndicatorHeight :指示线的高度
app:tabSelectedTextColor : tab选中时的字体颜色
app:tabMode="scrollable" : 默认是fixed,固定的;scrollable:可滚动的

自定义TabLayout

  TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.item_main_menu, null);
            // 使用自定义视图,目的是为了便于修改,也可使用自带的视图
            tab.setCustomView(view);

            TextView tvTitle = (TextView) view.findViewById(R.id.txt_tab);
            tvTitle.setText(tabTitlees[i]);
            ImageView imgTab = (ImageView) view.findViewById(R.id.img_tab);
            imgTab.setImageResource(tabImgs[i]);
            tabLayout.addTab(tab);

TabLayout监听事件

 //tab监听事件,当选中tab时候会调用此方法
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //选中此tab的时候触发
                viewPager.setCurrentItem(tab.getPosition(),false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //没有选中的时候触发
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //选中之后再次点击此事件的时候
                /**
                 *  刷新数据的时候需要再次点击或者多次点击事件
                 */
           }
        });

TabLayout的选择器的使用(selector)的使用
1、必须在drawable中创建的标签

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

/**
* 菜单图标
*/
private final int[] TAB_IMGS = new int[]{R.drawable.tab_main_msg_selector, R.drawable.tab_main_contact_selector, R.drawable.tab_main_find_selector
, R.drawable.tab_main_me_selector};
,tab_main_msg_selector.xml内容

ViewGroup禁止左右滑动
新建类继承ViewPager重写方法

package com.example.MyAdapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class OverPageAdapter extends ViewPager {

    private boolean link = true;

    public OverPageAdapter(@NonNull Context context) {
        super(context);
    }

    public OverPageAdapter(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(link){
            return false;
        }else {
            return super.onTouchEvent(ev);
        }
    }

    @Override
    public void scrollTo(int x, int y) {
        super.scrollTo(x, y);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(link){
            return false;
        }else {
            return super.onInterceptTouchEvent(ev);
        }
    }

    /**
     *  重写setCurrentItem为了取消点击的效果
     * @param item
     */
    @Override
    public void setCurrentItem(int item) {
        super.setCurrentItem(item);
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        super.setCurrentItem(item, smoothScroll);
    }
}

在布局文件中
文件的全路径

        <com.example.MyAdapters.OverPageAdapter
            android:id="@+id/nav_viewpager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </com.example.MyAdapters.OverPageAdapter>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值