Điều này đã khiến tôi bối rối trong một vài ngày tới (làm việc trước Ice Cream Sandwich) nhưng tôi nghĩ Tôi cuối cùng cũng đã đến đó!(nhờ Gautam K và Mike Israel dẫn đầu) Cuối cùng tôi đã mở rộng View
(FrameLayout
) để bắt đầu dịch các hoạt ảnh phải/trái theo yêu cầu và nghe phần cuối của hoạt ảnh để di chuyển FrameLayout
phải của tôi/trái như thích hợp, như sau:
public class SlidingFrameLayout extends FrameLayout
{
private final int durationMilliseconds = 1000;
private final int displacementPixels = 200;
private boolean isInOriginalPosition = true;
private boolean isSliding = false;
public SlidingFrameLayout(Context context)
{
super(context);
}
public SlidingFrameLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SlidingFrameLayout(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onAnimationEnd()
{
super.onAnimationEnd();
if (isInOriginalPosition)
offsetLeftAndRight(displacementPixels);
else
offsetLeftAndRight(-displacementPixels);
isSliding = false;
isInOriginalPosition = !isInOriginalPosition;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
super.onLayout(changed, left, top, right, bottom);
// need this since otherwise this View jumps back to its original position
// ignoring its displacement
// when (re-)doing layout, e.g. when a fragment transaction is committed
if (changed && !isInOriginalPosition)
offsetLeftAndRight(displacementPixels);
}
public void toggleSlide()
{
// check whether frame layout is already sliding
if (isSliding)
return; // ignore request to slide
if (isInOriginalPosition)
startAnimation(new SlideRightAnimation());
else
startAnimation(new SlideLeftAnimation());
isSliding = true;
}
private class SlideRightAnimation extends TranslateAnimation
{
public SlideRightAnimation()
{
super(
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, displacementPixels,
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0);
setDuration(durationMilliseconds);
setFillAfter(false);
}
}
private class SlideLeftAnimation extends TranslateAnimation
{
public SlideLeftAnimation()
{
super(
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, -displacementPixels,
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0);
setDuration(durationMilliseconds);
setFillAfter(false);
}
}
}
Và, cuối cùng, để trượt SlidingFrameLayout
phải/trái, tất cả các bạn phải làm là gọi phương thức SlidingFrameLayout.toggleSlide()
. Tất nhiên bạn có thể tinh chỉnh số này SlidingFrameLayout
cho mục đích của bạn để trượt nhiều pixel hơn, để trượt lâu hơn, nhưng điều này là đủ để bạn bắt đầu :)
@AdilHussain: sau đó bạn phải triển khai chức năng với chuỗi trên điện thoại không hỗ trợ. Chỉ cần kiểm tra xem điện thoại có hỗ trợ hay viết một số dự phòng đơn giản. Đó là lựa chọn tốt nhất. – Gautam
Tìm thấy thư viện tuyệt vời này, công việc: [http://nineoldandroids.com] (http://nineoldandroids.com). Thư viện có sẵn để tải xuống dưới dạng 'jar' từ đây: [https://github.com/JakeWharton/NineOldAndroids/downloads](https://github.com/JakeWharton/NineOldAndroids/downloads) –
Có vẻ như tôi đã nói quá sớm. Khi thực hiện việc dịch bằng thư viện [nineoldandroids] (https://github.com/JakeWharton/NineOldAndroids) trên điện thoại cũ (pre-Honeycomb), các nút trong chế độ xem đã dịch dường như đã di chuyển nhưng vẫn có thể nhấp được ở vị trí cũ của chúng (và không phải vị trí mới của họ). Tôi đã gửi email cho tác giả của thư viện nhưng chưa nhận được phản hồi. –