寻呼机适配器实例化项在每次滚动时都会被调用



我有一个使用寻呼机Coverflow库的活动。但是我看到一个问题,每次滚动时,都会在限制中的所有项目上再次调用instantiateItem(在这种情况下为 3,因为屏幕外限制设置为 1(。我的寻呼机只有 2 个项目,因此所有项目都应该在一次调用中加载以实例化每个项目的 Item(这意味着无论我滚动多少次,总共 2 次调用(,而不是每次滚动 2 次调用。这使得卷轴不光滑和滞后,一点也不漂亮。我试图将其与使用寻呼机设置一个全新的项目并在我的设备上运行进行比较,并且可以流畅地滚动。

知道我在搞砸什么吗?

我看到的日志是(您可以在下面的适配器代码中看到日志行(:

I/MyViewAdapter.class: [Wed Jan 08]>>>> Instantiate item in my view adapter called. Position: 0
I/MyViewAdapter.class: [Wed Jan 08]>>>> Instantiate item in my view adapter ended. Position: 0
I/MyViewAdapter.class: [Wed Jan 08]>>>> Instantiate item in my view adapter called. Position: 1
I/MyViewAdapter.class: [Wed Jan 08]>>>> Instantiate item in my view adapter ended. Position: 1

XML 如下所示:

我的活动.xml

...
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="270dp"
android:background="@android:color/transparent">
<me.crosswall.lib.coverflow.core.PagerContainer
android:id="@+id/pager_container"
android:layout_width="match_parent"
android:layout_height="275dp">
<androidx.viewpager.widget.ViewPager
android:id="@+id/overlap_pager"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:overScrollMode="never"/>
</me.crosswall.lib.coverflow.core.PagerContainer>
</FrameLayout>
...

我的视图项.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@android:color/transparent">
<androidx.cardview.widget.CardView
android:id="@+id/cardview"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
app:cardCornerRadius="4dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="false"
app:cardPreventCornerOverlap="true"
android:background="@android:color/transparent"
app:cardBackgroundColor="@android:color/transparent">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profilePicture"
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:scaleType="centerCrop"
app:civ_border_color="#fff"
app:civ_border_width="2dip"
android:alpha="0.5"
/>
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:progressBackgroundTint="@color/colorPrimary"
android:progressTint="@color/colorPrimary"
android:layout_centerInParent="true"
android:layout_gravity="center"
/>
</FrameLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>

在活动中,我正在onCreate加载寻呼机,如下所示:

我的活动.java

PagerContainer container = (PagerContainer) findViewById(R.id.pager_container);
container.setOverlapEnabled(true);
pager = container.getViewPager();
pager.setAdapter(new MyViewAdapter(MyActivity.this, models, firstPosition));
pager.setOffscreenPageLimit(2);
// This is related to layout of the pager container: https://github.com/crosswall/Android-Coverflow
CoverFlow coverFlow = new CoverFlow.Builder()
.with(pager)
.scale(0.3f)
.pagerMargin(getResources().getDimensionPixelSize(R.dimen.pager_margin))
.spaceSize(0f)
.build();
pager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
Log.i(TAG, String.format("onPageSelected: (prevPosition, position) = (%d, %d)", prevPosition, position));
ImageView imageView = pager.findViewWithTag(pagerViewTagForPosition(position));
imageView.setAlpha(1f);
ImageView imageViewPrev;
if (prevPosition == -1) {
imageViewPrev = pager.findViewWithTag(pagerViewTagForPosition(firstPosition));
} else {
imageViewPrev = pager.findViewWithTag(pagerViewTagForPosition(prevPosition));
}
imageViewPrev.setAlpha(0.5f);
prevPosition = position;
// Update background and animation objects
}
});

在适配器中: 我的视图适配器.java

@Override
public Object instantiateItem(ViewGroup container, final int position) {
Log.i(TAG, String.format("[%s]>>>> Instantiate item in my view adapter called. Position: %d", new Date(), position));
View view = LayoutInflater.from(context).inflate(R.layout.my_view,null);
final CircleImageView circleImageView = (CircleImageView) view.findViewById(R.id.profilePicture);
final ProgressBar progressbar = (ProgressBar) view.findViewById(R.id.progressbar);
circleImageView.setTag("tag_" + position);
Picasso.get().load(models.get(position)).into(circleImageView, new Callback() {
@Override
public void onSuccess() {
progressbar.setVisibility(View.GONE);
}
@Override
public void onError(Exception e) {
Log.e(TAG, String.format("Failed to image. Error: %s", e));
}
});
}
container.addView(view);
Log.i(TAG, String.format("[%s]>>>> Instantiate item in my view adapter ended. Position: %d", new Date(), position));
return view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@Override
public int getCount() {
return models.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}

发现我使用的库每次都在调用实例化项目进行页面选择。所以解决方案是只编写我自己的PagerContainer来扩展库中的PagerContainer(因为库确实使视图看起来很漂亮(,并覆盖onPageSelected方法:

public class MyViewContainer extends PagerContainer {
public MyViewContainer(Context context) {
super(context);
init();
}
public MyViewContainer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyViewContainer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
//Disable clipping of children so non-selected pages are visible
setClipChildren(false);
//Child clipping doesn't work with hardware acceleration in Android 3.x/4.x
//You need to set this value here if using hardware acceleration in an
// application targeted at these releases.
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
@Override
public void onPageSelected(int position) {
// Override the page selected from the library since that one calls instantiate item again.
}
}

在 XML 文件中也使用您自己的容器。应该就是这样。

相关内容

  • 没有找到相关文章

最新更新