下面列出了android.widget.ProgressBar#setTag ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void calcProgressToViewAndMark(ProgressBar bar, long offset, long total,
boolean anim) {
final int contentLengthOnInt = reducePrecision(total);
final int shrinkRate = contentLengthOnInt == 0
? 1 : (int) (total / contentLengthOnInt);
bar.setTag(shrinkRate);
final int progress = (int) (offset / shrinkRate);
bar.setMax(contentLengthOnInt);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
bar.setProgress(progress, anim);
} else {
bar.setProgress(progress);
}
}
private void init(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ProgressLayout);
int backgroundColor = a.getColor(R.styleable.ProgressLayout_progressLayoutProgressBackground, Color.TRANSPARENT);
boolean startFromProgress = a.getBoolean(R.styleable.ProgressLayout_progressLayoutProgress, false);
a.recycle();
LayoutParams layoutParams;
// if progressBackground color == Color.TRANSPARENT just add progress bar
if (backgroundColor == Color.TRANSPARENT) {
mProgressView = new ProgressBar(getContext());
layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(CENTER_IN_PARENT);
} else { // else wrap progress bar in LinearLayout and set background color to LinearLayout
LinearLayout linearLayout = new LinearLayout(getContext());
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setBackgroundColor(backgroundColor);
layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ProgressBar progressBar = new ProgressBar(getContext());
linearLayout.addView(progressBar);
mProgressView = linearLayout;
}
mProgressView.setTag(TAG_PROGRESS);
if (!startFromProgress) {
mProgressView.setVisibility(View.GONE);
}
addView(mProgressView, layoutParams);
}
@Override
protected void prepare(View view, Param param, int immediatePassHint) {
super.prepare(view, param, immediatePassHint);
Cursor cursor = (Cursor) mAdapter.getItem(param.cursorPosition);
if (!prepared) {
titleColumn = cursor.getColumnIndex("title");
watchedColumn = cursor.getColumnIndex("seconds_watched");
completedColumn = cursor.getColumnIndex("completed");
prepared = true;
}
String title = cursor.getString(titleColumn);
TextView titleView = (TextView) view.findViewById(R.id.list_video_title);
ImageView iconView = (ImageView) view.findViewById(R.id.complete_icon);
ProgressBar bar = (ProgressBar) view.findViewById(R.id.list_video_dl_progress);
String youtubeId = param.youtubeId;
bar.setTag(youtubeId);
updateBar(bar);
// User view completion icon.
int watched = 0;
boolean complete = false;
try {
watched = cursor.getInt(watchedColumn);
complete = cursor.getInt(completedColumn) != 0;
} catch (Exception e) {
// Swallow. This will be due to null values not making their way through getInt in some implementations.
}
int resId = complete
? R.drawable.video_indicator_complete
: watched > 0
? R.drawable.video_indicator_started
: R.drawable.empty_icon;
iconView.setImageResource(resId);
titleView.setText(title);
}
@Override
protected void prepare(View view, Param param, int immediatePassHint) {
super.prepare(view, param, immediatePassHint);
Cursor cursor = (Cursor) mAdapter.getItem(param.cursorPosition);
if (!prepared) {
titleColumn = cursor.getColumnIndex("title");
watchedColumn = cursor.getColumnIndex("seconds_watched");
completedColumn = cursor.getColumnIndex("completed");
prepared = true;
}
String title = cursor.getString(titleColumn);
TextView titleView = (TextView) view.findViewById(R.id.list_video_title);
ImageView iconView = (ImageView) view.findViewById(R.id.complete_icon);
ProgressBar bar = (ProgressBar) view.findViewById(R.id.list_video_dl_progress);
bar.setTag(param.youtubeId);
updateBar(bar);
// User view completion icon.
int watched = 0;
boolean complete = false;
try {
watched = cursor.getInt(watchedColumn);
complete = cursor.getInt(completedColumn) != 0;
} catch (Exception e) {
// Swallow. This will be due to null values not making their way through getInt in some implementations.
}
int resId = complete
? R.drawable.video_indicator_complete
: watched > 0
? R.drawable.video_indicator_started
: R.drawable.empty_icon;
iconView.setImageResource(resId);
titleView.setText(title);
}