工作需要,做一個左劃的事件,類似淘寶的商品詳情的輪播圖,最後一個左劃到詳情,第一個想法,RefreshLayout橫過來就好了,但是時間不允許,後來就一直耽擱了,最後使用的一個簡單的辦法:很low
使用 HorizontalScrolView 通過滑動監聽做到
直接貼下代碼吧,沒啥難度;
<HorizontalScrollView android:id="@+id/horizontalScrollView" android:layout_width="match_parent" android:layout_height="@dimen/dp_341" android:scrollbars="none"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/iv_goodsDetail_pic" android:layout_width="match_parent" android:layout_height="@dimen/dp_341" android:scaleType="centerCrop" android:src="@mipmap/goodsimg" /> <LinearLayout android:layout_toRightOf="@+id/iv_goodsDetail_pic" android:id="@+id/ll_zuohua" android:layout_width="90dp" android:layout_height="match_parent" android:background="#eeeeee" android:orientation="horizontal"> <ImageView android:id="@+id/iv_zuohua" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="@dimen/dp_5" android:src="@mipmap/zuohua" /> <TextView android:id="@+id/tv_zuohua" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="@dimen/dp_5" android:text="@string/zuohua" android:textColor="#808080" android:textSize="12sp" /> </LinearLayout> </HorizontalScrollView>
horizontalScrollView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: LogUtils.i("onTouch: " + "ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: int zuohua1 = ll_zuohua.getMeasuredWidth() / 2; LogUtils.i("onTouch: " + "ACTION_MOVE" + "\nv.getScrollX()" + v.getScrollX()); if (v.getScrollX() >= zuohua1 && rotateState.equals("zuohua")) { RotateAnimation rotateAnimation1 = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation1.setDuration(500); rotateAnimation1.setFillAfter(true); iv_zuohua.startAnimation(rotateAnimation1); tv_zuohua.setText(R.string.shifang); rotateState = "shifang"; } else if (v.getScrollX() < zuohua1 && rotateState.equals("shifang")) { RotateAnimation rotateAnimation1 = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation1.setDuration(500); rotateAnimation1.setFillAfter(true); iv_zuohua.startAnimation(rotateAnimation1); tv_zuohua.setText(R.string.zuohua); rotateState = "zuohua"; } break; case MotionEvent.ACTION_UP: int zuohua = ll_zuohua.getMeasuredWidth() / 2; if (v.getScrollX() > zuohua) { LogUtils.i("onTouch: " + "ACTION_UP1" + "\nv.getScrollX()" + v.getScrollX() + "\nzuohuaWidth/2" + zuohua); v.scrollBy(ll_zuohua.getMeasuredWidth(), 0); if (drawUrl == null) { } else { intent.putExtra("key", "goods_detail"); intent.putExtra("goodsname", goodsName); intent.putExtra("drawUrl", drawUrl); startActivity(intent); v.scrollTo(0, 0); rotateState = "zuohua"; } } else { LogUtils.i("onTouch: " + "ACTION_UP2" + "\nv.getScrollX()" + v.getScrollX() + "\nzuohuaWidth/2=" + v.getScrollY()); v.scrollTo(0, 0); } iv_zuohua.clearAnimation(); tv_zuohua.setText(R.string.zuohua); break; } return false; } });