如何在Android中按向下/向上缩放回收器查看卡片项目?
我有一个回收视图,里面的每个项目都是卡片视图。 我想在按下卡片项时缩小卡片项,在单击向上时放大卡片项 以下是CardViewActivity.java 和myRecycleServiceAdapter.java card_view_row.xml-如何在Android中按向下/向上缩放回收器查看卡片项目?,android,animation,android-recyclerview,android-cardview,cardview,Android,Animation,Android Recyclerview,Android Cardview,Cardview,我有一个回收视图,里面的每个项目都是卡片视图。 我想在按下卡片项时缩小卡片项,在单击向上时放大卡片项 以下是CardViewActivity.java 和myRecycleServiceAdapter.java card_view_row.xml- 可在RecyclerView.OnItemTouchListeneronTouch中检测到按下和释放操作: onTouch中返回false不会停止事件,因此您设置的onItemClick也会工作。 您还可以轻松地为缩放设置动画以查找更多信息: 请注意
可在RecyclerView.OnItemTouchListeneronTouch中检测到按下和释放操作: onTouch中返回false不会停止事件,因此您设置的onItemClick也会工作。 您还可以轻松地为缩放设置动画以查找更多信息:
请注意,如果不返回正确的值,mRecyclerView.OnItemTouchListener和mRecyclerView.OnTouchListener可能会相互干扰,对于缩放项目,第一个值就足够了。您需要创建一个动画来执行此操作,例如
item_click.xml
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100"
android:fromXScale="1.0"
android:toXScale="0.9"
android:fromYScale="1.0"
android:toYScale="0.9"
android:pivotX="50%"
android:pivotY="50%" />
或者不使用xml
val anim: Animation = ScaleAnimation(
1f,
0.9f,
1f,
0.9f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f)
anim.duration = 100
view.startAnimation(anim)
持续时间:动画持续时间为毫秒
FromXScale/FromYScale:起始X和Y位置1是视图的原始大小
ToXScale/ToYScale:末端X和Y位置0.9略小于原始尺寸
PivotX/PivotY:在本例中,动画的原点50%是视图的中心
最后
您需要在适配器中使用此动画,因为每个项视图都是在适配器中访问的,对于您来说,这可以是DataObjectHolder OnClick
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp"
card_view:contentPadding="10dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textStyle="bold"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginTop="10dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
//...
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
v.setScale(0.9f);
break;
case MotionEvent.ACTION_UP:
v.setScale(1f);
break;
}
return false;
}
// ...
});
Animation anim = new ScaleAnimation(/* Scale down/up animation */);
anim.setFillAfter(true);
anim.setDuration(/* ... */);
v.startAnimation(anim);
item_click.xml
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100"
android:fromXScale="1.0"
android:toXScale="0.9"
android:fromYScale="1.0"
android:toYScale="0.9"
android:pivotX="50%"
android:pivotY="50%" />
val anim: Animation = AnimationUtils.loadAnimation(context, R.anim.item_click)
view.startAnimation(anim)
val anim: Animation = ScaleAnimation(
1f,
0.9f,
1f,
0.9f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f)
anim.duration = 100
view.startAnimation(anim)