From 022c7991d776441c85f8d2708538b2e49fe8a986 Mon Sep 17 00:00:00 2001 From: chaychan <844738237@qq.com> Date: Fri, 23 Jun 2017 11:54:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0bottomBarLayout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 4 +- .../activity/BottomBarLayoutDemoActivity.java | 87 +++++++++ .../activity/MainActivity.java | 3 +- .../fragment/TabFragment.java | 33 ++++ .../activity_bottom_bar_layout_demo.xml | 82 ++++++++ .../res/mipmap-xxhdpi/tab_home_normal.png | Bin 0 -> 717 bytes .../res/mipmap-xxhdpi/tab_home_selected.png | Bin 0 -> 525 bytes .../main/res/mipmap-xxhdpi/tab_me_normal.png | Bin 0 -> 877 bytes .../res/mipmap-xxhdpi/tab_me_selected.png | Bin 0 -> 646 bytes .../res/mipmap-xxhdpi/tab_micro_normal.png | Bin 0 -> 891 bytes .../res/mipmap-xxhdpi/tab_micro_selected.png | Bin 0 -> 656 bytes .../res/mipmap-xxhdpi/tab_video_normal.png | Bin 0 -> 582 bytes .../res/mipmap-xxhdpi/tab_video_selected.png | Bin 0 -> 675 bytes app/src/main/res/values/colors.xml | 5 + .../bottombarlayout/BottomBarItem.java | 117 +++++++++++ .../bottombarlayout/BottomBarLayout.java | 181 ++++++++++++++++++ .../src/main/res/layout/item_bottom_bar.xml | 23 +++ viewlib/src/main/res/values/attr.xml | 11 ++ 18 files changed, 544 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/chaychan/powerfulviewlibrary/activity/BottomBarLayoutDemoActivity.java create mode 100644 app/src/main/java/com/chaychan/powerfulviewlibrary/fragment/TabFragment.java create mode 100644 app/src/main/res/layout/activity_bottom_bar_layout_demo.xml create mode 100644 app/src/main/res/mipmap-xxhdpi/tab_home_normal.png create mode 100644 app/src/main/res/mipmap-xxhdpi/tab_home_selected.png create mode 100644 app/src/main/res/mipmap-xxhdpi/tab_me_normal.png create mode 100644 app/src/main/res/mipmap-xxhdpi/tab_me_selected.png create mode 100644 app/src/main/res/mipmap-xxhdpi/tab_micro_normal.png create mode 100644 app/src/main/res/mipmap-xxhdpi/tab_micro_selected.png create mode 100644 app/src/main/res/mipmap-xxhdpi/tab_video_normal.png create mode 100644 app/src/main/res/mipmap-xxhdpi/tab_video_selected.png create mode 100644 viewlib/src/main/java/com/chaychan/viewlib/bottombarlayout/BottomBarItem.java create mode 100644 viewlib/src/main/java/com/chaychan/viewlib/bottombarlayout/BottomBarLayout.java create mode 100644 viewlib/src/main/res/layout/item_bottom_bar.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4688709..b87ca63 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -28,9 +28,11 @@ - + + + \ No newline at end of file diff --git a/app/src/main/java/com/chaychan/powerfulviewlibrary/activity/BottomBarLayoutDemoActivity.java b/app/src/main/java/com/chaychan/powerfulviewlibrary/activity/BottomBarLayoutDemoActivity.java new file mode 100644 index 0000000..06138f6 --- /dev/null +++ b/app/src/main/java/com/chaychan/powerfulviewlibrary/activity/BottomBarLayoutDemoActivity.java @@ -0,0 +1,87 @@ +package com.chaychan.powerfulviewlibrary.activity; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.view.ViewPager; + +import com.chaychan.powerfulviewlibrary.R; +import com.chaychan.powerfulviewlibrary.fragment.TabFragment; +import com.chaychan.viewlib.bottombarlayout.BottomBarLayout; + +import java.util.ArrayList; +import java.util.List; + +import butterknife.Bind; +import butterknife.ButterKnife; + +public class BottomBarLayoutDemoActivity extends FragmentActivity { + + @Bind(R.id.vp_content) + ViewPager vpContent; + @Bind(R.id.bbl) + BottomBarLayout bbl; + + private List mFragmentList = new ArrayList<>(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_bottom_bar_layout_demo); + ButterKnife.bind(this); + + initData(); + initListener(); + } + + private void initData() { + + TabFragment homeFragment = new TabFragment(); + Bundle bundle1 = new Bundle(); + bundle1.putString(TabFragment.CONTENT,"首页"); + homeFragment.setArguments(bundle1); + mFragmentList.add(homeFragment); + + TabFragment videoFragment = new TabFragment(); + Bundle bundle2 = new Bundle(); + bundle2.putString(TabFragment.CONTENT,"视频"); + videoFragment.setArguments(bundle2); + mFragmentList.add(videoFragment); + + TabFragment microFragment = new TabFragment(); + Bundle bundle3 = new Bundle(); + bundle3.putString(TabFragment.CONTENT,"微头条"); + microFragment.setArguments(bundle3); + mFragmentList.add(microFragment); + + TabFragment meFragment = new TabFragment(); + Bundle bundle4 = new Bundle(); + bundle4.putString(TabFragment.CONTENT,"我的"); + meFragment.setArguments(bundle4); + mFragmentList.add(meFragment); + } + + private void initListener() { + vpContent.setAdapter(new MyAdapter(getSupportFragmentManager())); + bbl.setmViewPager(vpContent); + } + + class MyAdapter extends FragmentStatePagerAdapter{ + + public MyAdapter(FragmentManager fm) { + super(fm); + } + + @Override + public Fragment getItem(int position) { + return mFragmentList.get(position); + } + + @Override + public int getCount() { + return mFragmentList.size(); + } + } +} diff --git a/app/src/main/java/com/chaychan/powerfulviewlibrary/activity/MainActivity.java b/app/src/main/java/com/chaychan/powerfulviewlibrary/activity/MainActivity.java index f10cfa3..1322c2f 100644 --- a/app/src/main/java/com/chaychan/powerfulviewlibrary/activity/MainActivity.java +++ b/app/src/main/java/com/chaychan/powerfulviewlibrary/activity/MainActivity.java @@ -16,7 +16,8 @@ public class MainActivity extends ListActivity { new DemoBean("ExpandableTextViewDemo",ExpandableTextViewDemoActivity.class), new DemoBean("PieChartViewDemo",PieChartViewDemoActivity.class), new DemoBean("RunningTextViewDemo",RunningTextViewDemoActivity.class), - new DemoBean("ExpandableLinearLayoutDemo",ExpandableLinearLayoutChooseActivity.class) + new DemoBean("ExpandableLinearLayoutDemo",ExpandableLinearLayoutChooseActivity.class), + new DemoBean("BottomBarLayoutDemoActivity",BottomBarLayoutDemoActivity.class) }; @Override diff --git a/app/src/main/java/com/chaychan/powerfulviewlibrary/fragment/TabFragment.java b/app/src/main/java/com/chaychan/powerfulviewlibrary/fragment/TabFragment.java new file mode 100644 index 0000000..6b2378c --- /dev/null +++ b/app/src/main/java/com/chaychan/powerfulviewlibrary/fragment/TabFragment.java @@ -0,0 +1,33 @@ +package com.chaychan.powerfulviewlibrary.fragment; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +/** + * @author ChayChan + * @date 2017/6/23 11:22 + */ +public class TabFragment extends Fragment { + + public static final String CONTENT = "content"; + private TextView mTextView; + + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle bundle) { + mTextView = new TextView(getActivity()); + mTextView.setGravity(Gravity.CENTER); + String content = getArguments().getString(CONTENT); + mTextView.setText(content); + return mTextView; + } + + +} diff --git a/app/src/main/res/layout/activity_bottom_bar_layout_demo.xml b/app/src/main/res/layout/activity_bottom_bar_layout_demo.xml new file mode 100644 index 0000000..536c9b8 --- /dev/null +++ b/app/src/main/res/layout/activity_bottom_bar_layout_demo.xml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/mipmap-xxhdpi/tab_home_normal.png b/app/src/main/res/mipmap-xxhdpi/tab_home_normal.png new file mode 100644 index 0000000000000000000000000000000000000000..f1713c779a403cfbd7ba53e44c29e8b64569c528 GIT binary patch literal 717 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD3?#3*wSy$81AIbUfpowCfpD?@(?IJrOM?7@ z88|xMeVoiDbo9UKo!>v~Vzii=#TTtka(Ph0xnKE4+N*HOvpj`6CISZZz*6eoE51f z)_pW-;|bGEb3Y!D)%$mad5XEL*1sIS2kZ==ZyJ2uFT?Qs^?NIqKdxWj9NNUP&wYae zzy9>?Z`!ZwZ=AB_<6TjQi+Po}p#>*w`j&G(NpJL|suIk{FcY^cR)Y>`E_?F3E zQ`AuSC(G4_?S%E=kc^KP4}_F--*4;ZQ;VwcNT}-MXY_gh;U)Lwhq`uBGx)ar_|m9( zc+Rwk>6X0ScT#2qH%j(iSstR%a?<>@fbf!a&J(Ac*qXaV==Ss%pN@;nh*;b@)55bM`y#aHg26c zj$U~UwHJ4nWjuX(I?A5;&BEqnQ<_CE0J_wwlT-4ki*sPj!fDMTD@}`J0|Jn4p zm*+!p{pBBJHp~{^t=S*k>F94iwsT@5Z&mI6>2so{&6sNzy7KOx^|GJ&8p}GjZ#^UP xX356uKPxTVx-=@keVDv7u?8GD5x#$!Zse$$T;J6+6PPde}&8r2<;R1Ut(q~*ScyRV-rxXJNqlKr7V@Srm zw=?b*H5mxFwN81U?lHlh)urP9@-3G)ZsGFqFuUCSZs)l-GYiiNnD3T7Y`}Ba;7P^4 z9~?_||9mJnU&bl?^J0nkBNJ!;bZ=yT^dhNlk-;RjFQ02|3YFyajrUBLcuYjjpwVcV z#&I6YNT1dNI;oCR{JU5)lB^!`eltADmA_J@)^3X7+~PHp=IegaT-aFYGhHNVi8%Yu z8^^ay>(BKmZT2-Wt+g{V+^T98yzR@+8OKUm6~oW2-)|(Uu~&Lgv*EsJ3TEPWwy-?= ztQf<6fJy8Rv*UH|o8|9V`rj8m@(QS(ze0xdnD*)dl@G^CZ%$0OEGV|2fmP>+apNT+ zp4b1m#pB!svN@ZU{{HmbBYA7?we3Gu-&~*VXuY55t?gM};~D?F7FAbGezEyssc&HO iddvBV5^aeyG|M+A#fLBZsHqAJa0X9TKbLh*2~7avqwiG! literal 0 HcmV?d00001 diff --git a/app/src/main/res/mipmap-xxhdpi/tab_me_normal.png b/app/src/main/res/mipmap-xxhdpi/tab_me_normal.png new file mode 100644 index 0000000000000000000000000000000000000000..ce73b0f07951fc2c72a105280bbf821f9bdd19ec GIT binary patch literal 877 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD3?#3*wSy$;0(?STfpoyYf$0*P6@f0$Dhcun zW{}ERaq0d4*NZX?xBOYlIje_N^vtnBTMPbu*K2mfK1*%Z_^#*UtQhw3j$!r>Wd;VO z5>FS$kc@k8BW_M>GT@o2yMT#FxqDH^j&uM2KUh-Q$G+uikTHL;-+SraJ09Okj0_FH zU_ooD^*Z(kyANLZ#r*50O^fxR3^yNn{e%t!PtBj3_1$)z>DK96YZU)!c~9T|qKO4p zH=oLBWq;k}Ey&^ zN%~7V+bs7j&=j4xg+H)_<;-=rIc)o`ST8O9Y3sl>Raiy$5DOSN{rM z*uu=x=NRw5t;5j%nM<&1=TnV}FFaB!ZU>*$<&096sBwP9;A{PR>Fo`H8^tH3o7u2U z*{py0!rjfUijKOsm;CaL{&IM0c*5aXRrVTHcUe_dewS?HQv2`E$X5}&zI)!vpZpRt zPTxy-E4IXN%Tw>mdpFK4oWfWxC44b@?fL8b?se3rNFDIj;0d*;spU0ip8aO=?aY#2 zSIlbmvi$$KKPvaI>$@=f#h34X?@bopf9c+hs&gH0pRE-=HT&zopr0JsKu A5dZ)H literal 0 HcmV?d00001 diff --git a/app/src/main/res/mipmap-xxhdpi/tab_me_selected.png b/app/src/main/res/mipmap-xxhdpi/tab_me_selected.png new file mode 100644 index 0000000000000000000000000000000000000000..1f2d12db64731c496697d63205c5c86fdd0ca99c GIT binary patch literal 646 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD3?#3*wSy$;0(?STf%K2a$bmxkNmDlfU7%GG z(hBAL1ng&L|8f#HE~weL;Qfo`LNnu2zc|D`{4}5aQ|B~& z14qZ^H32_)BTqQ8#@%MoTy{ZvlaM3J%-9VsPx3n%3-6W9NnjT^GG_q;hlqLrgX1Yq zp@s!dS)>#$%xPd~vYozv{w_96hk^$z9gAa%o0J_kJYeZwxt{AtcfqV*n~XU(clsFQ z3JRzuT<5q_7is=V=1S-so$G_bNpHPC;MP*b9qyLM)~odOu9}tdG3fs_RYF<=-#Jy>UX7+s*m|3FX@h7Hzm{P z7vt3*^DQ>-oWOkj#CnO;5>@v+Ykt?qGXEE59P7Ks+M8mRrNb}U>(=|DX(iv2g9rb9 zUZkKur@BG1@=+CY*}f*eiyMls^fk?1pTH~mo55JU@^d`P+WflL8Wz=3x8my7vpNc} c9GqT0>ycTx@a8A6!02S~boFyt=akR{0I@SBe*gdg literal 0 HcmV?d00001 diff --git a/app/src/main/res/mipmap-xxhdpi/tab_micro_normal.png b/app/src/main/res/mipmap-xxhdpi/tab_micro_normal.png new file mode 100644 index 0000000000000000000000000000000000000000..b1daabdad5ed41df0016ff7d601a0dd070824e53 GIT binary patch literal 891 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD3?#3*wSy$O0(?STfpoy&0P>pR6+qYMl?3?( zGw38w+J5cj=l`2}+58rYH2io|8Dc2O_0Wv@L-zT|lh$_~mHC%>m9CyHui3tLkKW7o zj0_A+MV>B>AsP4HM%E-v`qo;Lw&DfuEvHa5ih$VcdT{KUf587RwxFy+7%ju8c-=OvJv#wg*?EE1xdrGXF zsM(a)37x0shphO#vc}IY$`ct2H(MD*XT^1mi{Wd7s>=?#B-)^UgY z77@}Ay4uFY6=HItk@d-^#!quT&whWRQN&7tcj6pF&Xo(aR;0=N4EEXNT>WTD%30aP zT_%k8eRj>blUkY>DO=;f{$i2n%6_hbVzY+r*9&8rA0GLg;Iy~*`ZSZf2iQ$Qwp8Y> zEa5Z}co5#YfSq-{?$(=eD(pAiL$|Pms8sPsE%;XVO^y5NB3*&->ql0<6z}ZVpBW^? z_Mw6CC3}w7j~w@8XI8Tx>o?3h^M?0{kvZFs{yVzYrdpTiB`w~~`IKkQn;UQbosDnf zsWA)HJ^kG8z$~MlGL2Q`)p2zbzdK45Y;UR4{dDMN;q`l7Z+f2 z`2{mD>LyRxe(lAV{~LOkeHOm*7p@ci@gYmXIyB~w$%DsBZnHbCs5pK0&0e{dKeNiz z7#J8Qd%8G=WZZi@<91n-fq<(Z$4L#2s3xVLqr3n8XaDxddF|@8mdcT<`8V4?keQ)U z%+2H|z~U&-BF&a4vf4%3$V#$rQNe}PRkM0cYt`k&Gvhx^e!2ITsBP^_S3A9z=BFw? zmAp{)u1{S3oG*FV3&E^!E&pOJCx4lXsHY9so-{5##kxlSnhzDDgF;A32ZGMOIv-e!FH+RWzOqGmy zE^)TAy*TQwA#d3_w&-OCPhCwoZ(^}SYKsr^+pL6{Y)e-=9yU?9U!M@mYjXL)*ZX&r z-oHQa+xy$RZmY+ZvyJ!@UvFOiV*$e~Eyv3s#$*5w1iQa7w08Q&7=;!i#m((teF@Qrm0p+8H~d}b`H?t1oCYhTbr z+uo$h|F=EeEcfJ9(ERnk7yWtuApS=6guS~&wl93!b@gLh?VM7_`AZT-=TAy@R`U;u k`MPJbFfd?Q94k&p-%Lx&K5y?k3mB^mp00i_>zopr0DLt!K>z>% literal 0 HcmV?d00001 diff --git a/app/src/main/res/mipmap-xxhdpi/tab_video_normal.png b/app/src/main/res/mipmap-xxhdpi/tab_video_normal.png new file mode 100644 index 0000000000000000000000000000000000000000..57343f2265126f1587fd1867a9e67f2b2ad2fc45 GIT binary patch literal 582 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD3?#3*wSy!w1AIbUfph>}fa|?mia^VhOM?7@ z8Ca7Kd^(=PCG!7iq{WXG>8dviie?JvJ@mi5eVN1gt8VL5dn#o1C^0ZFW_r3fhGg7( zJLBY}Lk2u9shbiNBs>n~ZTtV@OX2$a|M<^US#At&_qKl)5Ifxx(fXd5>;3LO%YM0Nh{x-@zW)2+^A$U@<%ev_~Pn3BSitJQfY?2rX zOU*txFJOr3vYVcCFMz@H(_i+PzqlNFYrdB)+s7tweedj*`b_g?zS205zwB58qufj> zS@r-VoKIJj_%a_z`kn0ern{eGogCq9_A+MC?a%GlieR*Fx?!sz9N_S<4RJ}!!O z-|@5cc6kNELU)Y{j+WyQ6#~n{IZqg|#6J)D;m^ f3^BFIFIQR@H^^>$Q*kT+7)K1Au6{1-oD!M<^_A-+ literal 0 HcmV?d00001 diff --git a/app/src/main/res/mipmap-xxhdpi/tab_video_selected.png b/app/src/main/res/mipmap-xxhdpi/tab_video_selected.png new file mode 100644 index 0000000000000000000000000000000000000000..56b390416fe51d2421edfc255e3a782c033b5108 GIT binary patch literal 675 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD3?#3*wSy$?1^9%x0_h)-k;4^r-4bX5`p~N+ z$S;_ICFSs!Q#m}MNB=*GWIS&1Z=vL&&()k?9|r9={5wbFeKFf@pY2+|rwYE#y6U!0 zR9t%+-Q=3$v>v*x(R108|JCyK06oloae9s&*2~H6Ek$= z;=NT?K3{%m)~0#r+HCIl4QuDWdGqYB*qfK;zu6VJzqB7#j#%(` zGL!ZMzw_rJ8$)ysGOdhhWL-Wa=0Dl6kN}Y)SZE z<;(jsFT_3#bmX_Lh&kNo?5-yB>%%Q3{kSi$IP~{__{Ar$@P(6AzVU-3xBR1$sRBik zMoV7LDc?0g>f+Q{@&}eKkY>+W`!eQVvQdt|-NvMAK))LFSjM+yMVs<4JADSm1%s!n KpUXO@geCwFd_P|R literal 0 HcmV?d00001 diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 3ab3e9c..e354b4a 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,4 +3,9 @@ #3F51B5 #303F9F #FF4081 + + #F3F5F4 + #515051 + #D33D3C + diff --git a/viewlib/src/main/java/com/chaychan/viewlib/bottombarlayout/BottomBarItem.java b/viewlib/src/main/java/com/chaychan/viewlib/bottombarlayout/BottomBarItem.java new file mode 100644 index 0000000..52367cd --- /dev/null +++ b/viewlib/src/main/java/com/chaychan/viewlib/bottombarlayout/BottomBarItem.java @@ -0,0 +1,117 @@ +package com.chaychan.viewlib.bottombarlayout; + +import android.content.Context; +import android.content.res.TypedArray; +import android.support.annotation.Nullable; +import android.util.AttributeSet; +import android.view.Gravity; +import android.view.View; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.chaychan.viewlib.R; +import com.chaychan.viewlib.utils.UIUtils; + + +/** + * @author ChayChan + * @description: 底部tab条目 + * @date 2017/6/23 9:14 + */ + +public class BottomBarItem extends LinearLayout { + + private Context mContext; + private int mIconNormalResourceId;//普通状态图标的资源id + private int mIconSelectedResourceId;//选中状态图标的资源id + private String mText;//文本 + private int mTextSize = 12;//文字大小 默认为12sp + private int mTextColorNormal = 0xFF999999; //描述文本的默认显示颜色 + private int mTextColorSelected = 0xFF46C01B; //述文本的默认选中显示颜色 + private int mMarginTop = 5;//文字和图标的距离,默认5dp + private TextView mTextView; + private ImageView mImageView; + + public BottomBarItem(Context context) { + this(context, null); + } + + public BottomBarItem(Context context, @Nullable AttributeSet attrs) { + this(context, attrs, 0); + } + + public BottomBarItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + + mContext = context; + + TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BottomBarItem); + + mIconNormalResourceId = ta.getResourceId(R.styleable.BottomBarItem_iconNormal, -1); + mIconSelectedResourceId = ta.getResourceId(R.styleable.BottomBarItem_iconSelected, -1); + + mText = ta.getString(R.styleable.BottomBarItem_itemText); + mTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemTextSize, UIUtils.sp2px(mContext,mTextSize)); + + mTextColorNormal = ta.getColor(R.styleable.BottomBarItem_textColorNormal, mTextColorNormal); + mTextColorSelected = ta.getColor(R.styleable.BottomBarItem_textColorSelected, mTextColorSelected); + + mMarginTop = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemMarginTop, UIUtils.dip2Px(mContext, mMarginTop)); + ta.recycle(); + + checkValues(); + init(); + } + + /** + * 检查传入的值是否完善 + */ + private void checkValues() { + if (mIconNormalResourceId == -1) { + throw new IllegalStateException("您还没有设置默认状态下的图标,请指定iconNormal的图标"); + } + + if (mIconSelectedResourceId == -1) { + throw new IllegalStateException("您还没有设置选中状态下的图标,请指定iconSelected的图标"); + } + } + + private void init() { + setOrientation(VERTICAL); + setGravity(Gravity.CENTER); + + View view = View.inflate(mContext, R.layout.item_bottom_bar, null); + mImageView = (ImageView) view.findViewById(R.id.iv_icon); + mTextView = (TextView) view.findViewById(R.id.tv_text); + + mImageView.setImageResource(mIconNormalResourceId); + mTextView.getPaint().setTextSize(mTextSize); + mTextView.setText(mText); + mTextView.setTextColor(mTextColorNormal); + + LinearLayout.LayoutParams layoutParams = (LayoutParams) mTextView.getLayoutParams(); + layoutParams.topMargin = mMarginTop; + mTextView.setLayoutParams(layoutParams); + + addView(view); + } + + private ImageView createImageView() { + mImageView = new ImageView(mContext); + mImageView.setImageResource(mIconNormalResourceId); + return mImageView; + } + + private TextView creatTextView() { + mTextView = new TextView(mContext); + + mTextView.setGravity(Gravity.CENTER); + return mTextView; + } + + public void setStatus(boolean isSelected){ + mImageView.setImageResource(isSelected?mIconSelectedResourceId:mIconNormalResourceId); + mTextView.setTextColor(isSelected?mTextColorSelected:mTextColorNormal); + } +} diff --git a/viewlib/src/main/java/com/chaychan/viewlib/bottombarlayout/BottomBarLayout.java b/viewlib/src/main/java/com/chaychan/viewlib/bottombarlayout/BottomBarLayout.java new file mode 100644 index 0000000..7d5692b --- /dev/null +++ b/viewlib/src/main/java/com/chaychan/viewlib/bottombarlayout/BottomBarLayout.java @@ -0,0 +1,181 @@ +package com.chaychan.viewlib.bottombarlayout; + +import android.content.Context; +import android.os.Bundle; +import android.os.Parcelable; +import android.support.v4.view.ViewPager; +import android.util.AttributeSet; +import android.view.View; +import android.widget.LinearLayout; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author ChayChan + * @description: 底部页签根节点 + * @date 2017/6/23 11:02 + */ +public class BottomBarLayout extends LinearLayout implements ViewPager.OnPageChangeListener { + + private static final String STATE_INSTANCE = "instance_state"; + private static final String STATE_ITEM = "state_item"; + + + private ViewPager mViewPager; + private int mChildCount;//子条目个数 + private List itemViews = new ArrayList<>(); + private int mCurrentItem;//当前条目的索引 + private boolean mSmoothScroll; + + public BottomBarLayout(Context context) { + this(context, null); + } + + public BottomBarLayout(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public BottomBarLayout(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + + setOrientation(HORIZONTAL); + } + + @Override + public void setOrientation(int orientation) { + if (LinearLayout.VERTICAL == orientation) { + throw new IllegalArgumentException("BottomBarLayout only supports Horizontal Orientation."); + } + super.setOrientation(orientation); + } + + public void setmViewPager(ViewPager mViewPager) { + this.mViewPager = mViewPager; + init(); + } + + private void init() { + if (mViewPager == null) { + throw new IllegalArgumentException("参数不能为空"); + } + + mChildCount = getChildCount(); + if (mViewPager.getAdapter().getCount() != mChildCount) { + throw new IllegalArgumentException("LinearLayout的子View数量必须和ViewPager条目数量一致"); + } + for (int i = 0; i < mChildCount; i++) { + if (getChildAt(i) instanceof BottomBarItem) { + BottomBarItem bottomBarItem = (BottomBarItem) getChildAt(i); + itemViews.add(bottomBarItem); + //设置点击监听 + bottomBarItem.setOnClickListener(new MyOnClickListener(i)); + } else { + throw new IllegalArgumentException("AlphaIndicator的子View必须是AlphaView"); + } + } + + itemViews.get(mCurrentItem).setStatus(true);//设置选中项 + mViewPager.setOnPageChangeListener(this); + } + + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + + } + + @Override + public void onPageSelected(int position) { + mCurrentItem = position;//记录当前位置 + resetState(); + itemViews.get(position).setStatus(true); + mViewPager.setCurrentItem(position, mSmoothScroll); + } + + @Override + public void onPageScrollStateChanged(int state) { + + } + + private class MyOnClickListener implements OnClickListener { + + private int currentIndex; + + public MyOnClickListener(int i) { + this.currentIndex = i; + } + + @Override + public void onClick(View v) { + //回调点击的位置 + if (onItemSelectedListener != null) { + onItemSelectedListener.onItemSelected(currentIndex); + } + + //点击前先重置所有按钮的状态 + resetState(); + itemViews.get(currentIndex).setStatus(true);//设置为选中状态 + //不能使用平滑滚动,否者颜色改变会乱 + mViewPager.setCurrentItem(currentIndex, false); + //点击是保存当前按钮索引 + mCurrentItem = currentIndex; + } + } + + /** + * 重置所有按钮的状态 + */ + private void resetState() { + for (int i = 0; i < mChildCount; i++) { + itemViews.get(i).setStatus(false); + } + } + + + public int getCurrentItem() { + return mCurrentItem; + } + + public void setSmoothScroll(boolean mSmoothScroll) { + this.mSmoothScroll = mSmoothScroll; + } + + /** + * @return 当View被销毁的时候,保存数据 + */ + @Override + protected Parcelable onSaveInstanceState() { + Bundle bundle = new Bundle(); + bundle.putParcelable(STATE_INSTANCE, super.onSaveInstanceState()); + bundle.putInt(STATE_ITEM, mCurrentItem); + return bundle; + } + + /** + * @param state 用于恢复数据使用 + */ + @Override + protected void onRestoreInstanceState(Parcelable state) { + if (state instanceof Bundle) { + Bundle bundle = (Bundle) state; + mCurrentItem = bundle.getInt(STATE_ITEM); + //重置所有按钮状态 + resetState(); + //恢复点击的条目颜色 + itemViews.get(mCurrentItem).setStatus(true); + super.onRestoreInstanceState(bundle.getParcelable(STATE_INSTANCE)); + } else { + super.onRestoreInstanceState(state); + } + } + + private OnItemSelectedListener onItemSelectedListener; + + public interface OnItemSelectedListener { + void onItemSelected(int position); + } + + public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) { + this.onItemSelectedListener = onItemSelectedListener; + } +} diff --git a/viewlib/src/main/res/layout/item_bottom_bar.xml b/viewlib/src/main/res/layout/item_bottom_bar.xml new file mode 100644 index 0000000..78f25ba --- /dev/null +++ b/viewlib/src/main/res/layout/item_bottom_bar.xml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file diff --git a/viewlib/src/main/res/values/attr.xml b/viewlib/src/main/res/values/attr.xml index fd30768..b5ddf0c 100644 --- a/viewlib/src/main/res/values/attr.xml +++ b/viewlib/src/main/res/values/attr.xml @@ -97,4 +97,15 @@ + + + + + + + + + + + \ No newline at end of file -- GitLab