【问题标题】:Android item swiping on listviewAndroid项目在listview上滑动
【发布时间】:2014-03-18 11:34:04
【问题描述】:

几天以来,我一直在 Android Listview 上为这个话题而苦苦挣扎,但我似乎没有做对。我只是不明白该怎么做。我已经尽我所能了解Adapters(特别是BaseAdapter),但仍然想不出办法做到这一点。

我在网上搜索过信息,但没有完全得到。

  • 我想做的是:我想创建一个ListView 联系人。
  • 每行有 3 个水平部分:一张固定的照片、内容 x 和内容 y(最后一张不在屏幕上且不可见)
  • 我希望当用户滑动单个项目时 从右到左,内容(带有信息 x)淡出。这 其他内容(带有信息 Y)从屏幕外滑入, 同时具有从右到左的方向。
  • 当用户 向后滑动(从左到右)内容 y 再次向外滑动并且 初始内容 x 淡入。

我无法做到这一点,所以我请求你的帮助。 非常感谢您的时间和精力

【问题讨论】:

    标签: java android xml android-listview adapter


    【解决方案1】:

    将这样的动画添加到 ListView 是没有问题的,我在几分钟内为普通的非自定义 ListView 构建了这个解决方案,通常这种事情适用于任何开箱即用的 ListView,这一切都在适配器中。我的答案中唯一缺少的是滑动检测,不幸的是我现在没有时间测试它。但是滑动检测并不难,如果你用谷歌搜索的话,会有很多例子。无论如何,如果您有任何问题,请随时提出。

    结果:

    我使用的是带有 ViewHolder 模式的简单 BaseAdapter,没什么特别的,无论如何我都会发布 getView 方法以进行澄清:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        if(getItemId(position) == TEST_VIEW_ID) {
            TestViewModel viewModel = (TestViewModel) getItem(position);
    
            TestRow row;
            if(convertView == null) {
                convertView = this.inflater.inflate(TestRow.LAYOUT, parent, false);
                row = new TestRow(convertView); // Here the magic happens
                convertView.setTag(row);
            }
    
            row = (TestRow) convertView.getTag();
            row.bind(viewModel);
        }
    
        return convertView;
    }
    

    在我的 ViewHolder 类中,这里称为 TestRow 我为动画创建了一些辅助方法,我将在下面进一步解释它们,但这里首先是来自 TestRow 的代码:

    public class TestRow {
    
        public static final int LAYOUT = R.layout.list_item_test;
    
        public ImageView ivLogo;
        public TextView tvFadeOut;
        public TextView tvSlideIn;
    
        public TestRow(View view) {
            this.ivLogo = (ImageView) view.findViewById(R.id.ivLogo);
            this.tvFadeOut = (TextView) view.findViewById(R.id.tvFadeOut);
            this.tvSlideIn = (TextView) view.findViewById(R.id.tvSlideIn);
    
            this.ivLogo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // When the ImageView is clicked the animations are applied to the TextViews.
                    if(tvFadeOut.getVisibility() == View.VISIBLE) {
                        fadeOutView(tvFadeOut);
                        slideInView(tvSlideIn);
                    } else {
                        fadeInView(tvFadeOut);
                        slideOutView(tvSlideIn);
                    }
                }
            });
        }
    
        public void bind(TestViewModel viewModel) {
            // Nothing to do here
        }
    }
    

    这里是我用于动画的辅助方法:

    private void fadeOutView(View view) {
        Animation fadeOut = AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_out);
        if (fadeOut != null) {
            fadeOut.setAnimationListener(new ViewAnimationListener(view) {
                @Override
                protected void onAnimationStart(View view, Animation animation) {
    
                }
    
                @Override
                protected void onAnimationEnd(View view, Animation animation) {
                    view.setVisibility(View.GONE);
                }
            });
            view.startAnimation(fadeOut);
        }
    }
    
    private void fadeInView(View view) {
        Animation fadeIn = AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_in);
        if (fadeIn != null) {
            fadeIn.setAnimationListener(new ViewAnimationListener(view) {
                @Override
                protected void onAnimationStart(View view, Animation animation) {
                    view.setVisibility(View.VISIBLE);
                }
    
                @Override
                protected void onAnimationEnd(View view, Animation animation) {
    
                }
            });
            view.startAnimation(fadeIn);
        }
    }
    
    private void slideInView(View view) {
        Animation slideIn = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_in_right);
        if (slideIn != null) {
            slideIn.setAnimationListener(new ViewAnimationListener(view) {
                @Override
                protected void onAnimationStart(View view, Animation animation) {
                    view.setVisibility(View.VISIBLE);
                }
    
                @Override
                protected void onAnimationEnd(View view, Animation animation) {
    
                }
            });
            view.startAnimation(slideIn);
        }
    }
    
    private void slideOutView(View view) {
        Animation slideOut = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_out_right);
        if (slideOut != null) {
            slideOut.setAnimationListener(new ViewAnimationListener(view) {
                @Override
                protected void onAnimationStart(View view, Animation animation) {
    
                }
    
                @Override
                protected void onAnimationEnd(View view, Animation animation) {
                    view.setVisibility(View.GONE);
                }
            });
            view.startAnimation(slideOut);
        }
    }
    
    private abstract class ViewAnimationListener implements Animation.AnimationListener {
    
        private final View view;
    
        protected ViewAnimationListener(View view) {
            this.view = view;
        }
    
        @Override
        public void onAnimationStart(Animation animation) {
            onAnimationStart(this.view, animation);
        }
    
        @Override
        public void onAnimationEnd(Animation animation) {
            onAnimationEnd(this.view, animation);
        }
    
        @Override
        public void onAnimationRepeat(Animation animation) {
    
        }
    
        protected abstract void onAnimationStart(View view, Animation animation);
        protected abstract void onAnimationEnd(View view, Animation animation);
    }
    

    这些是我使用的动画 xml:

    淡入:

    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:shareInterpolator="false">
        <alpha
                android:fromAlpha="0"
                android:toAlpha="1"
                android:duration="700"/>
    </set>
    

    淡出:

    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:shareInterpolator="false">
        <alpha
                android:fromAlpha="1"
                android:toAlpha="0"
                android:duration="700"/>
    </set>
    

    滑入:

    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:shareInterpolator="false">
        <translate
                android:fromXDelta="100%" android:toXDelta="0%"
                android:duration="700"/>
    </set>
    

    滑出:

    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:shareInterpolator="false">
        <translate
                android:fromXDelta="0%" android:toXDelta="100%"
                android:duration="700"/>
    </set>
    

    【讨论】:

      【解决方案2】:

      在 res/anim/ 中使用这个 xml(用于动画目的)

      这是从左到右的动画:

      <set xmlns:android="http://schemas.android.com/apk/res/android"
       android:shareInterpolator="false">
        <translate android:fromXDelta="-100%" android:toXDelta="0%"
               android:fromYDelta="0%" android:toYDelta="0%"
               android:duration="700"/>
      </set>
      

      这是从右到左的动画:

      <set xmlns:android="http://schemas.android.com/apk/res/android"
       android:shareInterpolator="false">
        <translate
       android:fromXDelta="0%" android:toXDelta="100%"
       android:fromYDelta="0%" android:toYDelta="0%"
       android:duration="700" />
      </set>
      

      在您的编码中使用从左到右的意图:

      this.overridePendingTransition(R.anim.animation_enter,
                     R.anim.animation_leave);
      

      在您的编码中使用从右到左的意图

      this.overridePendingTransition(R.anim.animation_leave,
                                 R.anim.animation_enter);
      

      对于自定义列表视图,您可以使用以下代码: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

      只需捕捉 Gesture 事件并应用动画,并制作一个将在手势事件上出现-消失的布局。


      如果你是出于正常目的使用,而不是你想要的工作示例: https://github.com/47deg/android-swipelistview

      【讨论】:

      • 第二个链接不再可用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      相关资源
      最近更新 更多