Android: Collapsing Linearlayout instead of Collapsing Toolbar

前端 未结 4 728
生来不讨喜
生来不讨喜 2020-12-30 10:52

I\'m trying to create a Master/Detail transaction in a single fragment. I thought of using LinearLayout as the container of my edittext for my header. Then a RecyclerView fo

4条回答
  •  清歌不尽
    2020-12-30 11:06

    Based on RIP's tutorial, you can collapse or expand views using the snippet below.

    public class ViewAnimationUtils {
            
            public static void expand(final View v) {
                v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                final int targtetHeight = v.getMeasuredHeight();
        
                v.getLayoutParams().height = 0;
                v.setVisibility(View.VISIBLE);
                Animation a = new Animation()
                {
                    @Override
                    protected void applyTransformation(float interpolatedTime, Transformation t) {
                        v.getLayoutParams().height = interpolatedTime == 1
                                ? LayoutParams.WRAP_CONTENT
                                : (int)(targtetHeight * interpolatedTime);
                        v.requestLayout();
                    }
        
                    @Override
                    public boolean willChangeBounds() {
                        return true;
                    }
                };
        
                a.setDuration((int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
                v.startAnimation(a);
            }
        
            public static void collapse(final View v) {
                final int initialHeight = v.getMeasuredHeight();
        
                Animation a = new Animation()
                {
                    @Override
                    protected void applyTransformation(float interpolatedTime, Transformation t) {
                        if(interpolatedTime == 1){
                            v.setVisibility(View.GONE);
                        }else{
                            v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                            v.requestLayout();
                        }
                    }
        
                    @Override
                    public boolean willChangeBounds() {
                        return true;
                    }
                };
        
                a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
                v.startAnimation(a);
            }
        }
    

    In addition, to specify the logic being applied to the animated view, in your case, it could be used a ScrollListener like

    private fun RecyclerView.onViewScroll(title: String): OnScrollListener {
        return object : OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, state: Int) {
                super.onScrollStateChanged(recyclerView, state)
                rootView.findViewById(R.id.views_menu_header).let { layout ->
                    if(state == SCROLL_STATE_DRAGGING) {                     
                        collapse(layout)
                    }
                }
            }
        }
    }
    

    then attaching the listener to your recycler

    findViewById(R.id.views_menu_items).apply { addOnScrollListener(onViewScroll(title)) }
    

    which would collapse R.id.views_menu_header whenever the R.id.views_menu_items starts being dragged.

提交回复
热议问题