Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可通过Drawable设置Tab的图片 #238

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions FlycoTabLayout_Lib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
apply plugin: 'com.android.library'
//apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'maven-publish'
//apply plugin: 'com.jfrog.bintray'

// 这个version是区分library版本的,因此当我们需要更新library时记得修改这个version
version = "2.1.2"
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
compileSdkVersion 30

defaultConfig {
minSdkVersion 11
targetSdkVersion 25
targetSdkVersion 30
versionCode 212
versionName version
}
Expand All @@ -23,13 +22,26 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:25.1.0'
api fileTree(dir: 'libs', include: ['*.jar'])
api 'androidx.legacy:legacy-support-v4:1.0.0'
}

group = "com.github.lizhongkan" // Maven Group ID for the artifact,一般填你唯一的包名
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
groupId = 'com.github.lizhongkan'
artifactId = 'FlycoTableLayout'
version = '2.1.5'
}
}
}
}

//def siteUrl = 'https://github.com/H07000223' // 项目的主页
//def gitUrl = 'https://github.com/H07000223' // Git仓库的url
//group = "com.flyco.tablayout" // Maven Group ID for the artifact,一般填你唯一的包名
//install {
// repositories.mavenInstaller {
// // This generates POM.xml with proper parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.util.TypedValue;
Expand All @@ -34,6 +33,9 @@

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

