我的目标是在片段1拉到视觉视图之前,在片段1上有一个滑动视图,滑动一定距离。
我在这里制作了一个视觉,展示了我要做的事情: 图1显示片段1(粉红色)和滑动视图(绿色)的默认视图,在此状态下未发生任何事情。
图2滑动视图可以滑动到它的截止(黑线)。它不能在这个片段上移过它。
图3整个片段1开始滑动,片段2(蓝色)现在开始被拉入视图
图片4两个片段都像以前一样继续滑动。
Pic 5片段2现已滑入视野。
当用户尝试返回上一个屏幕时,转换反向发生。
现在我已经实现了一个托管片段1和2的ViewPager。默认的ViewPager行为允许我在两个片段之间滑动,但我不确定如何实现我所描述的功能。
答案 0 :(得分:0)
我能够使用ViewPager PageTransformer
实现所需的过渡我在项目中使用Xamarin.Android,因此使用了C#。虽然这个代码片段在C#中,但可以很容易地移植到Java代码中以便在其他项目中使用。
float passpoint = 7/8f;
public void TransformPage(View view, float position)
{
var pageWidth = view.Width;
var slidingGap = pageWidth - pageWidth * passpoint;
var translation = position * pageWidth;
if (position < -1)
{ // [-Infinity,-1)
// This page is way off-screen to the left.
view.Alpha = 0;
}
else if (position <= 1)
{ // [-1,1]
var r = new Rect();
//if valid, this view is the startpage
Button button = (Button)view.FindViewById(Resource.Id.slidingButton);
if (button != null)
{
var posPassPoint = -(1 - passpoint);
float ratio;
//create sliding gap
if (position > posPassPoint)
{
ratio = RatioBetween(position, posPassPoint, 0);
button.TranslationX = slidingGap * -ratio;
}
//compensate for sliding gap after it has been reached
else
{
//1- bc want the ratio to shrink
ratio = 1-RatioBetween(position, -1, posPassPoint);
}
view.TranslationX = slidingGap * ratio;
}
//valid on views other than start page (no sliding button)
if (button == null)
{
float ratio;
//create sliding gap
if (position >= passpoint)
ratio = RatioBetween(position, passpoint, 1);
//compensate for sliding gap after it has been reached
else
ratio = 1 - RatioBetween(position, 0, passpoint);
view.TranslationX = slidingGap * ratio;
}
}
else
{ // (1,+Infinity]
// This page is way off-screen to the right.
view.Alpha = 0;
}
}
float RatioBetween(float input, float upperBound, float lowerBound)
{
return (input - lowerBound) / (upperBound - lowerBound);
}