与RecyclerView视差头效果 [英] Parallax header effect with RecyclerView

查看:1111
本文介绍了与RecyclerView视差头效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变我的ListView我目前有超过使用 RecyclerView 这样我就可以使用 StaggeredGridLayoutManager ,但RecyclerView没有添加头像ListView控件的能力。

I want to change my ListView I currently have over to use RecyclerView so I can make use of StaggeredGridLayoutManager but RecyclerView does not have the ability to add a header like ListView.

通常与一个ListView我设置在头一个空的观点,并把图像列表视图下方和翻译底部图像列表的滚动来创建视差的效果。

Usually with a ListView I set an empty view in the header and put the image below the listview and translate the bottom image with the scrolling of the list to create the Parallax effect.

因此​​,与出我怎么可以创建一个 RecyclerView

So with out a header how can I create the same parallax effect with RecyclerView?

推荐答案

所以,今天我试图存档一个<$​​ C $ C> RecyclerView 的效果。我能做到这一点,但因为code是太多了,我会贴在这里我github上的项目,我会解释一些项目的关键点。

So today I tried to archive that effect on a RecyclerView. I was able to do it but since the code is too much I will paste here my github project and I will explain some of the key points of the project.

<一个href="https://github.com/kanytu/android-parallax-recyclerview">https://github.com/kanytu/android-parallax-recyclerview

首先,我们需要看看 getItemViewType RecyclerView.Adapter 类。这种方法定义了什么样的看法,我们正在处理。这种类型将被传递给 onCreateViewHolder 还有我们可以膨胀不同的看法。因此,我所做的是:检查位置是第一位的。如果是的话,抬高头部,如果不夸大普通行。

First we need to look at getItemViewType on the RecyclerView.Adapter class. This methods defines what type of view we're dealing with. That type will be passed on to onCreateViewHolder and there we can inflate different views. So what I did was: check if the position is the first one. If so then inflate the header, if not inflate a normal row.

我已经添加也是一个 CustomRelativeLayout 的剪辑的看法,所以我们没有与分隔,并与行获得的头顶部任何麻烦。

I've added also a CustomRelativeLayout that clips the view so we don't have any trouble with the dividers and with the rows getting on top of the header.

从这一点你似乎知道这背后的逻辑的其余部分。

From this point you seem to know the rest of the logic behind it.

最终的结果是:

编辑:

如果你需要插入一些适配器一定要加1的 notifyItemChanged /插入方法通知的正确位置。例如:

If you need to insert something in adapter make sure you notify the correct position by adding 1 in the notifyItemChanged/Inserted method. For example:

public void addItem(String item, int position) {
    mData.add(position, item);
    notifyItemInserted(position + 1); //we have to add 1 to the notification position since we don't want to mess with the header
}

另外一个重要的编辑我所做的就是滚动的逻辑。如果添加一个项目的 mCurrentOffset 系统我用的项目插入,因为补偿没工作就会改变。因此,我所做的是:

Another important edit I've done is the scroll logic. The mCurrentOffset system I was using didn't work with the item insertion since the offset will change if you add an item. So what I did was:

ViewHolder holder = findViewHolderForPosition(0);
if (holder != null)
  ((ParallaxRecyclerAdapter) getAdapter()).translateHeader(-holder.itemView.getTop() * 0.5f);

要测试这一点,我添加了一个 postDelayed Runnable接口,启动应用程序,滚动到最后,添加项目位置0,并再次向上滚动。其结果是:

To test this I added a postDelayed Runnable, started the app, scrolled to the end, add the item in position 0, and scroll up again. The result was:

如果有人正在寻找其他的视差效果,他们可以检查我的其他回购:

If anyone is looking for other parallax effects they can check my other repo:

<一个href="https://github.com/kanytu/android-parallax-listview">https://github.com/kanytu/android-parallax-listview

这篇关于与RecyclerView视差头效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