下面列出了怎么用android.support.v7.widget.CardView的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Animation Card for down animation, please do not modify this.
* @param card card view instance
* @param toolbar toolbar instance
*/
@Override
public void animationCardDown(@NotNull final CardView card, @NotNull final Toolbar toolbar, final long duration){
new CountDownTimer(1, 1) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
card.animate()
.translationY(height - ((int)(toolbar.getHeight() * 1.7)))
.setDuration(duration).start();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
fab.animate()
.translationY((height - ((int) (toolbar.getHeight() * 1.7))) + card.getHeight() - (fab.getHeight() - fab.getHeight()/4))
.setDuration(duration).start();
}else{
fab.animate()
.translationY((height - ((int) (toolbar.getHeight() * 1.7))) + card.getHeight() - (fab.getHeight() - fab.getHeight()/3))
.setDuration(duration).start();
}
}
}.start();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = LayoutInflater.from(container.getContext())
.inflate(R.layout.adapter, container, false);
container.addView(view);
bind(mData.get(position), view);
CardView cardView = (CardView) view.findViewById(R.id.cardView);
if (mBaseElevation == 0) {
mBaseElevation = cardView.getCardElevation();
}
cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
mViews.set(position, cardView);
return view;
}
private ViewHolder createViewHolder(View contentView, int position) {
ViewHolder viewHolder = new ViewHolder();
if (contentView == null) {
return viewHolder;
}
viewHolder.cardView = (CardView) contentView.findViewById(R.id.card_view);
// viewHolder.cardView.setViewName("card:view:" + position);
viewHolder.titleText = (TextView) contentView.findViewById(R.id.navigation_title_text);
// viewHolder.titleText.setViewName("nav:title:" + position);
viewHolder.infoText = (TextView) contentView.findViewById(R.id.navigation_info_text);
// viewHolder.infoText.setViewName("nav:info:" + position);
contentView.setTag(viewHolder);
return viewHolder;
}
RecordingViewHolder(View itemView) {
super(itemView);
// Get views
viewHolder = itemView;
card = (CardView) itemView.findViewById(R.id.card);
recordingDate = (TextView) itemView.findViewById(R.id.recording_date);
chart = (LineChart) itemView.findViewById(R.id.chart);
moreIcon = (ImageView) itemView.findViewById(R.id.more_icon);
// Set listeners
viewHolder.setOnClickListener(this);
viewHolder.setOnCreateContextMenuListener(this);
moreIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Open Menu
listener.onOpenMenuClick(viewHolder);
}
});
background = card.getBackground();
formatter = new SimpleDateFormat(
context.getString(R.string.hive_recordings_date_format), Locale.getDefault());
}
HivesViewHolder(View itemView) {
super(itemView);
// Get views
viewHolder = itemView;
card = (CardView) itemView.findViewById(R.id.card);
hiveName = (TextView) itemView.findViewById(R.id.hive_name);
lastRevision = (TextView) itemView.findViewById(R.id.last_revision);
moreIcon = (ImageView) itemView.findViewById(R.id.more_icon);
// Set listeners
viewHolder.setOnClickListener(this);
viewHolder.setOnCreateContextMenuListener(this);
moreIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Open Menu
listener.onOpenMenuClick(viewHolder);
}
});
background = card.getBackground();
}
private void initContentView(boolean fullScreen) {
alpha = SPHelper.getInt(Constants.TIMECAT_ALPHA, 100);
lastPickedColor = SPHelper.getInt(Constants.TIMECAT_DIY_BG_COLOR, Color.parseColor("#03A9F4"));
int value = (int) ((alpha / 100.0f) * 255);
RegexUtil.refreshSymbolSelection();
if (fullScreen) {
setTheme(R.style.PreSettingTheme);
setContentView(R.layout.activity_time_cat);
getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.timecat_activity_window_full));
getWindow().getDecorView().setBackgroundColor(Color.argb(value, Color.red(lastPickedColor), Color.green(lastPickedColor), Color.blue(lastPickedColor)));
showAppList4OneStep();
} else {
CardView cardView = new CardView(this);
cardView.setRadius(ViewUtil.dp2px(10));
cardView.setCardBackgroundColor(Color.argb(value, Color.red(lastPickedColor), Color.green(lastPickedColor), Color.blue(lastPickedColor)));
View view = LayoutInflater.from(this).inflate(R.layout.activity_time_cat, null, false);
cardView.addView(view);
getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.trans));
setContentView(cardView);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CardView cardView = new CardView(this);
cardView.setRadius(ViewUtil.dp2px(10));
View view = LayoutInflater.from(this).inflate(R.layout.activity_history, null, false);
cardView.addView(view);
getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.trans));
setContentView(cardView);
//<功能归类分区方法,必须调用>-----------------------------------------------------------------
initView();
initData();
initEvent();
//</功能归类分区方法,必须调用>----------------------------------------------------------------
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
setContentView(R.layout.activity_login_registration);
login = (Button) findViewById(R.id.btn_login);
register = (Button) findViewById(R.id.btn_register);
cardView=(CardView)findViewById(R.id.layout2);
splashActivity=new SplashActivity();
login.setOnClickListener(this);
register.setOnClickListener(this);
b=splashActivity.containsPass("password");
if(b==true)
{
register.setVisibility(View.INVISIBLE);
cardView.setVisibility(View.INVISIBLE);
}
}
private void init() {
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
githubContainer = (FrameLayout) findViewById(R.id.githubContainer);
playStoreContainer = (FrameLayout) findViewById(R.id.playStoreContainer);
linkedInContainer = (FrameLayout) findViewById(R.id.linkedInContainer);
socialContainer = (CardView) findViewById(R.id.socialContainer);
githubContainer.setOnClickListener(this);
playStoreContainer.setOnClickListener(this);
linkedInContainer.setOnClickListener(this);
}
public VideoViewHolder(View contentView) {
super(contentView);
img = (ImageView) contentView.findViewById(R.id.img);
tv_title = (TextView) contentView.findViewById(R.id.tv_title);
tv_like = (TextView) contentView.findViewById(R.id.tv_like);
tv_unlike = (TextView) contentView.findViewById(R.id.tv_unlike);
tv_comment_count = (TextView) contentView.findViewById(R.id.tv_comment_count);
tv_un_support_des = (TextView) contentView.findViewById(R.id.tv_unsupport_des);
tv_support_des = (TextView) contentView.findViewById(R.id.tv_support_des);
img_share = (ImageView) contentView.findViewById(R.id.img_share);
ll_comment = (LinearLayout) contentView.findViewById(R.id.ll_comment);
card = (CardView) contentView.findViewById(R.id.card);
}
@Override
public void onClick(View v) {
fab.setSelected(false);
removeFabMenuItems();
if (menuListener != null) {
if (v == this || v == touchGuard) {
menuListener.onMenuClosed();
} else if (v instanceof FloatingActionButton) {
menuListener.onMenuItemSelected(fabMenuItemMap.get(v));
} else if (v instanceof CardView) {
menuListener.onMenuItemSelected(cardViewMenuItemMap.get(v));
}
} else {
Log.d(TAG, "You haven't provided a MenuListener.");
}
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = LayoutInflater.from(container.getContext())
.inflate(R.layout.adapter, container, false);
container.addView(view);
bind(mData.get(position), view);
CardView cardView = (CardView) view.findViewById(R.id.cardView);
if (mBaseElevation == 0) {
mBaseElevation = cardView.getCardElevation();
}
cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
mViews.set(position, cardView);
return view;
}
public ParticipantItemPresenter(CardView view, Conversation conversation) {
super(view);
this.cardView = view;
this.conversation = conversation;
this.propertyChangeListener = new ParticipantItemPropertyChangeListener();
this.participant = null;
this.participantDisplayNameTextView = (TextView)this.cardView.findViewById(
R.id.participantDisplayId);
this.participantOnHoldTextView = (TextView)this.cardView.findViewById(
R.id.participantHoldId);
this.participantMuteButton = (Button)this.cardView.findViewById(R.id.participantMuteId);
this.participantMuteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onParticipantMuteButtonClicked(v);
}
});
this.conversation.getSelfParticipant().addOnPropertyChangedCallback(
this.propertyChangeListener);
this.setLocalParticipantLeader();
}
private void onEnterTransitionFinished() {
if (fab.getScaleX() != 1f) {
fab.animate().scaleX(1f).scaleY(1f).setDuration(mShortAnimTime).start();
}
if (albumArtDim.getAlpha() != 1f) {
albumArtDim.animate().alpha(1f).setDuration(mShortAnimTime).start();
}
final CardView cardView = findViewById(R.id.cardView);
if (cardView != null && cardView.getVisibility() != View.VISIBLE) {
if (TransitionUtils.supportsActivityTransitions() && hasCoverTransition) {
cardView.setTranslationY(SlideFromBottomHelper.getStartTranslation(cardView));
cardView.setVisibility(View.VISIBLE);
SlideFromBottomHelper.createAnimator(cardView).setDuration(mMediumAnimTime).start();
} else {
cardView.setVisibility(View.VISIBLE);
}
}
}
@Override
public View getView(int position, View v, ViewGroup parent) {
ViewHolder vh = null;
if (v == null) {
v = View.inflate(PlanActivity.this, R.layout.item_plan, null);
vh = new ViewHolder();
vh.cv = (CardView) v.findViewById(R.id.item_plan_cv);
vh.name = (TextView) v.findViewById(R.id.item_plan_name);
vh.des = (TextView) v.findViewById(R.id.item_plan_des);
v.setTag(vh);
} else {
vh = (ViewHolder) v.getTag();
}
int ranColor = 0xff000000 | random.nextInt(0x00ffffff);
vh.cv.setCardBackgroundColor(ranColor);
vh.name.setText(plans.get(position));
vh.des.setText("共" + counts.get(position).getTotal() + "个,已完成" + counts.get(position).getDone() + "个");
return v;
}
@Override
public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_mine, container, false);
mLlFace = (CardView) view.findViewById(R.id.ll_face);
mCivFace = (CircleImageView) view.findViewById(R.id.civ_face);
mTvName = (TextView) view.findViewById(R.id.tv_name);
mLlCache = (LinearLayout) view.findViewById(R.id.ll_cache);
mLlAttention = (LinearLayout) view.findViewById(R.id.ll_attention);
mLlFeedBack = (LinearLayout) view.findViewById(R.id.ll_feedback);
mLlRecord = (LinearLayout) view.findViewById(R.id.ll_record);
return view;
}
@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final CardView child,
final View directTargetChild, final View target, final int nestedScrollAxes) {
if (!canScrolling)
child.setTranslationY(0);
return canScrolling;
}
public void initView() {
cardView = (CardView) view.findViewById(R.id.item1);
textView = (TextView) view.findViewById(R.id.lab_textview);
textView.setText(R.string.nightMode1);
aSwitch = (Switch) cardView.findViewById(R.id.cb);
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(MyApplication.shareFilename2, getActivity().MODE_PRIVATE);
boolean checked = sharedPreferences.getBoolean("ischecked", false);
aSwitch.setChecked(checked);
switchStatus(aSwitch, MyApplication.shareFilename2);
}
PersonViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
cv = (CardView) itemView.findViewById(R.id.cv);
personName = (TextView) itemView.findViewById(R.id.person_name);
personAge = (TextView) itemView.findViewById(R.id.person_age);
personPhoto = (ImageView) itemView.findViewById(R.id.person_photo);
cbSelected = (CheckBox) itemView.findViewById(R.id.cbSelected);
}
static float calculateVerticalPadding(CardView cardView) {
float maxShadowSize = cardView.getMaxCardElevation();
float cornerRadius = cardView.getRadius();
boolean addPaddingForCorners = cardView.getPreventCornerOverlap();
if (addPaddingForCorners) {
return (float) (maxShadowSize * 1.5f + (1 - COS_45) * cornerRadius);
} else {
return maxShadowSize * 1.5f;
}
}
public ViewHolder(View view) {
super(view);
cr = (CardView) view.findViewById(R.id.card_view);
imgSrc = (ImageView) view.findViewById(R.id.img_gallery);
checkBoxSelect = (AnimateCheckBox) view.findViewById(R.id.cig_checkBox_select_user);
prgBottomSheet = (ProgressBar) view.findViewById(R.id.prgBottomSheet);
}
@Override
public View getRealChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder holder;
if (convertView == null || !(convertView.getTag() instanceof ChildViewHolder)) {
convertView = inflater.inflate(R.layout.local_child_item, parent, false);
holder = new ChildViewHolder();
holder.title = convertView.findViewById(R.id.child_title);
holder.divider = convertView.findViewById(R.id.child_divider);
holder.card = (CardView) holder.title.getParent();
convertView.setTag(holder);
} else
holder = (ChildViewHolder) convertView.getTag();
holder.lyrics = getChild(groupPosition, childPosition);
if (holder.lyrics != null) {
holder.title.setText(holder.lyrics.getTitle());
holder.card.setBackgroundColor(markedRows.contains(new int[]{groupPosition, childPosition}) ? childSelectedStateColor : childDefaultStateColor);
holder.title.setTextColor(markedRows.contains(new int[]{groupPosition, childPosition}) ? childDefaultStateColor : childSelectedStateColor);
convertView.setOnTouchListener(mTouchListener);
holder.groupPosition = groupPosition;
holder.divider.setVisibility(isLastChild ? View.GONE : View.VISIBLE);
convertView.setAlpha(1f);
convertView.setVisibility(View.VISIBLE);
} else
convertView.setVisibility(View.GONE);
convertView.setTranslationX(0f);
return convertView;
}
public AppViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.txtName);
vApk = (TextView) v.findViewById(R.id.txtApk);
vIcon = (ImageView) v.findViewById(R.id.imgIcon);
vExtract = (ButtonFlat) v.findViewById(R.id.btnExtract);
vShare = (ButtonFlat) v.findViewById(R.id.btnShare);
vCard = (CardView) v.findViewById(R.id.app_card);
}
@Override
public void onBindItemHolder(SuperViewHolder holder, int position) {
CardView cardView = holder.getView(R.id.card_view);
TextView textView = holder.getView(R.id.info_text);
ItemModel itemModel = mDataList.get(position);
textView.setText(itemModel.title);
//修改高度,模拟交错效果
cardView.getLayoutParams().height = itemModel.height;
}
public static void loadPoster(Context context, CardView card, ImageView imageView, String url) {
Glide.with(context)
.load(url)
.listener(getPalette(Type.POSTER, null, null, card, url))
.transition(DrawableTransitionOptions.withCrossFade())
.apply(RequestOptions.centerCropTransform())
.into(imageView);
}
/**
* Animation Card for up animation, please do not modify this.
* @param card card view instance
* @param toolbar toolbar instance
*/
@Override
public void animationCardUp(@NotNull final CardView card, final Toolbar toolbar, final long duration){
new CountDownTimer(1, 1) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
card.animate()
.translationY(height - (card.getHeight() + toolbar.getHeight()))
.setDuration(duration).start();
}
}.start();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
int searchViewWidth = r - l;
int searchViewHeight = b - t;
int searchCardLeft;
int searchCardTop;
int searchCardRight;
int searchCardBottom;
int searchCardWidth;
int searchCardHeight;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (i == 0 && child instanceof CardView) {
CardView searchCard = (CardView) child;
int horizontalPadding = (int) Math.ceil(calculateHorizontalPadding(searchCard));
int verticalPadding = (int) Math.ceil(calculateVerticalPadding(searchCard));
searchCardLeft = mCardHorizontalPadding - horizontalPadding;
searchCardTop = mCardVerticalPadding - verticalPadding;
searchCardWidth = searchViewWidth - searchCardLeft * 2;
searchCardHeight = child.getMeasuredHeight();
searchCardRight = searchCardLeft + searchCardWidth;
searchCardBottom = searchCardTop + searchCardHeight;
child.layout(searchCardLeft, searchCardTop, searchCardRight, searchCardBottom);
}
}
}
public VideoViewHolder(View itemView) {
super(itemView);
videoCard = (CardView) itemView.findViewById(R.id.card_view_video);
videoImageView = (ImageView) itemView.findViewById(R.id.image_view_video);
videoTextView = (TextView) itemView.findViewById(R.id.text_view_video_name);
videoCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent youtubeIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.YOUTUBE_WATCH_BASE_URL + mVideos.get(getAdapterPosition()).getKey()));
mContext.startActivity(youtubeIntent);
}
});
}
@Override
public void bindView(){
btnBack = (ImageView) findViewById( R.id.btn_back );
btnOrder = (Button) findViewById( R.id.btn_order );
cardLoading = (CardView) findViewById( R.id.card_loading );
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById( R.id.map );
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int totalHeight = 0;
int searchCardWidth;
final int childCount = getChildCount();
for (int i = 0; i < childCount; ++i) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
if (i == 0 && child instanceof CardView) {
CardView searchCard = (CardView) child;
int horizontalPadding = (int) Math.ceil(calculateHorizontalPadding(searchCard));
int verticalPadding = (int) Math.ceil(calculateVerticalPadding(searchCard));
// searchCardWidth = widthSize - 2 * mCardVisiblePadding + horizontalPadding * 2;
int searchCardLeft = mCardHorizontalPadding - horizontalPadding;
// searchCardTop = mCardVisiblePadding - verticalPadding;
searchCardWidth = widthSize - searchCardLeft * 2;
int cardWidthSpec = MeasureSpec.makeMeasureSpec(searchCardWidth, MeasureSpec.EXACTLY);
// int cardHeightSpec = MeasureSpec.makeMeasureSpec(searchCardHeight, MeasureSpec.EXACTLY);
measureChild(child, cardWidthSpec, heightMeasureSpec);
int childMeasuredHeight = child.getMeasuredHeight();
int childMeasuredWidth = child.getMeasuredWidth();
int childHeight = childMeasuredHeight - verticalPadding * 2;
totalHeight = totalHeight + childHeight + mCardVerticalPadding * 2;
}
}
}
if(totalHeight < mCustomToolbarHeight)
totalHeight = mCustomToolbarHeight;
setMeasuredDimension(widthSize, totalHeight);
}