Comments (9)
继承后,加个类 public void setpos(int current_tab_position){
this.current_tab_position=current_tab_position;
},然后在调用这个控件的actvity里写:mDropDownMenu.setpos(0);
mDropDownMenu.setTabText(type_pos == 0 ? headers[0] :types[type_pos]);
mDropDownMenu.closeMenu();
from dropdownmenu.
继承后,加个类 public void setpos(int current_tab_position){
this.current_tab_position=current_tab_position;
},然后在调用这个控件的actvity里写:mDropDownMenu.setpos(0);
mDropDownMenu.setTabText(type_pos == 0 ? headers[0] :types[type_pos]);
mDropDownMenu.closeMenu();
这样做还是不可以默认选中啊。。。
from dropdownmenu.
可以的,你继承后,把DropDownMenu.class类的代码全部复制到你的自定义的类,然后在你自定义的类加我给你的方法。我已经实现了,你可以看看DropDownMenu.class里的代码,理解一下就可以了
from dropdownmenu.
继承后,加个类 public void setpos(int current_tab_position){
this.current_tab_position=current_tab_position;
},然后在调用这个控件的actvity里写:mDropDownMenu.setpos(0);
mDropDownMenu.setTabText(type_pos == 0 ? headers[0] :types[type_pos]);
mDropDownMenu.closeMenu();这样做还是不可以默认选中啊。。。
可以的,你继承后,把DropDownMenu.class类的代码全部复制到你的自定义的类,然后在你自定义的类加我给你的方法。我已经实现了,你可以看看DropDownMenu.class里的代码,理解一下就可以了
from dropdownmenu.
可以的,你继承后,把DropDownMenu.class类的代码全部复制到你的自定义的类,然后在你自定义的类加我给你的方法。我已经实现了,你可以看看DropDownMenu.class里的代码,理解一下就可以了
我是直接在DropDownMenu的源码里面加的你的方法setTabText方法还是不生效
from dropdownmenu.
我贴代码给你吧,public class MyDropDownMenu extends DropDownMenu {
private LinearLayout tabMenuView;
private FrameLayout containerView;
private FrameLayout popupMenuViews;
private View maskView;
private int current_tab_position;
private int dividerColor;
private int textSelectedColor;
private int textUnselectedColor;
private int maskColor;
private int menuTextSize;
private int menuSelectedIcon;
private int menuUnselectedIcon;
private float menuHeighPercent;
public MyDropDownMenu(Context context) {
super(context, (AttributeSet)null);
this.current_tab_position = -1;
this.dividerColor = -3355444;
this.textSelectedColor = -7795579;
this.textUnselectedColor = -15658735;
this.maskColor = -2004318072;
this.menuTextSize = 14;
this.menuHeighPercent = 0.5F;
}
public MyDropDownMenu(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyDropDownMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.current_tab_position = -1;
this.dividerColor = -3355444;
this.textSelectedColor = -7795579;
this.textUnselectedColor = -15658735;
this.maskColor = -2004318072;
this.menuTextSize = 14;
this.menuHeighPercent = 0.5F;
this.setOrientation(LinearLayout.VERTICAL);
int menuBackgroundColor = -1;
int underlineColor = -3355444;
TypedArray a = context.obtainStyledAttributes(attrs, com.yyydjk.library.R.styleable.DropDownMenu);
underlineColor = a.getColor(com.yyydjk.library.R.styleable.DropDownMenu_ddunderlineColor, underlineColor);
this.dividerColor = a.getColor(com.yyydjk.library.R.styleable.DropDownMenu_dddividerColor, this.dividerColor);
this.textSelectedColor = a.getColor(com.yyydjk.library.R.styleable.DropDownMenu_ddtextSelectedColor, this.textSelectedColor);
this.textUnselectedColor = a.getColor(com.yyydjk.library.R.styleable.DropDownMenu_ddtextUnselectedColor, this.textUnselectedColor);
menuBackgroundColor = a.getColor(com.yyydjk.library.R.styleable.DropDownMenu_ddmenuBackgroundColor, menuBackgroundColor);
this.maskColor = a.getColor(com.yyydjk.library.R.styleable.DropDownMenu_ddmaskColor, this.maskColor);
this.menuTextSize = a.getDimensionPixelSize(com.yyydjk.library.R.styleable.DropDownMenu_ddmenuTextSize, this.menuTextSize);
this.menuSelectedIcon = a.getResourceId(com.yyydjk.library.R.styleable.DropDownMenu_ddmenuSelectedIcon, this.menuSelectedIcon);
this.menuUnselectedIcon = a.getResourceId(com.yyydjk.library.R.styleable.DropDownMenu_ddmenuUnselectedIcon, this.menuUnselectedIcon);
this.menuHeighPercent = a.getFloat(com.yyydjk.library.R.styleable.DropDownMenu_ddmenuMenuHeightPercent, this.menuHeighPercent);
a.recycle();
this.tabMenuView = new LinearLayout(context);
LayoutParams params = new LayoutParams(-1, -2);
this.tabMenuView.setOrientation(HORIZONTAL);
this.tabMenuView.setBackgroundColor(menuBackgroundColor);
this.tabMenuView.setLayoutParams(params);
this.addView(this.tabMenuView, 0);
View underLine = new View(this.getContext());
underLine.setLayoutParams(new LayoutParams(-1, this.dpTpPx(1.0F)));
underLine.setBackgroundColor(underlineColor);
this.addView(underLine, 1);
this.containerView = new FrameLayout(context);
this.containerView.setLayoutParams(new android.widget.FrameLayout.LayoutParams(-1, -1));
this.addView(this.containerView, 2);
}
public void setDropDownMenu(@NonNull List<String> tabTexts, @NonNull List<View> popupViews, @NonNull View contentView) {
if(tabTexts.size() != popupViews.size()) {
throw new IllegalArgumentException("params not match, tabTexts.size() should be equal popupViews.size()");
} else {
int i;
for(i = 0; i < tabTexts.size(); ++i) {
this.addTab(tabTexts, i);
}
this.containerView.addView(contentView, 0);
this.maskView = new View(this.getContext());
this.maskView.setLayoutParams(new android.widget.FrameLayout.LayoutParams(-1, -1));
this.maskView.setBackgroundColor(this.maskColor);
this.maskView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MyDropDownMenu.this.closeMenu();
}
});
this.containerView.addView(this.maskView, 1);
this.maskView.setVisibility(GONE);
if(this.containerView.getChildAt(2) != null) {
this.containerView.removeViewAt(2);
}
this.popupMenuViews = new FrameLayout(this.getContext());
this.popupMenuViews.setLayoutParams(new android.widget.FrameLayout.LayoutParams(-1, (int)((float)DeviceUtils.getScreenSize(this.getContext()).y * this.menuHeighPercent)));
this.popupMenuViews.setVisibility(GONE);
this.containerView.addView(this.popupMenuViews, 2);
for(i = 0; i < popupViews.size(); ++i) {
((View)popupViews.get(i)).setLayoutParams(new android.view.ViewGroup.LayoutParams(-1, -2));
this.popupMenuViews.addView((View)popupViews.get(i), i);
}
}
}
private void addTab(@NonNull List<String> tabTexts, int i) {
final TextView tab = new TextView(this.getContext());
tab.setSingleLine();
tab.setEllipsize(TextUtils.TruncateAt.END);
tab.setGravity(17);
tab.setTextSize(0, (float)this.menuTextSize);
tab.setLayoutParams(new LayoutParams(0, -2, 1.0F));
tab.setTextColor(this.textUnselectedColor);
tab.setCompoundDrawablesWithIntrinsicBounds((Drawable)null, (Drawable)null, this.getResources().getDrawable(this.menuUnselectedIcon), (Drawable)null);
tab.setText((CharSequence)tabTexts.get(i));
tab.setPadding(this.dpTpPx(5.0F), this.dpTpPx(12.0F), this.dpTpPx(5.0F), this.dpTpPx(12.0F));
tab.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MyDropDownMenu.this.switchMenu(tab);
}
});
this.tabMenuView.addView(tab);
if(i < tabTexts.size() - 1) {
View view = new View(this.getContext());
view.setLayoutParams(new LayoutParams(this.dpTpPx(0.5F), -1));
view.setBackgroundColor(this.dividerColor);
this.tabMenuView.addView(view);
}
}
public void setTabText(String text) {
if(this.current_tab_position != -1) {
((TextView)this.tabMenuView.getChildAt(this.current_tab_position)).setText(text);
Log.i("mypan", "setTabText: "+this.current_tab_position);
}
}
public void setTabClickable(boolean clickable) {
for(int i = 0; i < this.tabMenuView.getChildCount(); i += 2) {
this.tabMenuView.getChildAt(i).setClickable(clickable);
}
}
public void closeMenu() {
if(this.current_tab_position != -1) {
((TextView)this.tabMenuView.getChildAt(this.current_tab_position)).setTextColor(this.textUnselectedColor);
((TextView)this.tabMenuView.getChildAt(this.current_tab_position)).setCompoundDrawablesWithIntrinsicBounds((Drawable)null, (Drawable)null, this.getResources().getDrawable(this.menuUnselectedIcon), (Drawable)null);
this.popupMenuViews.setVisibility(GONE);
this.popupMenuViews.setAnimation(AnimationUtils.loadAnimation(this.getContext(), com.yyydjk.library.R.anim.dd_menu_out));
this.maskView.setVisibility(GONE);
this.maskView.setAnimation(AnimationUtils.loadAnimation(this.getContext(), com.yyydjk.library.R.anim.dd_mask_out));
this.current_tab_position = -1;
}
}
public boolean isShowing() {
return this.current_tab_position != -1;
}
private void switchMenu(View target) {
System.out.println(this.current_tab_position);
for(int i = 0; i < this.tabMenuView.getChildCount(); i += 2) {
if(target == this.tabMenuView.getChildAt(i)) {
if(this.current_tab_position == i) {
this.closeMenu();
} else {
if(this.current_tab_position == -1) {
this.popupMenuViews.setVisibility(VISIBLE);
this.popupMenuViews.setAnimation(AnimationUtils.loadAnimation(this.getContext(), com.yyydjk.library.R.anim.dd_menu_in));
this.maskView.setVisibility(VISIBLE);
this.maskView.setAnimation(AnimationUtils.loadAnimation(this.getContext(), com.yyydjk.library.R.anim.dd_mask_in));
this.popupMenuViews.getChildAt(i / 2).setVisibility(VISIBLE);
} else {
this.popupMenuViews.getChildAt(i / 2).setVisibility(VISIBLE);
}
this.current_tab_position = i;
((TextView)this.tabMenuView.getChildAt(i)).setTextColor(this.textSelectedColor);
((TextView)this.tabMenuView.getChildAt(i)).setCompoundDrawablesWithIntrinsicBounds((Drawable)null, (Drawable)null, this.getResources().getDrawable(this.menuSelectedIcon), (Drawable)null);
}
} else {
((TextView)this.tabMenuView.getChildAt(i)).setTextColor(this.textUnselectedColor);
((TextView)this.tabMenuView.getChildAt(i)).setCompoundDrawablesWithIntrinsicBounds((Drawable)null, (Drawable)null, this.getResources().getDrawable(this.menuUnselectedIcon), (Drawable)null);
this.popupMenuViews.getChildAt(i / 2).setVisibility(GONE);
}
}
}
public int dpTpPx(float value) {
DisplayMetrics dm = this.getResources().getDisplayMetrics();
return (int)((double)TypedValue.applyDimension(1, value, dm) + 0.5D);
}
public void setpos(int current_tab_position){
this.current_tab_position=current_tab_position;
}
}
from dropdownmenu.
current_tab_position这个值,要点击控件才不是-1,所以我们使用的时候,传值给它
from dropdownmenu.
的
谢谢你,直接在源码中添加你的方法也是可行的,难道我昨晚加班懵逼了,昨晚不行,今天就可以了 。
/** * 改变对应位置tab标签文字 */ public void setTabText(int position, String text) { current_tab_position = position; if (null != tabMenuView.getChildAt(current_tab_position)) { getTabTextView(tabMenuView.getChildAt(current_tab_position)).setText(text); } }
from dropdownmenu.
current_tab_position这个值,要点击控件才不是-1,所以我们使用的时候,传值给它
如果我的dropmenu里面有多个view怎么办呢?这样只是设置第一个view的默认选中
from dropdownmenu.
Related Issues (20)
- 清空的处理没有吗?
- 怎么遮盖navigationbar HOT 4
- 如何获得点击Tabs是第几个?
- 提一个需求 HOT 1
- ddmaskColor设置不管用?
- 控件里加了RecyclerView就报错 HOT 5
- 这,,,代码 HOT 1
- 能不能动态更改标题问题呀?
- 这个项目是否可以实现动态设置数据源
- 动态添加数据刷新控件内容不会再崩溃了 看这。。。 HOT 6
- 可以设置Menu里每个Item的样式吗。 HOT 1
- 我魔改扩展了你的代码
- 从慕课网拷的?? HOT 2
- 默认选中 HOT 1
- 怎样设置成想要的长度,和特定的位置 HOT 1
- 怎么获取选中的文字啊
- 怎么拿到tab的点击事件?怎么改呢 HOT 1
- 必须作为父布局这个限制太多了 HOT 1
- 弹窗弹出时底部视图被覆盖
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dropdownmenu.