UpdateItemAt

This commit is contained in:
vakrilov
2015-09-03 14:51:33 +03:00
parent a55f59bf06
commit 5a926a7188

View File

@@ -157,6 +157,16 @@ public class TabLayout extends HorizontalScrollView {
} }
} }
/**
* Updates the ui of an item at specified index
*/
public void updateItemAt(int position, TabItemSpec tabItem) {
LinearLayout ll = (LinearLayout)mTabStrip.getChildAt(position);
ImageView imgView = (ImageView)ll.getChildAt(0);
TextView textView = (TextView)ll.getChildAt(1);
this.setupItem(ll, textView, imgView, tabItem);
}
/** /**
* Create a default view to be used for tabs. * Create a default view to be used for tabs.
*/ */
@@ -172,53 +182,61 @@ public class TabLayout extends HorizontalScrollView {
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true); getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
ll.setBackgroundResource(outValue.resourceId); ll.setBackgroundResource(outValue.resourceId);
ImageView imgView = null; ImageView imgView = new ImageView(context);
if (tabItem.iconId != 0 || tabItem.iconDrawable != null) { imgView.setScaleType(ScaleType.FIT_CENTER);
imgView = new ImageView(context); LinearLayout.LayoutParams imgLP = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imgView.setScaleType(ScaleType.FIT_CENTER); imgLP.gravity = Gravity.CENTER;
LinearLayout.LayoutParams imgLP = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); imgView.setLayoutParams(imgLP);
imgLP.gravity = Gravity.CENTER;
imgView.setLayoutParams(imgLP); TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setMaxWidth((int) (TEXT_MAX_WIDHT * density));
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setAllCaps(true);
textView.setMaxLines(2);
textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setPadding(padding, 0, padding, 0);
if (tabItem.iconId != 0) { this.setupItem(ll, textView, imgView, tabItem);
imgView.setImageResource(tabItem.iconId);
} else { ll.addView(imgView);
imgView.setImageDrawable(tabItem.iconDrawable); ll.addView(textView);
} return ll;
}
private void setupItem(LinearLayout ll, TextView textView,ImageView imgView, TabItemSpec tabItem){
float density = getResources().getDisplayMetrics().density;
if (tabItem.iconId != 0) {
imgView.setImageResource(tabItem.iconId);
imgView.setVisibility(VISIBLE);
} else if (tabItem.iconDrawable != null) {
imgView.setImageDrawable(tabItem.iconDrawable);
imgView.setVisibility(VISIBLE);
} else {
imgView.setVisibility(GONE);
} }
TextView textView = null; if (tabItem.title != null && !tabItem.title.isEmpty()) {
if(tabItem.title != null && !tabItem.title.isEmpty()) {
textView = new TextView(context);
textView.setText(tabItem.title); textView.setText(tabItem.title);
textView.setGravity(Gravity.CENTER); textView.setVisibility(VISIBLE);
textView.setMaxWidth((int) (TEXT_MAX_WIDHT * density)); } else {
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP); textView.setVisibility(GONE);
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setAllCaps(true);
textView.setMaxLines(2);
textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setPadding(padding, 0, padding, 0);
} }
if(imgView != null && textView!= null){ if (imgView.getVisibility() == VISIBLE && textView.getVisibility() == VISIBLE) {
ll.setMinimumHeight((int) (LARGE_MIN_HEIGHT * density)); ll.setMinimumHeight((int) (LARGE_MIN_HEIGHT * density));
} } else {
else{
ll.setMinimumHeight((int) (SMALL_MIN_HEIGHT * density)); ll.setMinimumHeight((int) (SMALL_MIN_HEIGHT * density));
} }
if(imgView != null){ if (mDistributeEvenly) {
ll.addView(imgView); LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ll.getLayoutParams();
lp.width = 0;
lp.weight = 1;
} }
if(textView != null) {
ll.addView(textView);
}
return ll;
} }
private void populateTabStrip() { private void populateTabStrip() {
@@ -229,22 +247,15 @@ public class TabLayout extends HorizontalScrollView {
View tabView = null; View tabView = null;
TabItemSpec tabItem; TabItemSpec tabItem;
if(this.mTabItems != null && this.mTabItems.length > i) { if (this.mTabItems != null && this.mTabItems.length > i) {
tabItem = this.mTabItems[i]; tabItem = this.mTabItems[i];
} } else {
else{
tabItem = new TabItemSpec(); tabItem = new TabItemSpec();
tabItem.title = adapter.getPageTitle(i).toString(); tabItem.title = adapter.getPageTitle(i).toString();
} }
tabView = createDefaultTabView(getContext(), tabItem); tabView = createDefaultTabView(getContext(), tabItem);
if (mDistributeEvenly) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams();
lp.width = 0;
lp.weight = 1;
}
tabView.setOnClickListener(tabClickListener); tabView.setOnClickListener(tabClickListener);
String desc = mContentDescriptions.get(i, null); String desc = mContentDescriptions.get(i, null);
if (desc != null) { if (desc != null) {