/** 没有继承HorizontalScrollView不能滑动,对于ViewPager无依赖 */
public class CommonTabLayout extends FrameLayout implements ValueAnimator.AnimatorUpdateListener {
private Context mContext;
Expand Down Expand Up @@ -235,7 +237,7 @@ private void addTab(final int position, View tabView) {
TextView tv_tab_title = (TextView) tabView.findViewById(R.id.tv_tab_title);
tv_tab_title.setText(mTabEntitys.get(position).getTabTitle());
ImageView iv_tab_icon = (ImageView) tabView.findViewById(R.id.iv_tab_icon);
iv_tab_icon.setImageResource(mTabEntitys.get(position).getTabUnselectedIcon());
updateTabIcon(iv_tab_icon, mTabEntitys.get(position), false);

tabView.setOnClickListener(new OnClickListener() {
@Override
Expand Down Expand Up @@ -286,7 +288,7 @@ private void updateTabStyles() {
if (mIconVisible) {
iv_tab_icon.setVisibility(View.VISIBLE);
CustomTabEntity tabEntity = mTabEntitys.get(i);
iv_tab_icon.setImageResource(i == mCurrentTab ? tabEntity.getTabSelectedIcon() : tabEntity.getTabUnselectedIcon());
updateTabIcon(iv_tab_icon, tabEntity, i == mCurrentTab);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
mIconWidth <= 0 ? LinearLayout.LayoutParams.WRAP_CONTENT : (int) mIconWidth,
mIconHeight <= 0 ? LinearLayout.LayoutParams.WRAP_CONTENT : (int) mIconHeight);
Expand Down Expand Up @@ -315,13 +317,23 @@ private void updateTabSelection(int position) {
tab_title.setTextColor(isSelect ? mTextSelectColor : mTextUnselectColor);
ImageView iv_tab_icon = (ImageView) tabView.findViewById(R.id.iv_tab_icon);
CustomTabEntity tabEntity = mTabEntitys.get(i);
iv_tab_icon.setImageResource(isSelect ? tabEntity.getTabSelectedIcon() : tabEntity.getTabUnselectedIcon());
updateTabIcon(iv_tab_icon, tabEntity, isSelect);
if (mTextBold == TEXT_BOLD_WHEN_SELECT) {
tab_title.getPaint().setFakeBoldText(isSelect);
}
}
}

protected void updateTabIcon(ImageView tabIcon, CustomTabEntity tabEntity, boolean isSelected) {
Drawable d = isSelected ? tabEntity.getTabSelectedDrawable() : tabEntity.getTabUnselectedDrawable();
if (d != null) {
tabIcon.setImageDrawable(d);
} else {
int resId = isSelected ? tabEntity.getTabSelectedIcon() : tabEntity.getTabUnselectedIcon();
tabIcon.setImageResource(resId);
}
}

private void calcOffset() {
final View currentTabView = mTabsContainer.getChildAt(this.mCurrentTab);
mCurrentP.left = currentTabView.getLeft();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.util.TypedValue;
Expand All @@ -30,6 +28,9 @@

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

public class SegmentTabLayout extends FrameLayout implements ValueAnimator.AnimatorUpdateListener {
private Context mContext;
private String[] mTitles;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.util.TypedValue;
Expand All @@ -33,6 +27,13 @@
import java.util.ArrayList;
import java.util.Collections;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

/** 滑动TabLayout,对于ViewPager的依赖性强 */
public class SlidingTabLayout extends HorizontalScrollView implements ViewPager.OnPageChangeListener {
private Context mContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.flyco.tablayout.listener;

import android.support.annotation.DrawableRes;
import android.graphics.drawable.Drawable;
import androidx.annotation.DrawableRes;

public interface CustomTabEntity {
String getTabTitle();
Expand All @@ -10,4 +11,7 @@ public interface CustomTabEntity {

@DrawableRes
int getTabUnselectedIcon();

Drawable getTabSelectedDrawable();
Drawable getTabUnselectedDrawable();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.flyco.tablayout.listener;

import android.graphics.drawable.Drawable;

/**
* Created by lizk on 2017/11/1.
*/

public class TabEntity implements CustomTabEntity {

private String tabTitle;
private Drawable selectedDrawable;
private Drawable unselectedDrawable;
private int selectedIcon;
private int unSelectedIcon;

public TabEntity(String tabTitle, Drawable selectedDrawable, Drawable unselectedDrawable) {
this.tabTitle = tabTitle;
this.selectedDrawable = selectedDrawable;
this.unselectedDrawable = unselectedDrawable;
}

public TabEntity(String tabTitle, int selectedIcon, int unselectedIcon) {
this.tabTitle = tabTitle;
this.selectedIcon = selectedIcon;
this.unSelectedIcon = unselectedIcon;
}

@Override
public String getTabTitle() {
return tabTitle;
}

@Override
public int getTabSelectedIcon() {
return selectedIcon;
}

@Override
public int getTabUnselectedIcon() {
return unSelectedIcon;
}

@Override
public Drawable getTabSelectedDrawable() {
return selectedDrawable;
}

@Override
public Drawable getTabUnselectedDrawable() {
return unselectedDrawable;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.flyco.tablayout.utils;

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

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class FragmentChangeManager {
private FragmentManager mFragmentManager;
private int mContainerViewId;
Expand Down
15 changes: 7 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 30

defaultConfig {
applicationId "com.flyco.tablayoutsamples"
minSdkVersion 11
targetSdkVersion 25
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
}
Expand All @@ -20,12 +19,12 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.1.0'
// compile project(':FlycoTabLayout_Lib')
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:25.1.0'
implementation project(':FlycoTabLayout_Lib')

//--->support 3.0+
compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'
// compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'
//--->support 2.2+
// compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.0@aar'
// compile 'com.nineoldandroids:library:2.4.0'
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/flyco/tablayoutsamples/entity/TabEntity.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package com.flyco.tablayoutsamples.entity;

import android.graphics.drawable.Drawable;

import com.flyco.tablayout.listener.CustomTabEntity;

public class TabEntity implements CustomTabEntity {
public String title;
public int selectedIcon;
public int unSelectedIcon;

public Drawable selectedDrawable;
public Drawable unselectedDrawable;

public TabEntity(String title, int selectedIcon, int unSelectedIcon) {
this.title = title;
this.selectedIcon = selectedIcon;
this.unSelectedIcon = unSelectedIcon;
}

public TabEntity(String title, Drawable selectedDrawable, Drawable unselectedDrawable) {
this.title = title;
this.selectedDrawable = selectedDrawable;
this.unselectedDrawable= unselectedDrawable;
}

@Override
public String getTabTitle() {
return title;
Expand All @@ -27,4 +38,15 @@ public int getTabSelectedIcon() {
public int getTabUnselectedIcon() {
return unSelectedIcon;
}

@Override
public Drawable getTabSelectedDrawable() {
return selectedDrawable;
}

@Override
public Drawable getTabUnselectedDrawable() {
return unselectedDrawable;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.flyco.tablayout.CommonTabLayout;
Expand All @@ -22,6 +17,12 @@
import java.util.ArrayList;
import java.util.Random;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class CommonTabActivity extends AppCompatActivity {
private Context mContext = this;
private ArrayList<Fragment> mFragments = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.flyco.tablayout.SegmentTabLayout;
Expand All @@ -17,6 +12,12 @@

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class SegmentTabActivity extends AppCompatActivity {
private ArrayList<Fragment> mFragments = new ArrayList<>();
private ArrayList<Fragment> mFragments2 = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.flyco.tablayoutsamples.R;

import androidx.fragment.app.Fragment;

@SuppressLint("ValidFragment")
public class SimpleCardFragment extends Fragment {
private String mTitle;
Expand Down
Loading