| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.animation.LayoutTransition; |
| 20 |
import android.app.FragmentManager.BackStackEntry; |
| 21 |
import android.content.Context; |
| 22 |
import android.content.res.TypedArray; |
| 23 |
import android.util.AttributeSet; |
| 24 |
import android.view.Gravity; |
| 25 |
import android.view.LayoutInflater; |
| 26 |
import android.view.View; |
| 27 |
import android.view.ViewGroup; |
| 28 |
import android.widget.LinearLayout; |
| 29 |
import android.widget.TextView; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
@Deprecated |
| 44 |
public class FragmentBreadCrumbs extends ViewGroup |
| 45 |
implements FragmentManager.OnBackStackChangedListener { |
| 46 |
Activity mActivity; |
| 47 |
LayoutInflater mInflater; |
| 48 |
LinearLayout mContainer; |
| 49 |
int mMaxVisible = -1; |
| 50 |
|
| 51 |
|
| 52 |
BackStackRecord mTopEntry; |
| 53 |
BackStackRecord mParentEntry; |
| 54 |
|
| 55 |
|
| 56 |
private OnClickListener mParentClickListener; |
| 57 |
|
| 58 |
private OnBreadCrumbClickListener mOnBreadCrumbClickListener; |
| 59 |
|
| 60 |
private int mGravity; |
| 61 |
private int mLayoutResId; |
| 62 |
private int mTextColor; |
| 63 |
|
| 64 |
private static final int DEFAULT_GRAVITY = Gravity.START | Gravity.CENTER_VERTICAL; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
@Deprecated |
| 72 |
public interface OnBreadCrumbClickListener { |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public boolean onBreadCrumbClick(BackStackEntry backStack, int flags); |
| 85 |
} |
| 86 |
|
| 87 |
public FragmentBreadCrumbs(Context context) { |
| 88 |
this(context, null); |
| 89 |
} |
| 90 |
|
| 91 |
public FragmentBreadCrumbs(Context context, AttributeSet attrs) { |
| 92 |
this(context, attrs, com.android.internal.R.attr.fragmentBreadCrumbsStyle); |
| 93 |
} |
| 94 |
|
| 95 |
public FragmentBreadCrumbs(Context context, AttributeSet attrs, int defStyleAttr) { |
| 96 |
this(context, attrs, defStyleAttr, 0); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public FragmentBreadCrumbs( |
| 103 |
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 104 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 105 |
|
| 106 |
final TypedArray a = context.obtainStyledAttributes(attrs, |
| 107 |
com.android.internal.R.styleable.FragmentBreadCrumbs, defStyleAttr, defStyleRes); |
| 108 |
|
| 109 |
mGravity = a.getInt(com.android.internal.R.styleable.FragmentBreadCrumbs_gravity, |
| 110 |
DEFAULT_GRAVITY); |
| 111 |
mLayoutResId = a.getResourceId( |
| 112 |
com.android.internal.R.styleable.FragmentBreadCrumbs_itemLayout, |
| 113 |
com.android.internal.R.layout.fragment_bread_crumb_item); |
| 114 |
mTextColor = a.getColor( |
| 115 |
com.android.internal.R.styleable.FragmentBreadCrumbs_itemColor, |
| 116 |
0); |
| 117 |
|
| 118 |
a.recycle(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public void setActivity(Activity a) { |
| 126 |
mActivity = a; |
| 127 |
mInflater = (LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 128 |
mContainer = (LinearLayout)mInflater.inflate( |
| 129 |
com.android.internal.R.layout.fragment_bread_crumbs, |
| 130 |
this, false); |
| 131 |
addView(mContainer); |
| 132 |
a.getFragmentManager().addOnBackStackChangedListener(this); |
| 133 |
updateCrumbs(); |
| 134 |
setLayoutTransition(new LayoutTransition()); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public void setMaxVisible(int visibleCrumbs) { |
| 142 |
if (visibleCrumbs < 1) { |
| 143 |
throw new IllegalArgumentException("visibleCrumbs must be greater than zero"); |
| 144 |
} |
| 145 |
mMaxVisible = visibleCrumbs; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public void setParentTitle(CharSequence title, CharSequence shortTitle, |
| 160 |
OnClickListener listener) { |
| 161 |
mParentEntry = createBackStackEntry(title, shortTitle); |
| 162 |
mParentClickListener = listener; |
| 163 |
updateCrumbs(); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public void setOnBreadCrumbClickListener(OnBreadCrumbClickListener listener) { |
| 173 |
mOnBreadCrumbClickListener = listener; |
| 174 |
} |
| 175 |
|
| 176 |
private BackStackRecord createBackStackEntry(CharSequence title, CharSequence shortTitle) { |
| 177 |
if (title == null) return null; |
| 178 |
|
| 179 |
final BackStackRecord entry = new BackStackRecord( |
| 180 |
(FragmentManagerImpl) mActivity.getFragmentManager()); |
| 181 |
entry.setBreadCrumbTitle(title); |
| 182 |
entry.setBreadCrumbShortTitle(shortTitle); |
| 183 |
return entry; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public void setTitle(CharSequence title, CharSequence shortTitle) { |
| 192 |
mTopEntry = createBackStackEntry(title, shortTitle); |
| 193 |
updateCrumbs(); |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 198 |
|
| 199 |
|
| 200 |
final int childCount = getChildCount(); |
| 201 |
if (childCount == 0) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
final View child = getChildAt(0); |
| 206 |
|
| 207 |
final int childTop = mPaddingTop; |
| 208 |
final int childBottom = mPaddingTop + child.getMeasuredHeight() - mPaddingBottom; |
| 209 |
|
| 210 |
int childLeft; |
| 211 |
int childRight; |
| 212 |
|
| 213 |
final int layoutDirection = getLayoutDirection(); |
| 214 |
final int horizontalGravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
| 215 |
switch (Gravity.getAbsoluteGravity(horizontalGravity, layoutDirection)) { |
| 216 |
case Gravity.RIGHT: |
| 217 |
childRight = mRight - mLeft - mPaddingRight; |
| 218 |
childLeft = childRight - child.getMeasuredWidth(); |
| 219 |
break; |
| 220 |
|
| 221 |
case Gravity.CENTER_HORIZONTAL: |
| 222 |
childLeft = mPaddingLeft + (mRight - mLeft - child.getMeasuredWidth()) / 2; |
| 223 |
childRight = childLeft + child.getMeasuredWidth(); |
| 224 |
break; |
| 225 |
|
| 226 |
case Gravity.LEFT: |
| 227 |
default: |
| 228 |
childLeft = mPaddingLeft; |
| 229 |
childRight = childLeft + child.getMeasuredWidth(); |
| 230 |
break; |
| 231 |
} |
| 232 |
|
| 233 |
if (childLeft < mPaddingLeft) { |
| 234 |
childLeft = mPaddingLeft; |
| 235 |
} |
| 236 |
|
| 237 |
if (childRight > mRight - mLeft - mPaddingRight) { |
| 238 |
childRight = mRight - mLeft - mPaddingRight; |
| 239 |
} |
| 240 |
|
| 241 |
child.layout(childLeft, childTop, childRight, childBottom); |
| 242 |
} |
| 243 |
|
| 244 |
@Override |
| 245 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 246 |
final int count = getChildCount(); |
| 247 |
|
| 248 |
int maxHeight = 0; |
| 249 |
int maxWidth = 0; |
| 250 |
int measuredChildState = 0; |
| 251 |
|
| 252 |
|
| 253 |
for (int i = 0; i < count; i++) { |
| 254 |
final View child = getChildAt(i); |
| 255 |
if (child.getVisibility() != GONE) { |
| 256 |
measureChild(child, widthMeasureSpec, heightMeasureSpec); |
| 257 |
maxWidth = Math.max(maxWidth, child.getMeasuredWidth()); |
| 258 |
maxHeight = Math.max(maxHeight, child.getMeasuredHeight()); |
| 259 |
measuredChildState = combineMeasuredStates(measuredChildState, |
| 260 |
child.getMeasuredState()); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
maxWidth += mPaddingLeft + mPaddingRight; |
| 266 |
maxHeight += mPaddingTop + mPaddingBottom; |
| 267 |
|
| 268 |
|
| 269 |
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); |
| 270 |
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); |
| 271 |
|
| 272 |
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, measuredChildState), |
| 273 |
resolveSizeAndState(maxHeight, heightMeasureSpec, |
| 274 |
measuredChildState<<MEASURED_HEIGHT_STATE_SHIFT)); |
| 275 |
} |
| 276 |
|
| 277 |
@Override |
| 278 |
public void onBackStackChanged() { |
| 279 |
updateCrumbs(); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
private int getPreEntryCount() { |
| 287 |
return (mTopEntry != null ? 1 : 0) + (mParentEntry != null ? 1 : 0); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
private BackStackEntry getPreEntry(int index) { |
| 297 |
|
| 298 |
if (mParentEntry != null) { |
| 299 |
return index == 0 ? mParentEntry : mTopEntry; |
| 300 |
} else { |
| 301 |
return mTopEntry; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
void updateCrumbs() { |
| 306 |
FragmentManager fm = mActivity.getFragmentManager(); |
| 307 |
int numEntries = fm.getBackStackEntryCount(); |
| 308 |
int numPreEntries = getPreEntryCount(); |
| 309 |
int numViews = mContainer.getChildCount(); |
| 310 |
for (int i = 0; i < numEntries + numPreEntries; i++) { |
| 311 |
BackStackEntry bse = i < numPreEntries |
| 312 |
? getPreEntry(i) |
| 313 |
: fm.getBackStackEntryAt(i - numPreEntries); |
| 314 |
if (i < numViews) { |
| 315 |
View v = mContainer.getChildAt(i); |
| 316 |
Object tag = v.getTag(); |
| 317 |
if (tag != bse) { |
| 318 |
for (int j = i; j < numViews; j++) { |
| 319 |
mContainer.removeViewAt(i); |
| 320 |
} |
| 321 |
numViews = i; |
| 322 |
} |
| 323 |
} |
| 324 |
if (i >= numViews) { |
| 325 |
final View item = mInflater.inflate(mLayoutResId, this, false); |
| 326 |
final TextView text = (TextView) item.findViewById(com.android.internal.R.id.title); |
| 327 |
text.setText(bse.getBreadCrumbTitle()); |
| 328 |
text.setTag(bse); |
| 329 |
text.setTextColor(mTextColor); |
| 330 |
if (i == 0) { |
| 331 |
item.findViewById(com.android.internal.R.id.left_icon).setVisibility(View.GONE); |
| 332 |
} |
| 333 |
mContainer.addView(item); |
| 334 |
text.setOnClickListener(mOnClickListener); |
| 335 |
} |
| 336 |
} |
| 337 |
int viewI = numEntries + numPreEntries; |
| 338 |
numViews = mContainer.getChildCount(); |
| 339 |
while (numViews > viewI) { |
| 340 |
mContainer.removeViewAt(numViews - 1); |
| 341 |
numViews--; |
| 342 |
} |
| 343 |
|
| 344 |
for (int i = 0; i < numViews; i++) { |
| 345 |
final View child = mContainer.getChildAt(i); |
| 346 |
|
| 347 |
child.findViewById(com.android.internal.R.id.title).setEnabled(i < numViews - 1); |
| 348 |
if (mMaxVisible > 0) { |
| 349 |
|
| 350 |
child.setVisibility(i < numViews - mMaxVisible ? View.GONE : View.VISIBLE); |
| 351 |
final View leftIcon = child.findViewById(com.android.internal.R.id.left_icon); |
| 352 |
|
| 353 |
leftIcon.setVisibility(i > numViews - mMaxVisible && i != 0 ? View.VISIBLE |
| 354 |
: View.GONE); |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
private OnClickListener mOnClickListener = new OnClickListener() { |
| 360 |
public void onClick(View v) { |
| 361 |
if (v.getTag() instanceof BackStackEntry) { |
| 362 |
BackStackEntry bse = (BackStackEntry) v.getTag(); |
| 363 |
if (bse == mParentEntry) { |
| 364 |
if (mParentClickListener != null) { |
| 365 |
mParentClickListener.onClick(v); |
| 366 |
} |
| 367 |
} else { |
| 368 |
if (mOnBreadCrumbClickListener != null) { |
| 369 |
if (mOnBreadCrumbClickListener.onBreadCrumbClick( |
| 370 |
bse == mTopEntry ? null : bse, 0)) { |
| 371 |
return; |
| 372 |
} |
| 373 |
} |
| 374 |
if (bse == mTopEntry) { |
| 375 |
|
| 376 |
mActivity.getFragmentManager().popBackStack(); |
| 377 |
} else { |
| 378 |
mActivity.getFragmentManager().popBackStack(bse.getId(), 0); |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
}; |
| 384 |
} |