diff --git a/DropDownMenu.iml b/DropDownMenu.iml index 6981f2b..a164e72 100644 --- a/DropDownMenu.iml +++ b/DropDownMenu.iml @@ -13,7 +13,7 @@ - + \ No newline at end of file diff --git a/README.md b/README.md index 3ba1093..cc10721 100644 --- a/README.md +++ b/README.md @@ -1,65 +1,103 @@ -[![](https://jitpack.io/v/dongjunkun/DropDownMenu.svg)](https://jitpack.io/#dongjunkun/DropDownMenu) - ## 简介 -一个实用的多条件筛选菜单,在很多App上都能看到这个效果,如美团,爱奇艺电影票等 +扩展的思路 可以看我的[简书](http://www.jianshu.com/p/719267a0df32) -我的博客 [自己造轮子--android常用多条件帅选菜单实现思路(类似美团,爱奇艺电影票下拉菜单)](http://www.jianshu.com/p/d9407f799d2d) +## 下载 +[demo.apk](app/build/outputs/apk/app-debug.apk) -##特色 - - 支持多级菜单 - - 你可以完全自定义你的菜单样式,我这里只是封装了一些实用的方法,Tab的切换效果,菜单显示隐藏效果等 - - 并非用popupWindow实现,无卡顿 +## 特色 +* 支持多级菜单 +* 你可以完全自定义你的菜单样式,我这里只是封装了一些实用的方法,Tab的切换效果,菜单显示隐藏效果等 +* 非用popupWindow实现,无卡顿 + +## 截图 +
-##ScreenShot - +## 扩展 -Download APK +箭头居中而不是居最右
+ -或者扫描二维码 +icon方向属性
+ + - +分隔线高度属性
+ + -##Gradle Dependency +为popupWindows集合的view增加了对LayoutParams的支持
+ + -``` -allprojects { - repositories { - ... - maven { url "https://jitpack.io" } - } -} +支持手动添加非下拉tabView
+ -dependencies { - compile 'com.github.dongjunkun:DropDownMenu:1.0.3' -} -``` +## 使用 +注意:`此布局需要作为根布局才可能覆盖内容区域` -##使用 -添加DropDownMenu 到你的布局文件,如下 +* 样式扩展 +添加名为tab_item.xml到你的布局文件,在要显示内容的TextView上设置id为R.id.tv_tab。tab_item.xml中 任意布局即可。
+tab_item.xml +```xml + + + ``` - -``` -我们只需要在java代码中调用下面的代码 - +* 手动添加非下拉tabView +tab_text.xml设置样式/也可以代码生成,加载控件添加到DropDownMenu中
+tab_text +```xml + ``` - //tabs 所有标题,popupViews 所有菜单,contentView 内容 -mDropDownMenu.setDropDownMenu(tabs, popupViews, contentView); +调用添加,在setDropDownMenu之后添加 +```java +... +//init dropdownview + mDropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, contentView); + //测试tabView扩展功能 + TextView textView= (TextView) getLayoutInflater().inflate(R.layout.tab_text,null); + textView.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f)); + textView.setText("所有"); + mDropDownMenu.addTab(textView,0); + textView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + mDropDownMenu.closeMenu(); + } + }); ``` -如果你要了解更多,可以直接看源码 Example -##关于我 -简书[dongjunkun](http://www.jianshu.com/users/f07458c1a8f3/latest_articles) +如果你要了解更多,可以直接看源码 Example + +## 关于我 + +简书 [keyboard3](http://www.jianshu.com/users/62329de8c8a6/latest_articles)
+邮箱 keyboard3@icloud.com + +# 致谢 + +- 感谢 [dongjunkun/DropDownMenu](https://github.com/dongjunkun/DropDownMenu) diff --git a/app/app.iml b/app/app.iml index d95da30..c57f34d 100644 --- a/app/app.iml +++ b/app/app.iml @@ -66,14 +66,6 @@ - - - - - - - - @@ -82,6 +74,14 @@ + + + + + + + + diff --git a/app/build/outputs/apk/app-debug.apk b/app/build/outputs/apk/app-debug.apk index 74175f4..23a14b1 100644 Binary files a/app/build/outputs/apk/app-debug.apk and b/app/build/outputs/apk/app-debug.apk differ diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b9e6369..8d99ca5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" + android:fullBackupContent="true" android:theme="@style/AppTheme" > popupViews = new ArrayList<>(); @@ -47,6 +50,12 @@ protected void onCreate(Bundle savedInstanceState) { } private void initView() { + //测试tabView扩展功能 + final TextView textView = (TextView) getLayoutInflater().inflate(R.layout.tab_text, null); + textView.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f)); + textView.setTextColor(getResources().getColor(R.color.drop_down_selected)); + textView.setText("所有"); + //init city menu final ListView cityView = new ListView(this); cityAdapter = new GirdDropDownAdapter(this, Arrays.asList(citys)); @@ -78,7 +87,11 @@ public void onClick(View v) { mDropDownMenu.closeMenu(); } }); - + //添加时间 2017年1月5日21:28:22 + //keyboard3 为支持可以外部控制popViews里的View可以设置layoutParams。注意必须是FrameLayout的布局参数 + FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT); + layoutParams.setMargins(200, 100, 200, 0); + cityView.setLayoutParams(layoutParams); //init popupViews popupViews.add(cityView); popupViews.add(ageView); @@ -92,6 +105,7 @@ public void onItemClick(AdapterView parent, View view, int position, long id) cityAdapter.setCheckItem(position); mDropDownMenu.setTabText(position == 0 ? headers[0] : citys[position]); mDropDownMenu.closeMenu(); + textView.setTextColor(getResources().getColor(R.color.drop_down_unselected)); } }); @@ -101,6 +115,7 @@ public void onItemClick(AdapterView parent, View view, int position, long id) ageAdapter.setCheckItem(position); mDropDownMenu.setTabText(position == 0 ? headers[1] : ages[position]); mDropDownMenu.closeMenu(); + textView.setTextColor(getResources().getColor(R.color.drop_down_unselected)); } }); @@ -110,6 +125,7 @@ public void onItemClick(AdapterView parent, View view, int position, long id) sexAdapter.setCheckItem(position); mDropDownMenu.setTabText(position == 0 ? headers[2] : sexs[position]); mDropDownMenu.closeMenu(); + textView.setTextColor(getResources().getColor(R.color.drop_down_unselected)); } }); @@ -128,8 +144,26 @@ public void onItemClick(AdapterView parent, View view, int position, long id) contentView.setGravity(Gravity.CENTER); contentView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); - //init dropdownview + //初始化 dropdownview mDropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, contentView); + mDropDownMenu.addTab(textView, 4); + textView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (mDropDownMenu.isActive()) { + mDropDownMenu.closeMenu(); + textView.setTextColor(getResources().getColor(R.color.drop_down_selected)); + } else { + textView.setTextColor(getResources().getColor(R.color.drop_down_unselected)); + } + } + }); + mDropDownMenu.setOnItemMenuClickListener(new DropDownMenu.OnItemMenuClickListener() { + @Override + public void OnItemMenuClick(TextView tabView, int position) { + textView.setTextColor(getResources().getColor(R.color.drop_down_unselected)); + } + }); } @Override diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 200eb11..e50e8db 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,16 +1,15 @@ - + app:ddtextSelectedColor="@color/drop_down_selected" + app:ddtextUnselectedColor="@color/drop_down_unselected" + app:ddunderlineColor="@color/gray"> diff --git a/app/src/main/res/layout/custom_layout.xml b/app/src/main/res/layout/custom_layout.xml index f3b8a98..73a9111 100644 --- a/app/src/main/res/layout/custom_layout.xml +++ b/app/src/main/res/layout/custom_layout.xml @@ -2,7 +2,6 @@ + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f045a54..9d79f67 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,6 +1,5 @@ DropDownMenu - Hello world! Settings diff --git a/build.gradle b/build.gradle index 9a0af3a..e042b98 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.0' + classpath 'com.android.tools.build:gradle:2.2.3' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' // Add this line // NOTE: Do not place your application dependencies here; they belong diff --git a/dropdown_demo.gif b/dropdown_demo.gif new file mode 100644 index 0000000..2c32cf4 Binary files /dev/null and b/dropdown_demo.gif differ diff --git a/library/library.iml b/library/library.iml index 7b9985a..d82bb2e 100644 --- a/library/library.iml +++ b/library/library.iml @@ -123,7 +123,7 @@ - + \ No newline at end of file diff --git a/library/src/main/java/com/yyydjk/library/DropDownMenu.java b/library/src/main/java/com/yyydjk/library/DropDownMenu.java index 92f8480..180f3d2 100644 --- a/library/src/main/java/com/yyydjk/library/DropDownMenu.java +++ b/library/src/main/java/com/yyydjk/library/DropDownMenu.java @@ -2,8 +2,8 @@ import android.content.Context; import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; -import android.text.TextUtils; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.TypedValue; @@ -15,14 +15,60 @@ import android.widget.LinearLayout; import android.widget.TextView; +import java.util.ArrayList; import java.util.List; +class Orientation { + Context mContext; + private Drawable unSelectedDrawable; + private Drawable selectedDrawable; + private int orientation; + + public Orientation(Context context) { + mContext = context; + } + + public static final int left = 0; + public static final int top = 1; + public static final int right = 2; + public static final int bottom = 3; + + public Drawable getLeft(boolean close) { + return orientation == left ? (close ? unSelectedDrawable : selectedDrawable) : null; + } + + public Drawable getTop(boolean close) { + return orientation == top ? (close ? unSelectedDrawable : selectedDrawable) : null; + } + + public Drawable getRight(boolean close) { + return orientation == right ? (close ? unSelectedDrawable : selectedDrawable) : null; + } + + public Drawable getBottom(boolean close) { + return orientation == bottom ? (close ? unSelectedDrawable : selectedDrawable) : null; + } + + /** + * 初始化位置参数 + * + * @param orientation + * @param menuUnselectedIcon + */ + public void init(int orientation, int menuSelectedIcon, int menuUnselectedIcon) { + + unSelectedDrawable = mContext.getResources().getDrawable(menuUnselectedIcon); + selectedDrawable = mContext.getResources().getDrawable(menuSelectedIcon); + this.orientation = orientation; + } +} /** * Created by dongjunkun on 2015/6/17. */ public class DropDownMenu extends LinearLayout { - + //记录tabTexts的顺序 + List dropTabViews = new ArrayList<>(); //顶部菜单布局 private LinearLayout tabMenuView; //底部容器,包含popupMenuViews,maskView @@ -33,7 +79,7 @@ public class DropDownMenu extends LinearLayout { private View maskView; //tabMenuView里面选中的tab位置,-1表示未选中 private int current_tab_position = -1; - + private float dividerHeight; //分割线颜色 private int dividerColor = 0xffcccccc; //tab选中颜色 @@ -44,7 +90,9 @@ public class DropDownMenu extends LinearLayout { private int maskColor = 0x88888888; //tab字体大小 private int menuTextSize = 14; - + //icon的方向 + private static int iconOrientation = Orientation.right;//默认右则 + private Orientation mOrientation; //tab选中图标 private int menuSelectedIcon; //tab未选中图标 @@ -70,18 +118,30 @@ public DropDownMenu(Context context, AttributeSet attrs, int defStyleAttr) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DropDownMenu); underlineColor = a.getColor(R.styleable.DropDownMenu_ddunderlineColor, underlineColor); dividerColor = a.getColor(R.styleable.DropDownMenu_dddividerColor, dividerColor); - textSelectedColor = a.getColor(R.styleable.DropDownMenu_ddtextSelectedColor, textSelectedColor); - textUnselectedColor = a.getColor(R.styleable.DropDownMenu_ddtextUnselectedColor, textUnselectedColor); - menuBackgroundColor = a.getColor(R.styleable.DropDownMenu_ddmenuBackgroundColor, menuBackgroundColor); + textSelectedColor = a.getColor(R.styleable.DropDownMenu_ddtextSelectedColor, + textSelectedColor); + textUnselectedColor = a.getColor(R.styleable.DropDownMenu_ddtextUnselectedColor, + textUnselectedColor); + menuBackgroundColor = a.getColor(R.styleable.DropDownMenu_ddmenuBackgroundColor, + menuBackgroundColor); maskColor = a.getColor(R.styleable.DropDownMenu_ddmaskColor, maskColor); - menuTextSize = a.getDimensionPixelSize(R.styleable.DropDownMenu_ddmenuTextSize, menuTextSize); - menuSelectedIcon = a.getResourceId(R.styleable.DropDownMenu_ddmenuSelectedIcon, menuSelectedIcon); - menuUnselectedIcon = a.getResourceId(R.styleable.DropDownMenu_ddmenuUnselectedIcon, menuUnselectedIcon); + menuTextSize = a.getDimensionPixelSize(R.styleable.DropDownMenu_ddmenuTextSize, + menuTextSize); + dividerHeight = a.getDimensionPixelSize(R.styleable.DropDownMenu_dddividerHeight, ViewGroup.LayoutParams.MATCH_PARENT); + menuSelectedIcon = a.getResourceId(R.styleable.DropDownMenu_ddmenuSelectedIcon, + menuSelectedIcon); + menuUnselectedIcon = a.getResourceId(R.styleable.DropDownMenu_ddmenuUnselectedIcon, + menuUnselectedIcon); + iconOrientation = a.getInt(R.styleable.DropDownMenu_ddmenuIconOrientation, iconOrientation); a.recycle(); + //初始化位置参数 + mOrientation = new Orientation(getContext()); + mOrientation.init(iconOrientation, menuSelectedIcon, menuUnselectedIcon); //初始化tabMenuView并添加到tabMenuView tabMenuView = new LinearLayout(context); - LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); + LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup + .LayoutParams.WRAP_CONTENT); tabMenuView.setOrientation(HORIZONTAL); tabMenuView.setBackgroundColor(menuBackgroundColor); tabMenuView.setLayoutParams(params); @@ -89,17 +149,21 @@ public DropDownMenu(Context context, AttributeSet attrs, int defStyleAttr) { //为tabMenuView添加下划线 View underLine = new View(getContext()); - underLine.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpTpPx(1.0f))); + underLine.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + dpTpPx(1.0f))); underLine.setBackgroundColor(underlineColor); addView(underLine, 1); //初始化containerView并将其添加到DropDownMenu containerView = new FrameLayout(context); - containerView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); + containerView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams + .MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); addView(containerView, 2); + } + /** * 初始化DropDownMenu * @@ -107,9 +171,11 @@ public DropDownMenu(Context context, AttributeSet attrs, int defStyleAttr) { * @param popupViews * @param contentView */ - public void setDropDownMenu(@NonNull List tabTexts, @NonNull List popupViews, @NonNull View contentView) { + public void setDropDownMenu(@NonNull List tabTexts, @NonNull List popupViews, + @NonNull View contentView) { if (tabTexts.size() != popupViews.size()) { - throw new IllegalArgumentException("params not match, tabTexts.size() should be equal popupViews.size()"); + throw new IllegalArgumentException("params not match, tabTexts.size() should be equal" + + " popupViews.size()"); } for (int i = 0; i < tabTexts.size(); i++) { @@ -118,7 +184,8 @@ public void setDropDownMenu(@NonNull List tabTexts, @NonNull List containerView.addView(contentView, 0); maskView = new View(getContext()); - maskView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); + maskView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams + .MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); maskView.setBackgroundColor(maskColor); maskView.setOnClickListener(new OnClickListener() { @Override @@ -134,38 +201,80 @@ public void onClick(View v) { containerView.addView(popupMenuViews, 2); for (int i = 0; i < popupViews.size(); i++) { - popupViews.get(i).setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + if (popupViews.get(i).getLayoutParams() == null) { + popupViews.get(i).setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams + .MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + } popupMenuViews.addView(popupViews.get(i), i); } } - private void addTab(@NonNull List tabTexts, int i) { - final TextView tab = new TextView(getContext()); - tab.setSingleLine(); - tab.setEllipsize(TextUtils.TruncateAt.END); - tab.setGravity(Gravity.CENTER); - tab.setTextSize(TypedValue.COMPLEX_UNIT_PX,menuTextSize); + /** + * 自定义插入的tabView,如果包含R.id.tv_tab就当做普通的tabtext会统一做变色处理和向下的角标处理 + * + * @param tab + * @param index 0start + */ + public void addTab(View tab, int index) { + if (index == (tabMenuView.getChildCount() + 1) / 2) { + addTabEnd(tab); + return; + } + tabMenuView.addView(tab, index * 2); + tabMenuView.addView(getDividerView(), index * 2 + 1); + } + + public void addTabEnd(View tab) { + tabMenuView.addView(getDividerView(), tabMenuView.getChildCount()); + tabMenuView.addView(tab, tabMenuView.getChildCount()); + } + + private void addTab(@NonNull List tabTexts, final int i) { + final View tab = inflate(getContext(), R.layout.tab_item, null); tab.setLayoutParams(new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f)); - tab.setTextColor(textUnselectedColor); - tab.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(menuUnselectedIcon), null); - tab.setText(tabTexts.get(i)); - tab.setPadding(dpTpPx(5), dpTpPx(12), dpTpPx(5), dpTpPx(12)); - //添加点击事件 + final TextView textView = getTabTextView(tab); + textView.setText(tabTexts.get(i)); + textView.setTextColor(textUnselectedColor); + textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, menuTextSize); + setTextDrawables(textView, true); + tabMenuView.addView(tab); tab.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - switchMenu(tab); + if (itemMenuClickListener != null) { + itemMenuClickListener.OnItemMenuClick(textView, i); + } + switchMenu(v); } }); - tabMenuView.addView(tab); + //添加分割线 if (i < tabTexts.size() - 1) { - View view = new View(getContext()); - view.setLayoutParams(new LayoutParams(dpTpPx(0.5f), ViewGroup.LayoutParams.MATCH_PARENT)); - view.setBackgroundColor(dividerColor); - tabMenuView.addView(view); + tabMenuView.addView(getDividerView()); } + dropTabViews.add(tab); + } + + private View getDividerView() { + View view = new View(getContext()); + double height = dividerHeight > 0 ? dpTpPx(dividerHeight) : dividerHeight; + LayoutParams params = new LayoutParams(dpTpPx(0.5f), (int) height); + params.gravity = Gravity.CENTER_VERTICAL; + view.setLayoutParams(params); + view.setBackgroundColor(dividerColor); + return view; + } + + /** + * 获取tabView中id为tv_tab的textView + * + * @param tabView + * @return + */ + private TextView getTabTextView(View tabView) { + TextView tabtext = (TextView) tabView.findViewById(R.id.tv_tab); + return tabtext; } /** @@ -175,7 +284,7 @@ public void onClick(View v) { */ public void setTabText(String text) { if (current_tab_position != -1) { - ((TextView) tabMenuView.getChildAt(current_tab_position)).setText(text); + getTabTextView(tabMenuView.getChildAt(current_tab_position)).setText(text); } } @@ -190,16 +299,25 @@ public void setTabClickable(boolean clickable) { */ public void closeMenu() { if (current_tab_position != -1) { - ((TextView) tabMenuView.getChildAt(current_tab_position)).setTextColor(textUnselectedColor); - ((TextView) tabMenuView.getChildAt(current_tab_position)).setCompoundDrawablesWithIntrinsicBounds(null, null, - getResources().getDrawable(menuUnselectedIcon), null); + TextView textView = getTabTextView(tabMenuView.getChildAt(current_tab_position)); + textView.setTextColor(textUnselectedColor); + setTextDrawables(textView, true); popupMenuViews.setVisibility(View.GONE); - popupMenuViews.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.dd_menu_out)); + popupMenuViews.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim + .dd_menu_out)); maskView.setVisibility(GONE); maskView.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.dd_mask_out)); current_tab_position = -1; } + } + + public boolean isActive() { + return current_tab_position != -1; + } + public void setTextDrawables(TextView textview, boolean close) { + textview.setCompoundDrawablesWithIntrinsicBounds(mOrientation.getLeft(close), mOrientation.getTop(close), + mOrientation.getRight(close), mOrientation.getBottom(close)); } /** @@ -221,33 +339,69 @@ private void switchMenu(View target) { for (int i = 0; i < tabMenuView.getChildCount(); i = i + 2) { if (target == tabMenuView.getChildAt(i)) { if (current_tab_position == i) { - closeMenu(); - } else { + closeMenu();//关闭 + } else {//打开 if (current_tab_position == -1) { popupMenuViews.setVisibility(View.VISIBLE); - popupMenuViews.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.dd_menu_in)); + popupMenuViews.setAnimation(AnimationUtils.loadAnimation(getContext(), R + .anim.dd_menu_in)); maskView.setVisibility(VISIBLE); - maskView.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.dd_mask_in)); - popupMenuViews.getChildAt(i / 2).setVisibility(View.VISIBLE); - } else { - popupMenuViews.getChildAt(i / 2).setVisibility(View.VISIBLE); + maskView.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim + .dd_mask_in)); + } + View listView = getListView(tabMenuView.getChildAt(i)); + if (listView != null) { + listView.setVisibility(View.VISIBLE); } current_tab_position = i; - ((TextView) tabMenuView.getChildAt(i)).setTextColor(textSelectedColor); - ((TextView) tabMenuView.getChildAt(i)).setCompoundDrawablesWithIntrinsicBounds(null, null, - getResources().getDrawable(menuSelectedIcon), null); + TextView textView = getTabTextView(tabMenuView.getChildAt(i)); + textView.setTextColor(textSelectedColor); + setTextDrawables(textView, false); + } + } else {//关闭 + TextView textView = getTabTextView(tabMenuView.getChildAt(i)); + if (textView != null) { + textView.setTextColor(textUnselectedColor); + + } + View listView = getListView(tabMenuView.getChildAt(i)); + if (listView != null) { + if (textView != null) { + setTextDrawables(textView, true); + } + listView.setVisibility(View.GONE); } - } else { - ((TextView) tabMenuView.getChildAt(i)).setTextColor(textUnselectedColor); - ((TextView) tabMenuView.getChildAt(i)).setCompoundDrawablesWithIntrinsicBounds(null, null, - getResources().getDrawable(menuUnselectedIcon), null); - popupMenuViews.getChildAt(i / 2).setVisibility(View.GONE); } } } + OnItemMenuClickListener itemMenuClickListener; + + public void setOnItemMenuClickListener(OnItemMenuClickListener listener) { + itemMenuClickListener = listener; + } + + public interface OnItemMenuClickListener { + void OnItemMenuClick(TextView tabView, int position); + } + + /** + * 获取dropTabViews中对应popupMenuViews数组中的ListView + * + * @param view + * @return + */ + private View getListView(View view) { + if (dropTabViews.contains(view)) { + int index = dropTabViews.indexOf(view); + return popupMenuViews.getChildAt(index); + } else { + return null; + } + } + public int dpTpPx(float value) { DisplayMetrics dm = getResources().getDisplayMetrics(); return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, dm) + 0.5); } -} +} \ No newline at end of file diff --git a/library/src/main/res/layout/tab_item.xml b/library/src/main/res/layout/tab_item.xml new file mode 100644 index 0000000..806296e --- /dev/null +++ b/library/src/main/res/layout/tab_item.xml @@ -0,0 +1,19 @@ + + + + \ No newline at end of file diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index cf5bf47..5678b90 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -11,5 +11,15 @@ + + + + + + + + + + \ No newline at end of file