<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
reclyview實現列表回彈動畫效果,供大家參考,具體內容如下
1.reclyview列表佈局檔案
<com.example.demo1.ReboundLayout android:id="@+id/view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent" android:fadingEdgeLength="30px" android:requiresFadingEdge="horizontal"/> </com.example.demo1.ReboundLayout>
2.item佈局檔案
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="40px" android:layout_gravity="center_vertical"> <ImageView android:id="@+id/card_item_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp"/> </LinearLayout>
3.適配adapter
public class HomeCardAdapter extends RecyclerView.Adapter<HomeCardAdapter.ViewHolder> { private LayoutInflater mInflater; private List<Integer> mDatas; private OnItemClickListener mOnItemClickListener; private Context mContext; public HomeCardAdapter(Context context, List<Integer> datats) { mInflater = LayoutInflater.from(context); mDatas = datats; mContext = context; } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View arg0) { super(arg0); } ImageView mImg; } public interface OnItemClickListener{ void onClick(int position); } public void setOnItemClickListener(OnItemClickListener onItemClickListener ){ this.mOnItemClickListener =onItemClickListener; } @Override public int getItemCount() { return mDatas.size(); } /** * 建立ViewHolder */ @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = mInflater.inflate(R.layout.card_item, viewGroup, false); ViewHolder viewHolder = new ViewHolder(view); viewHolder.mImg = (ImageView) view.findViewById(R.id.card_item_img); return viewHolder; } /** * 設定值 */ @Override public void onBindViewHolder(final ViewHolder viewHolder, @SuppressLint("RecyclerView")int i) { viewHolder.mImg.setImageResource(mDatas.get(i)); if( mOnItemClickListener!= null){ viewHolder.itemView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { mOnItemClickListener.onClick(i); //卡片點選動畫 Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.scale_card); viewHolder.itemView.startAnimation(animation); } }); } } }
4.自定義彈性view
public class ReboundLayout extends LinearLayout implements NestedScrollingParent { private View mHeaderView; private View mFooterView; private static final int MAX_WIDTH = 200; private View mChildView; // 解決多點觸控問題 private boolean isRunAnim; private boolean isFlag = false; private int mDrag = 6;//除數越大可以滑動的距離越短 public ReboundLayout(Context context, AttributeSet attrs) { super(context, attrs); setOrientation(LinearLayout.HORIZONTAL); mHeaderView = new View(context); mHeaderView.setBackgroundColor(0xfff); mFooterView = new View(context); mFooterView.setBackgroundColor(0xfff); } public boolean isFlag() { return isFlag; } public void setFlag(boolean flag) { isFlag = flag; } @Override protected void onFinishInflate() { super.onFinishInflate(); mChildView = getChildAt(0); LayoutParams layoutParams = new LayoutParams(MAX_WIDTH, LayoutParams.MATCH_PARENT); addView(mHeaderView, 0, layoutParams); addView(mFooterView, getChildCount(), layoutParams); // 左移 scrollBy(MAX_WIDTH, 0); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); ViewGroup.LayoutParams params = mChildView.getLayoutParams(); params.width = getMeasuredWidth(); } /** * 必須要複寫 onStartNestedScroll後呼叫 */ @Override public void onNestedScrollAccepted(View child, View target, int axes) { } /** * 返回true代表處理本次事件 */ @Override public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) { if (target instanceof RecyclerView && !isRunAnim) { return true; } return false; } /** * 復位初始位置 */ @Override public void onStopNestedScroll(View target) { startAnimation(new ProgressAnimation()); } /** * 回彈動畫 */ private class ProgressAnimation extends Animation { // 預留 private float startProgress = 0; private float endProgress = 1; private ProgressAnimation() { isRunAnim = true; } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { float progress = ((endProgress - startProgress) * interpolatedTime) + startProgress; scrollBy((int) ((MAX_WIDTH - getScrollX()) * progress), 0); if (progress == 1) { //滑動停止 isRunAnim = false; isFlag = false; } } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); setDuration(200); } } @Override public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { } @Override public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) { // 如果在自定義ViewGroup之上還有父View交給我來處理 getParent().requestDisallowInterceptTouchEvent(true); // dx>0 往左滑動 dx<0往右滑動 boolean hiddenLeft = dx > 0 && getScrollX() < MAX_WIDTH && !ViewCompat .canScrollHorizontally(target, -1); boolean showLeft = dx < 0 && !ViewCompat.canScrollHorizontally(target, -1); boolean hiddenRight = dx < 0 && getScrollX() > MAX_WIDTH && !ViewCompat .canScrollHorizontally(target, 1); boolean showRight = dx > 0 && !ViewCompat.canScrollHorizontally(target, 1); if (hiddenLeft || showLeft || hiddenRight || showRight) { scrollBy(dx / mDrag, 0); consumed[0] = dx; } // 限制錯位問題 if (dx > 0 && getScrollX() > MAX_WIDTH && !ViewCompat.canScrollHorizontally(target, -1)) { scrollTo(MAX_WIDTH, 0); } if (dx < 0 && getScrollX() < MAX_WIDTH && !ViewCompat.canScrollHorizontally(target, 1)) { scrollTo(MAX_WIDTH, 0); } isFlag = true; } @Override public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) { return false; } @Override public boolean onNestedPreFling(View target, float velocityX, float velocityY) { // 當RecyclerView在介面之內交給它自己慣性滑動 if (getScrollX() == MAX_WIDTH) { return false; } return true; } @Override public int getNestedScrollAxes() { return 0; } /** * 限制滑動 移動x軸不能超出最大範圍 */ @Override public void scrollTo(int x, int y) { if (x < 0) { x = 0; } else if (x > MAX_WIDTH * 2) { x = MAX_WIDTH * 2; } super.scrollTo(x, y); } }
5.activity呼叫
public class MainActivity extends AppCompatActivity { private List<Integer> mDatas; private RecyclerView mRecyclerView; private HomeCardAdapter mAdapter; private ReboundLayout movingView; private LinearLayout linearLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRecyclerView.setLayoutManager(linearLayoutManager); //設定介面卡 mAdapter = new HomeCardAdapter(this, mDatas); mRecyclerView.setAdapter(mAdapter); mAdapter.setOnItemClickListener(new HomeCardAdapter.OnItemClickListener() { @Override public void onClick(int position) { switch (position){ case 0: Toast.makeText(MainActivity.this, "第"+position+"個", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(MainActivity.this, "第"+position+"個", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(MainActivity.this, "第"+position+"個", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(MainActivity.this, "第"+position+"個", Toast.LENGTH_SHORT).show(); break; case 4: Toast.makeText(MainActivity.this, "第"+position+"個", Toast.LENGTH_SHORT).show(); break; case 5: Toast.makeText(MainActivity.this, "第"+position+"個", Toast.LENGTH_SHORT).show(); break; case 6: Toast.makeText(MainActivity.this, "第"+position+"個", Toast.LENGTH_SHORT).show(); break; case 7: Toast.makeText(MainActivity.this, "第"+position+"個", Toast.LENGTH_SHORT).show(); break; } } }); mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(@NonNull @NotNull RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE && movingView.isFlag()) { if (!recyclerView.canScrollHorizontally(1)) { //回彈動畫 springAnim(movingView,-100,0,200,6); } else if (!recyclerView.canScrollHorizontally(-1)) { springAnim(mRecyclerView,100,0,200,6); } } movingView.setFlag(false); } }); } private void initData() { movingView = findViewById(R.id.view); linearLayout = findViewById(R.id.home_card_layout); mRecyclerView = findViewById(R.id.rv); mDatas = new ArrayList<>(Arrays.asList(R.drawable.home_control,R.drawable.home_navigation,R.drawable.home_media, R.drawable.home_hvac,R.drawable.home_energy,R.drawable.home_account,R.drawable.home_iot)); } /** * * @param view 執行動畫的view * @param from 幅度最大值 * @param to 結束值 * @param tension 張力 * @param friction 摩擦力(值越小 彈的次數越多 0為無限迴圈 參考值 4) */ private void springAnim(final View view,float from,float to,double tension,double friction){ SpringSystem system = SpringSystem.create(); Spring spring = system.createSpring(); spring.setCurrentValue(from); spring.setSpringConfig(new SpringConfig(tension, friction)); spring.addListener(new SimpleSpringListener(){ @Override public void onSpringUpdate(Spring spring) { view.setTranslationX((float) spring.getCurrentValue()); } }); spring.setEndValue(to); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45