向上滚动时展开带有动画的Recyclerview项目

时间:2015-09-01 09:30:32

标签: android android-layout android-recyclerview recycler-adapter recyclerview-layout

Iphone App video link

如何设计和开发上述视频中发布的视图?这基本上是recyclelerview与动画的项目扩展。我尝试过使用recyclelerview的onItemtouchlistener以及一些带动画的自定义视图,但没有得到准确的结果。

最后我来到了addonscrolllistener,这给了我结果但不准确。

 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if(newState == RecyclerView.FOCUS_UP) {
                System.out.println("hello, ia m going up");
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (dy > 0){
                TextView tv = (TextView)recyclerView.findViewById(R.id.title);
                //tv.setVisibility(View.VISIBLE);
                if (tv.getVisibility()==View.VISIBLE){
                     System.out.println("yes");
                }else {
                    slideToTop(tv);
                }
            }
        }
    });

 private void slideToTop(View view){
    TranslateAnimation animate = new TranslateAnimation(0,0,0,-view.getHeight());
    animate.setDuration(1000);
    animate.setFillAfter(false);
    view.startAnimation(animate);
    view.setVisibility(View.VISIBLE);
}

2 个答案:

答案 0 :(得分:2)

我认为你的问题太宽泛了 - 但是,这里有一些伪代码:

onScrolled() {
    View child = getSecondVisibleChild();
    int distanceFromTop = child.getTop();
    int distanceAtWhichExpandingShouldOccur = 100;
    if(distanceFromTop < distanceAtWhichExpandingShouldOccur ) {
         child.setHeight(child.getOriginalHeight() + (distanceFromTop - distanceAtWhichExpandingShouldOccur))
    }
}

所以你注意到,第二个可见的孩子是身高变化的孩子。当它从窗口顶部小于distanceAtWhichExpandingShouldOccur时会发生变化。它的高度变化以确保其bottom保持静止 - 因此它的高度以top移动的速度增加。

一旦它不再是第二个可见的孩子(也就是它的top为0),它应该正常滚动, next 孩子应该有它的高度 顶部小于distanceAtWhichExpandingShouldOccur时更改。

答案 1 :(得分:0)

此库可以作为您的研究案例:https://github.com/florent37/MaterialLeanBack