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.
*/
@@ -172,26 +182,13 @@ public class TabLayout extends HorizontalScrollView {
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
ll.setBackgroundResource(outValue.resourceId);
ImageView imgView = null;
if (tabItem.iconId != 0 || tabItem.iconDrawable != null) {
imgView = new ImageView(context);
ImageView imgView = new ImageView(context);
imgView.setScaleType(ScaleType.FIT_CENTER);
LinearLayout.LayoutParams imgLP = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imgLP.gravity = Gravity.CENTER;
imgView.setLayoutParams(imgLP);
if (tabItem.iconId != 0) {
imgView.setImageResource(tabItem.iconId);
} else {
imgView.setImageDrawable(tabItem.iconDrawable);
}
}
TextView textView = null;
if(tabItem.title != null && !tabItem.title.isEmpty()) {
textView = new TextView(context);
textView.setText(tabItem.title);
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);
@@ -201,24 +198,45 @@ public class TabLayout extends HorizontalScrollView {
textView.setMaxLines(2);
textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setPadding(padding, 0, padding, 0);
this.setupItem(ll, textView, imgView, tabItem);
ll.addView(imgView);
ll.addView(textView);
return ll;
}
if(imgView != null && textView!= null){
ll.setMinimumHeight((int) (LARGE_MIN_HEIGHT * density));
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);
}
else{
if (tabItem.title != null && !tabItem.title.isEmpty()) {
textView.setText(tabItem.title);
textView.setVisibility(VISIBLE);
} else {
textView.setVisibility(GONE);
}
if (imgView.getVisibility() == VISIBLE && textView.getVisibility() == VISIBLE) {
ll.setMinimumHeight((int) (LARGE_MIN_HEIGHT * density));
} else {
ll.setMinimumHeight((int) (SMALL_MIN_HEIGHT * density));
}
if(imgView != null){
ll.addView(imgView);
if (mDistributeEvenly) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ll.getLayoutParams();
lp.width = 0;
lp.weight = 1;
}
if(textView != null) {
ll.addView(textView);
}
return ll;
}
private void populateTabStrip() {
@@ -231,20 +249,13 @@ public class TabLayout extends HorizontalScrollView {
TabItemSpec tabItem;
if (this.mTabItems != null && this.mTabItems.length > i) {
tabItem = this.mTabItems[i];
}
else{
} else {
tabItem = new TabItemSpec();
tabItem.title = adapter.getPageTitle(i).toString();
}
tabView = createDefaultTabView(getContext(), tabItem);
if (mDistributeEvenly) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams();
lp.width = 0;
lp.weight = 1;
}
tabView.setOnClickListener(tabClickListener);
String desc = mContentDescriptions.get(i, null);
if (desc != null) {