gpt4 book ai didi

android - 应用 PageTransformer 时,ViewPager 中的 ListView 不会滚动

转载 作者:太空狗 更新时间:2023-10-29 15:38:44 24 4
gpt4 key购买 nike

我有一个 ViewPager,其中的页面包含 ListView。一切正常,我的 viewPAger 和 ListViews 都按预期工作:可以从一个页面滑动到另一个页面,并且 listviews 可以垂直滚动。

现在我想添加一个 PageTransformer 来平滑分页,我使用了 ZoomOutPageTransformer在谷歌文档中提供。

现在,在 View 之间滑动时我有一个漂亮的动画,但列表不再可滚动。

代码如下:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(getActivity());
viewPager = (ViewPager) view.findViewById(R.id.bookMenuPager);
viewPager.setPageTransformer(false, new ZoomOutPageTransformer());
pagerAdapter = new MenuPagerAdapter();
viewPager.setAdapter(pagerAdapter);
}


class MenuPagerAdapter extends PagerAdapter{

@Override
public int getCount() {

return 3; //change this as needed
}

@Override
public boolean isViewFromObject(View view, Object o) {
return view.equals( o );
}

@Override
public Object instantiateItem(ViewGroup collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if(position == 0){
if(!rootMenuAdded){
viewPager.addView(rootMenucont, 0);
rootMenuAdded = true;
}
return rootMenucont;
}else if(position == 1){
if(!level1MenuAdded){
viewPager.addView(level1MenuCont, 0);
level1MenuAdded = true;
}
return level1MenuCont;
}else if(position == 2){
if(!level2MenuAdded){
viewPager.addView(level2MenuCont, 0);
level2MenuAdded = true;
}
return level2MenuCont;
}

//we got a problem houston
return null;
}
}

和页面布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/level1MenuCont"
android:layout_height="match_parent"
android:layout_width="match_parent"
>

<ListView
android:id="@+id/level1Menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f02bb6"
>
</ListView>

</RelativeLayout>

如何使我的列表按预期滚动? PageTransformer 在我的 ListView 中破坏了什么,使其不再滚动?这是已知错误吗?

感谢您的帮助:)

最佳答案

我想我已经找到解决此问题的方法。

经过一些调查,我认为只有当您应用一个 PageTransformer 来更改 Views 的坐标时才会发生这种情况,因此 它们都在彼此之上 (两个示例转换器就是这样做的)。

当您朝 NEW VIEW 的 Z-index 低于 OLD VIEW 的方向滑动时(通常向后滑动),这些转换器会发生什么情况,即 OLD VIEW 打开NEW VIEW 的顶部,Alpha==0,并且是后来获得“幽灵”触摸的那个。

不幸的是,@ngatyrauks bringToFront() 的解决方案对我不起作用(尽管它绝对应该)。

但是,我调整了转换器,将不可见的 views 的可见性更改为“GONE”。这就是诀窍。

我还没有调查此可见性更改是否有任何副作用(GONE View 将在布局中返回 null 和零等,所以这可能会破坏内部的其他内容ViewPager),但到目前为止它运行良好。

我在这里发布了经过调整的 DepthPageTransformer(与文档中的相同),其中包含这些更改。希望对大家有帮助!

            package com.regaliz.gui.fx;

import android.util.Log;
import android.view.View;
import android.support.v4.view.ViewPager;

public class DepthPageTransformer implements ViewPager.PageTransformer {

private static final String TAG="DepthTransformer";
private static float MIN_SCALE = 0.75f;

public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
Log.d(TAG, "VIew "+view+" Position: "+position);

if (position <= -1) { // [-Infinity,-1) ] ***

// RLP> I Changed to include "-1" as well: When position is -1, the view is not visible

// This page is way off-screen to the left.

view.setAlpha(0);
Log.d(TAG, "VIew "+view+" Position: "+position+", way left");
view.setVisibility(View.GONE);

} else if (position <= 0) { // [ (-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);
if (position==0) {
Log.d(TAG, "View "+view+" focused now?");
}

if (view.getVisibility()!=View.VISIBLE)
view.setVisibility(View.VISIBLE);

} else if (position <= 1) { // (0,1]

// Fade the page out.
view.setAlpha(1 - position);

// Counteract the default slide transition

// I THINK THIS IS WHAT BREAKS EVERYTHING
// ViewPager normally has the views one after another, but this makes all views on top

view.setTranslationX(pageWidth * -position);

// Scale the page down (between MIN_SCALE and 1)

float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);

if (position==1) {

Log.d(TAG, "View "+view+" invisible now?");
view.setVisibility(View.GONE);
// we totally hide the view. This seems to solve focus issue

} else {
if (view.getVisibility()!=View.VISIBLE)
view.setVisibility(View.VISIBLE);
}

} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);

// we totally hide the view. This seems to solve focus issue
// I have to check for strange side-effects, but so far I found none :)

view.setVisibility(View.GONE);

Log.d(TAG, "VIew "+view+" Position: "+position+", way right");
}
}
}

关于android - 应用 PageTransformer 时,ViewPager 中的 ListView 不会滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17223254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com