关于gridview多item选中放大问题。

xiaoxiao2021-02-28  192

在前几天开发中遇到一个问题,就是要求选中放大gridview的item。然后就开始重写gridview ,发现按照一般的做法会发现空指针异常的现象。以下是我一开始做的方法:

@Override protected int getChildDrawingOrder(int childCount, int i) { if (position != -1) { if (i == childCount - 1) return position; if (i == position){ return childCount - 1; } } return i; } 重写gridview,的绘制顺序,把选中的item和最后一个绘制的item调换一下顺序。 public void setScaleAnimation(View view,int position) { AnimationSet animationSet = new AnimationSet(true); if (manimationSet != null && manimationSet != animationSet) { manimationSet.setFillAfter(false); manimationSet.cancel(); } ScaleAnimation scaleAnimation; if (position % 4 == 3){ scaleAnimation = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f, 1, 1f, 1, 0.5f); }else { scaleAnimation = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f, 1, 0.5f, 1, 0.5f); } scaleAnimation.setDuration(500); animationSet.addAnimation(scaleAnimation); animationSet.setFillAfter(true); view.startAnimation(animationSet); manimationSet = animationSet; }

上面是放大动画,最基础的动画。

当item多的时候会报错,因为position一直增加,而childcount是可见的数目,当所有的item都是可见的时候将不会出错。所有要修改一下代码。修改为以上的代码就不出错了

@Override protected int getChildDrawingOrder(int childCount, int i) { // return super.getChildDrawingOrder(childCount, i); if (this.getSelectedItemPosition() != -1) { if (i + this.getFirstVisiblePosition() == this.getSelectedItemPosition()) {// 这是原本要在最后一个刷新的item return childCount - 1; } if (i == childCount - 1) {// 这是最后一个需要刷新的item return this.getSelectedItemPosition() - this.getFirstVisiblePosition(); } } return i; } 以上的代码就是兼顾可见可不见的条目,这时候就适配左右的不管多或少的item报空指针异常问题。

以上是我遇见并解决的问题,欢迎大家交流。

转载请注明原文地址: https://www.6miu.com/read-43068.html

最新回复(0)