RecyclerView与CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout 解决上滑到toolbar卡顿

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@id/contentView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:theme="@style/Theme.Dialog"
        android:fitsSystemWindows="true">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="36dp"
            app:expandedTitleMarginEnd="52dp"
            android:fitsSystemWindows="true"
            >
            <ImageView
                android:fitsSystemWindows="true"
                android:id="@+id/iv_circle_cover"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:transitionName="mu"
                />
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_collapseMode="parallax"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                android:transitionName="mu"
                android:background="@drawable/circle_cover"
                />
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="@dimen/space_24">
                <LinearLayout
                    android:layout_alignParentBottom="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                <TextView
                    android:id="@+id/tv_circle_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textSize="17sp"
                    android:textColor="@color/white"
                    android:layout_marginLeft="@dimen/space_12"
                    />
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        >
                        <TextView
                            android:id="@+id/tv_circle_num"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            android:textSize="12sp"
                            android:textColor="@color/white"
                            android:layout_marginLeft="@dimen/space_12"
                            />
                        <TextView
                            android:id="@+id/tv_circle_post_num"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            android:textSize="12sp"
                            android:textColor="@color/white"
                            android:layout_marginLeft="@dimen/space_10"
                            android:layout_marginBottom="@dimen/space_12"
                            />

                    </LinearLayout>
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/ll_join"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="@dimen/space_12"
                    android:layout_marginRight="@dimen/space_12"
                    android:background="@drawable/text_circle_empty_white"
                    android:visibility="gone">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="  加入  "
                        android:padding="@dimen/space_6"
                        android:textSize="@dimen/space_12"
                        android:textColor="@color/white"
                        />

                </LinearLayout>
            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/tool_bar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" >
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <com.iyumiao.tongxue.ui.base.TTFTextView
                        android:id="@+id/tv_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/bigtitle"
                        android:textColor="@color/gray_title"
                        android:text=""
                        android:layout_centerInParent="true"
                        />

                    <ImageButton
                        android:id="@+id/ibNewsBack"
                        android:layout_marginTop="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:paddingLeft="@dimen/space_12"
                        android:paddingRight="@dimen/space_26"
                        android:layout_centerVertical="true"
                        android:contentDescription="@null"
                        android:src="@drawable/ic_nav_back"
                        android:background="@null"

                        />

                    <ImageView
                        android:layout_centerVertical="true"
                        android:id="@+id/iv_title_more"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:src="@drawable/growth_detail_more"
                        android:paddingRight="@dimen/space_12"
                        android:paddingLeft="@dimen/space_20"
                        android:layout_alignParentRight="true"
                        />

                </RelativeLayout>

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>


    <FrameLayout
        android:id="@+id/fl_all"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bg"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <include layout="@layout/loading_view" />
        <include layout="@layout/error_view" />
        <android.support.v7.widget.RecyclerView
            android:id="@id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cacheColorHint="@null"
            android:scrollbars="vertical"/>
        <RelativeLayout
            android:id="@+id/emptyView"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:gravity="center_horizontal"
            android:visibility="invisible"
            android:clickable="true"
            >
            <TextView
                android:id="@+id/tvEmptyTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"
                />
        </RelativeLayout>

</FrameLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <ImageView
                android:id="@+id/iv_add_post"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_add_post"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="32dp"
                android:layout_marginRight="@dimen/space_12"
                />
        </RelativeLayout>
        <!--</LinearLayout>-->
    <!--</android.support.v4.widget.NestedScrollView>-->
</android.support.design.widget.CoordinatorLayout>

</android.support.v4.widget.SwipeRefreshLayout>

        app_bar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset >= 0) {
                    contentView.setEnabled(true);
                } else {
                    contentView.setEnabled(false);
                }
            }
        });
        recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                touchState = event.getAction();
                return false;
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if(!recyclerView.canScrollVertically(-1)){
                    if(touchState == 1) {
                        app_bar.setExpanded(true, true);
                    }
                }
                LogUtils.e("gtt","dy" + dy+"");
                super.onScrolled(recyclerView, dx, dy);
                count++;
                if(count%10 == 0) {
                    if (dy < 0) {
                        if (canSrcollR) {
                            LogUtils.e("gtts","上上");
                            canSrcollR = false;
                            AnimL();
                        }
                    } else {
                        if (!canSrcollR) {
                            LogUtils.e("gtts", "下下");
                            canSrcollR = true;
                            AnimR();
                        }
                    }
                }
                LogUtils.e("gtt","dx:"+dx+"dy:"+dy);
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                LogUtils.e("gtt","linearLayoutManager.findFirstCompletelyVisibleItemPosition()"+linearLayoutManager.findFirstCompletelyVisibleItemPosition());
                LogUtils.e("gtt","newState"+ newState);
                if(newState == RecyclerView.SCROLL_STATE_IDLE){
                    if(linearLayoutManager.findFirstCompletelyVisibleItemPosition()==0){
                        app_bar.setExpanded(true, true);
                    }
                }
                super.onScrollStateChanged(recyclerView, newState);
            }
        });
        ibNewsBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        ll_join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(User.isLogined(getActivity())){
                    if (null != circle) {
                        presenter.joinCircle(circle.getId());
                    }
                }else {
                    ToastUtils.show(getActivity(), "请登录");
                    NavUtils.toActivity(getActivity(), LoginActivity.class);
                }
            }
        });
        iv_add_post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(User.isLogined(getActivity())) {
                    Intent intent = new Intent(getActivity(), AddPostActivity.class);
                    intent.putExtra("circleId", circle.getId());
                    intent.putExtra("circleName", circle.getName());
                    NavUtils.toActivity(getActivity(), intent);
                }else {
                    NavUtils.toActivity(getActivity(),LoginActivity.class);
                }
            }
        });
