【问题标题】:Restore the x offset of a listview item恢复列表视图项的 x 偏移量
【发布时间】:2014-05-12 12:02:21
【问题描述】:

我正在实现BaseAdapter,并且我为单个列表项设置了 TranslateAnimation。

问题是向下滚动时其他视图的偏移量相同。

这是我对getView() 方法的实现:

        @Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null) {
            //initialization code
            convertView.setTag(viewHolder);
    } else {
         viewHolder =(ViewHolder) convertView.getTag();
    }

        Animation animation;
        switch (item.getAnimationDirection()) {
        case AnimationDirection.HIDE:
            animation = new TranslateAnimation(itemOffset, 0, 0, 0);
            animation.setDuration(200);
            animation.setFillAfter(true);
            viewHolder.layoutToAnimate.setAnimation(animation);
            item.setAnimationDirection(AnimationDirection.HIDDEN);
            break;
        case AnimationDirection.REVEAL:
            itemOffset = viewHolder.remove.getWidth() + 20;
            animation = new TranslateAnimation(0, itemOffset, 0, 0);
            animation.setDuration(200);
            animation.setFillAfter(true);
            viewHolder.layoutToAnimate.setAnimation(animation);
            item.setAnimationDirection(AnimationDirection.SHOWING);
            break;
        }

        viewHolder.remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (item.getAnimationDirection() == AnimationDirection.HIDDEN) {
                    item.setAnimationDirection(AnimationDirection.REVEAL);
                    notifyDataSetChanged();
                }
            }
        });
        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (item.getAnimationDirection() == AnimationDirection.SHOWING) {
                    item.setAnimationDirection(AnimationDirection.HIDE);
                    notifyDataSetChanged();
                }
            }
        });
        return convertView;
  }

我的问题是:

如何为每个 ListView 项目保持 View 偏移状态?

【问题讨论】:

    标签: android


    【解决方案1】:

    Android 框架提供了保存和恢复视图属性的钩子,因此您需要做的就是确保您的视图代码实现 onSaveInstanceState()onRestoreInstanceState(Parcelable state),并在调用时为其子视图调用相同的代码并调用父类的方法,例如super.onSaveInstanceState().

    我发布了一个答案here,以回答有关保留 ListView 中使用的数据的问题,但也讨论了如何保留其他视图属性,例如 cmets 中的滚动位置。

    【讨论】:

    • 我可以保存每个项目的偏移量,但是如何在 getView() 方法中设置偏移量?
    • 您不需要设置偏移量。当调用onRestoreInstanceState 时,您的视图的父类将执行此操作。如果您最初想明确设置水平位置,那么您的视图必须是支持水平滚动的视图的子类(或内部) - 您可以在其上以编程方式设置滚动位置 - 请参阅developer.android.com/reference/android/widget/…
    【解决方案2】:

    为此使用Map<Integer, Integer>。类似的东西

    map.put(position, offset);
    

    int offset = map.get(position);
    

    不要忘记设置这个每次调用getView

    【讨论】:

    • 我应该在哪里存储视图偏移量?在我开始动画之后?
    • 每当它针对特定位置发生变化时。你可以使用AnimatorUpdateListener
    • 我怎样才能恢复偏移量?当我在适配器内部调用 setX() 时它似乎不起作用,我的意思是有没有办法在适配器内部实现它而不使用自定义列表视图实现?
    【解决方案3】:

    我遇到了类似的问题,但我的问题是颜色,并且有 2 个列表视图并排放置。我正在将列表上的项目设置为红色,该项目在另一个列表上的相同位置上被触及。正确的条目被突出显示,但列表中的另一个项目也处于任意位置。我解决它的方法是使用 if-else 语句(我只在之前使用过)。我在 getview() 中的代码是这样的:

    if(position == highLightPosition){
    holder.layout.setBackgroundColor(getResources().getColor(R.color.high_red));
    }else{
    //I set default color here that solved my problem
    holder.layout.setBackgroundColor(getResources().getColor(R.color.white));
    }
    

    尝试这样的事情,我知道开关,如果看起来一样,但这对我有用。

    【讨论】:

      猜你喜欢
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      相关资源
      最近更新 更多