tablayout增長選擇tab 的事件和重寫tab點擊事件

tablayout在點擊或者滑動的時候會觸發監聽事件 , 當你調用這個方法的時候會觸發事件:android

mTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        Toast.makeText(mContext, "選中的"+tab.getText(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

        Toast.makeText(mContext, "未選中的"+tab.getText(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

        Toast.makeText(mContext, "複選的"+tab.getText(), Toast.LENGTH_SHORT).show();

    }
});

複製代碼

tablayout重寫點擊事件api

從新設置點擊事件:

 

for (int i = 0; i < mTabLayout.getTabCount(); i++) {                                                           
    TabLayout.Tab tab = mTabLayout.getTabAt(i);
    if (tab != null) {
        tab.setCustomView(getTabView(i)); //對應tab定製view
        if (tab.getCustomView() != null) {
            View tabView = (View) tab.getCustomView().getParent(); //得到tab對應父item
            tabView.setTag(i); // 父item放入標記值
            tabView.setOnClickListener(mTabOnClickListener); //設置點擊事件
        }
    }
}
//自定義的TabView,佈局和圖標換成你想要的便可
app:tabBackground="@drawable/tab_bg_selector"


public View getTabView(int position) {
    //得到view
View v = LayoutInflater.from(ParentEditorActivity.this).inflate(R.layout.go, null);
    ImageView img = (ImageView) v.findViewById(R.id.icon_item);
    img.setImageResource(imageViewRes[position]);
    return v;
}

處理點擊事件

private View.OnClickListener mTabOnClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int pos = (int) view.getTag();
        Log.d(TAG, "onClick: mviewpager?" + mViewpager.getVisibility());
        if (mViewpager.getVisibility() == View.INVISIBLE) {
            mViewpager.setVisibility(VISIBLE);
            AnimUtils.stickerExitAnim(mViewpager, mTabLayout, false, getClickAnimListener(false));
        }
        TabLayout.Tab tab = mTabLayout.getTabAt(pos);
        if (tab != null) {
            tab.select();
        }
    }
};


XML佈局文件:
當咱們自定義tabview的時候tabview的background會發現tabview不能 佔據整個tab
須要使用api:
app:tabBackground="@drawable/tab_bg_selector"
設置整個tab的背景色

//getTabView所須要的渲染文件,自定義便可
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:id="@+id/ll_item"
              android:background="@drawable/tab_bg_selector"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ImageView
        android:id="@+id/icon_item"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

</LinearLayout>
//background的select文件(在這個Demo裏面須要,能夠不寫)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">

    <item android:drawable="@color/cpb_green" android:state_selected="true"/>   //選中狀態
    <item android:drawable="@color/cpb_green" android:state_pressed="true"/>    //按下狀態
    <item android:drawable="@drawable/md_transparent"/>     //平時狀態

</selector>
複製代碼
相關文章
相關標籤/搜索