//        contentView.setNestedScrollingEnabled(false);
        app_bar.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                LogUtils.e("STATE", state.name());
                if( state == State.EXPANDED ) {
                    tv_title.setTextColor(Color.WHITE);
                    ibNewsBack.setImageResource(R.drawable.ic_nav_back_white);
                    iv_title_more.setImageResource(R.drawable.ic_more_circle);
                    //展开状态
                    LogUtils.e("gtt","111");
                }else if(state == State.COLLAPSED){
                    tv_title.setTextColor(0xff333333);
                    ibNewsBack.setImageResource(R.drawable.ic_nav_back);
                    iv_title_more.setImageResource(R.drawable.growth_detail_more);
                    //折叠状态
                    LogUtils.e("gtt","222");
                }else {
                    tv_title.setTextColor(Color.WHITE);
                    ibNewsBack.setImageResource(R.drawable.ic_nav_back_white);
                    iv_title_more.setImageResource(R.drawable.ic_more_circle);
                    LogUtils.e("gtt","333");
                    //中间状态

                }
            }
        });
    }

    @Override
    protected CirclePostAdapter createLoadMoreAdapter() {
        return new CirclePostAdapter(new ArrayList<Post>());
    }

    @NonNull
    @Override
    public CirclePostDetailPresenter createPresenter() {
        CirclePostDetailPresenter postDetailPresenter = new CirclePostDetailPresenterImpl(getActivity());
        postDetailPresenter.setCircleId(circleId);
        return postDetailPresenter;
    }

    @Override
    public void loadData(boolean pullToRefresh) {
        presenter.fetchPost(pullToRefresh);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_circledetails;
    }

    @Override
    protected void onNextPage(int currentPage) {
        presenter.onLoadMore(currentPage);
    }

    @Override
    public void setData(List<Post> data) {
        super.setData(data);
        if(data != null && data.size() > 0){
            adapter.setDataList(data);
            adapter.notifyDataSetChanged();
        }else {
            mEndlessRecyclerOnScrollListener.setReachEnd(false);
            adapter.showFooter();
        }
    }

    private int posotion;
    private Post post;
    private PickerDialog mDialog;
    private View mPickerView;
    private PickerDialog mReportDialog;
    private View mReportView;
    Button item_report;
    @Override
    protected void onBindItemView(final CirclePostAdapter.MyViewHolder holder) {
        super.onBindItemView(holder);
        holder.iv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (User.isLogined(getActivity())) {
                    posotion = holder.getPosition();
                    post = adapter.getDataList().get(posotion);
                    if (mDialog == null) mDialog = new PickerDialog(getActivity());
                    if (mPickerView == null) {
                        mPickerView = LayoutInflater.from(getActivity()).inflate(R.layout.pop_hotpost, null);
                        item_report = (Button) mPickerView.findViewById(R.id.item_report);
                        Button item_cancel = (Button) mPickerView.findViewById(R.id.item_cancel);
                        item_report.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (User.isLogined(getActivity())) {
                                    if (SPUtil.getUser(getActivity()).getId() == post.getUserId()) {
                                        presenter.deletePost(post.getId());
                                    } else {
                                        mDialog.dismiss();
                                        mReportDialog = new PickerDialog(getActivity());
                                        mReportView = View.inflate(getActivity(), R.layout.pop_report, null);
                                        mReportView.findViewById(R.id.type_1).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                presenter.postInform(1, 1, post.getId());
                                            }
                                        });
                                        mReportView.findViewById(R.id.type_2).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                presenter.postInform(2, 1, post.getId());
                                            }
                                        });
                                        mReportView.findViewById(R.id.type_3).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                presenter.postInform(3, 1, post.getId());
                                            }
                                        });
                                        mReportDialog.showBottom(mReportView);
                                    }
                                }

                            }
                        });
                        item_cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mDialog.dismiss();
                            }
                        });
                    }
                    mDialog.showBottom(mPickerView);
                    LogUtils.e("gtt", post.getUserId() + ":" + post.getContent());
                    if (User.isLogined(getActivity())) {
                        if (SPUtil.getUser(getActivity()).getId() == post.getUserId()) {
                            item_report.setText("删除");
                        } else {
                            item_report.setText("举报");
                        }
                    } else {
                        item_report.setText("举报");
                    }
                }else {
                    NavUtils.toActivity(getActivity(),LoginActivity.class);
                }
            }
        });
        holder.video_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                posotion = holder.getPosition();
                post = adapter.getDataList().get(posotion);
                Intent mpdIntent = new Intent(getActivity(), PlayerActivity.class)
                        .setData(Uri.parse(post.getPostsMedias().get(0).getMediaUrl()));
                mpdIntent.putExtra("URIPIC", post.getPostsMedias().get(0).getScreenUrl())   ;
                getActivity().startActivity(mpdIntent);
            }
        });
        holder.ll_parise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(User.isLogined(getActivity())) {
                    posotion = holder.getPosition();
                    post = adapter.getDataList().get(posotion);
                    if (post.getIsPraise() == 0) {
                        presenter.praise(post.getId());
                    } else {
                        presenter.canclePrasise(post.getId());
                    }
                }else {
                    NavUtils.toActivity(getActivity(), LoginActivity.class);
                }
            }
        });
        holder.ng_hotpost_pic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Post ngPost;
                ngPost = adapter.getDataList().get(holder.getPosition());
                ArrayList<String> imageList = new ArrayList<>();
                for(int i =0;i<ngPost.getPostsMedias().size();i++){
                    imageList.add(ngPost.getPostsMedias().get(i).getMediaUrl());
                }

                Intent intent = new Intent(holder.getItemView().getContext(), GrowthMainPicActivity.class);
                intent.putStringArrayListExtra("URIs", imageList);
                intent.putExtra("position", position);
                holder.getItemView().getContext().startActivity(intent);
            }
        });
    }

    @Override
    public void reportSucc() {
        ToastUtils.show(getActivity(),"举报成功");
        mReportDialog.dismiss();
    }

    @Override
    public void deletePost() {
        adapter.getDataList().remove(post);
        adapter.notifyItemRemoved(posotion);
        mDialog.dismiss();
    }
    private Circle circle;
    @Override
    public void fetchCicle(final Circle circle) {
        this.circle = circle;
        tv_circle_name.setText(circle.getName());
        tv_circle_num.setText(circle.getMemberCount() + " 成员");
        tv_circle_post_num.setText(circle.getPostsCount() + " 话题");
        ImageLoader.getInstance().displayImage(circle.getCoverUrl(),iv_circle_cover, ImageDisplayOptUtils.getGrowthImageDisplayOpt());
        collapsing_toolbar.setTitle(circle.getName());
        collapsing_toolbar.setExpandedTitleColor(Color.WHITE);
        collapsing_toolbar.setExpandedTitleGravity(Gravity.LEFT);
        collapsing_toolbar.setCollapsedTitleGravity(Gravity.CENTER);
        collapsing_toolbar.setCollapsedTitleTextColor(Color.WHITE);
        AppCompatActivity activity = (AppCompatActivity)getActivity();
        activity.setSupportActionBar(tool_bar);
        ActionBar actionBar = activity.getSupportActionBar();
        tv_title.setText(circle.getName());
        if(circle.getIsJoin() == 1){
            ll_join.setVisibility(View.GONE);
        }else {
            ll_join.setVisibility(View.VISIBLE);
        }
        iv_title_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),CircleDetailsActivity.class);
                intent.putExtra("Circle",circle.getId());
                NavUtils.toActivity(getActivity(),intent);
            }
        });
    }

    @Override
    protected void onItemClick(View v) {
        Intent intent = new Intent(getActivity(),NewPostDetailsActivity.class);
        intent.putExtra("postId",adapter.getDataList().get(recyclerView.getChildPosition(v)).getId()+"");
        intent.putExtra("POST",adapter.getDataList().get(recyclerView.getChildPosition(v)));
        intent.putExtra("Tag","CirclePostDetailsFragment");
        posotion = recyclerView.getChildPosition(v);
        post = adapter.getDataList().get(recyclerView.getChildPosition(v));
        NavUtils.toActivity(getActivity(),intent);

    }

    @Override
    public void joinCircleSucc() {
        ToastUtils.show(getActivity(),"成功加入圈子");
        ll_join.setVisibility(View.GONE);
    }

    @Override
    public void praiseSucc() {
        adapter.getDataList().get(posotion).setIsPraise(1);
        adapter.getDataList().get(posotion).setPraiseCount(adapter.getDataList().get(posotion).getPraiseCount()+1);
        adapter.notifyItemChanged(posotion);

    }

    @Override
    public void canclePraiseSucc() {
        adapter.getDataList().get(posotion).setIsPraise(0);
        adapter.getDataList().get(posotion).setPraiseCount(adapter.getDataList().get(posotion).getPraiseCount()-1);
        adapter.notifyItemChanged(posotion);
    }

    public void onEvent(CircleModelImpl.PublishPostEvent event){
        if(getView()!= null){
            if(event.getStatus() == NetworkUtils.RES_OK){
                Post post = event.getPost();
                post.setContent(emojiParser.demojizedText(post.getContent()));
                adapter.getDataList().add(0,post);
                adapter.notifyDataSetChanged();
            }
        }
    }


    public static class PostUpdate{
        private long commentNum;
        private long praiseNum;

        public long getCommentNum() {
            return commentNum;
        }

        public void setCommentNum(long commentNum) {
            this.commentNum = commentNum;
        }

        public long getPraiseNum() {
            return praiseNum;
        }

        public void setPraiseNum(long praiseNum) {
            this.praiseNum = praiseNum;
        }
    }
    public void onEvent(PostUpdate event){
        LogUtils.e("gtt","gtt22"+event.getPraiseNum());
        adapter.getDataList().get(posotion).setPraiseCount(event.getPraiseNum());
        adapter.getDataList().get(posotion).setCommentCount(adapter.getDataList().get(posotion).getCommentCount() + event.getCommentNum());
        adapter.notifyItemChanged(posotion);
    }
    private Animation taR, taL, alT, alO, alAddress;
    AnimationSet setL, setR;
    public void AnimR() {
        if (null == alO) {
            alO = new AlphaAnimation(0.1f, 1.0f); //0表示完全透明。1表示不透明
            alO.setDuration(350);
            alO.setFillAfter(true);
            setR = new AnimationSet(false);
            setR.addAnimation(alO);
            setR.setFillAfter(true);
        }
        iv_add_post.startAnimation(alO); //iv表示动画显示显示在哪个地方 通过findviewbyid拿到
        alO.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                canSrcollR = true;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    private boolean canSrcollR = true;
    public void AnimL() {
        if (null == taL) {
            taL = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.9f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
            taL.setDuration(200);
            taL.setFillAfter(true);
            taL.setDuration(350);

            alT = new AlphaAnimation(1.0f, 0); //0表示完全透明。1表示不透明
            alT.setDuration(350);
            alT.setFillAfter(true);
            setL = new AnimationSet(false);
//            setL.addAnimation(taL);
            setL.setStartOffset(450);
            setL.addAnimation(alT);
            setL.setFillAfter(true);
        }
        iv_add_post.startAnimation(alT);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道你的问题了。针对你的问题,可以通过如下代码实现AppBarLayout的滚动监听: ```java AppBarLayout appBarLayout = findViewById(R.id.appBarLayout); appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { // 在这里处理滚动事件 } }); ``` 其中,`addOnOffsetChangedListener` 方法可以添加滚动监听器,该监听器中的 `onOffsetChanged` 方法中可以处理 AppBarLayout 的滚动事件。 关于CoordinatorLayoutAppBarLayoutCollapsingToolbarLayoutRecyclerView 的结合使用,可以参考以下代码: ```xml <androidx.coordinatorlayout.widget.CoordinatorLayout android:id="@+id/coordinatorLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true"> <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/collapsingToolbarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@drawable/image" app:layout_collapseMode="parallax" /> <com.google.android.material.toolbar.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" /> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 其中,`CoordinatorLayout` 是一个可以协调多个子视图之间交互的布局容器。`AppBarLayout` 是一个垂直的 LinearLayout,可以包含一个或多个子视图,用于实现应用程序栏。`CollapsingToolbarLayout` 是一个具有可折叠效果的 Toolbar 布局。`RecyclerView` 是一个可以滚动的列表视图。 以上代码实现了一个具有可折叠效果的 Toolbar 和一个可以滚动的列表视图。在 `CollapsingToolbarLayout` 中的 `ImageView` 和 `Toolbar` 都有一个 `layout_collapseMode` 属性,用于指定它们的行为。`ImageView` 的 `layout_collapseMode` 属性设置为 `parallax`,表示在滚动时具有视差效果;`Toolbar` 的 `layout_collapseMode` 属性设置为 `pin`,表示在折叠时固定在顶部。 `RecyclerView` 的 `layout_behavior` 属性设置为 `appbar_scrolling_view_behavior`,表示它是一个可以与 `AppBarLayout` 协同工作的滚动视图。这样,`AppBarLayout` 就可以根据 `RecyclerView` 的滚动情况来调整自身的显示状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值