DropDownMenu
DropDownMenu copied to clipboard
怎么设置默认选中
怎么设置默认选中
继承后,加个类 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();
继承后,加个类 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里的代码,理解一下就可以了
继承后,加个类 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里的代码,理解一下就可以了
可以的,你继承后,把DropDownMenu.class类的代码全部复制到你的自定义的类,然后在你自定义的类加我给你的方法。我已经实现了,你可以看看DropDownMenu.class里的代码,理解一下就可以了
我是直接在DropDownMenu的源码里面加的你的方法setTabText方法还是不生效
我贴代码给你吧,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;
}
}
current_tab_position这个值,要点击控件才不是-1,所以我们使用的时候,传值给它
的 谢谢你,直接在源码中添加你的方法也是可行的,难道我昨晚加班懵逼了,昨晚不行,今天就可以了 。
/** * 改变对应位置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); } }
current_tab_position这个值,要点击控件才不是-1,所以我们使用的时候,传值给它
如果我的dropmenu里面有多个view怎么办呢?这样只是设置第一个view的默认选中