简体   繁体   English

Android项目在列表视图上滑动

[英]Android item swiping on listview

I'm struggling with this topic on Android Listview since a few days and I don't seem to get it right. 几天以来,我一直在与Android Listview上的这个主题苦苦挣扎,但似乎做得不好。 I just can't understand how to do it. 我只是不明白该怎么做。 I've learned the best I could about Adapters ( BaseAdapter specially) but still couldn't think in a way to do this. 我已经对Adapters (特别是BaseAdapter )学到了最好的知识,但仍然想不出办法。

I've searched on the web for information but didn't quite get it. 我在网上搜索了一些信息,但并没有完全了解。

  • What I wish to do is the following: I want to create a ListView of contacts. 我想做的是以下几点:我想创建一个联系人的ListView
  • Each row has 3 horizontal sections: a photo that is constant, content x and content y (this last one is out of the screen and is invisible) 每行有3个水平部分:一张是恒定的照片,内容为x,内容为y(最后一个在屏幕外,不可见)
  • And I wish that when the user swipes a single item from right to left, the content (with information x) fades out. 我希望当用户从右向左滑动单个项目时,内容(带有信息x)会淡出。 The other content (with information Y) slides in from out of the screen, with right to left orientation, at the same time. 其他内容(信息为Y)同时从右向左从屏幕外滑入。
  • When the user swipes back (from left to right) the content y swipes out again and the initial content x fades in. 当用户向后滑动(从左到右)时,内容y再次滑出,并且初始内容x淡入。

I just can't to this, so I'm asking your help please. 我只是不能做到这一点,所以请你帮忙。 Thank you very much for your time and effort 非常感谢您的时间和精力

Adding such animations to a ListView is no problem, I built this solution for a normal non-custom ListView in a few minutes, generally this sort of thing works on any ListView out of the box, it's all just in the adapter. 将这样的动画添加到ListView没问题,我在几分钟内为普通的非自定义ListView构建了此解决方案,通常这种情况适用于任何开箱即用的ListView,它们全都在适配器中。 The only thing missing in my answer is the swipe detection, unfortunately I don't have the time to test that right now. 我的答案中唯一缺少的是滑动检测,很遗憾,我现在没有时间对其进行测试。 But swipe detection is not difficult and there are a ton of examples if you google it. 但是滑动检测并不困难,如果您使用Google进行滑动检测,则有很多示例。 Anyway if you have questions, feel free to ask. 无论如何,如果您有任何疑问,请随时提出。

Result: 结果:
在此处输入图片说明

I am using a simple BaseAdapter with ViewHolder pattern, nothing special, I will post the getView method anyway for clarification: 我使用的是带有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;
}

In my ViewHolder class, here called TestRow I created a few helper methods for the animations, I will explain them further below, but here first my code from TestRow : 在我的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
    }
}

And here are the helper methods i use for the animations: 这是我用于动画的辅助方法:

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);
}

These are the animation xml's I use: 这些是我使用的动画xml:

fade in: 淡入:

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

fade out: 淡出:

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

slide in: 滑入:

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

slide out: 滑出:

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

Use this xml in res/anim/ (FOR ANIMATION PURPOSE) 在res / anim /中使用此xml(用于动画目的)

This is for left to right animation: 这是从左到右的动画:

<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>

This is for right to left animation: 这是从右到左的动画:

<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>

In your coding use intent like for left to right: 在编码中,请使用从左到右的意图:

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

In your coding use intent like for right to left 在编码中使用意图,例如从右到左

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

For Custom List View u can use this Code: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ 对于自定义列表视图,您可以使用以下代码: http : //www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

Just catch Gesture event and apply animation, and make one layout which will appear-disappear on gesture event. 只需捕捉手势事件并应用动画,然后制作一种布局即可在手势事件中消失。


If u are Using for normal purpose than working example of exactly what u want is here: https://github.com/47deg/android-swipelistview 如果您将其用于正常用途,而不是您想要的实际工作示例,请访问: https : //github.com/47deg/android-swipelistview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM