协调者布局 CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout(写法很粗糙,但是很全,实现功能为主)

协调者布局 CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout(写法很粗糙,但是很全,实现功能为主)

main_activity.xml走起:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="15dp"
            android:background="#ff0000"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <ImageView
                app:layout_collapseMode="pin"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_centerInParent="true"
                android:src="@drawable/bbb"></ImageView>

            <ScrollView
                app:layout_collapseMode="pin"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:text="sdnfjsdnjfnsdjnf"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"></TextView>
                    <TextView
                        android:text="sdnfjsdnjfnsdjnf"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"></TextView>
                    <TextView
                        android:text="sdnfjsdnjfnsdjnf"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"></TextView>
                    <TextView
                        android:text="sdnfjsdnjfnsdjnf"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"></TextView>
                    <TextView
                        android:text="sdnfjsdnjfnsdjnf"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"></TextView>
                    <TextView
                        android:text="sdnfjsdnjfnsdjnf"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"></TextView>
                    <TextView
                        android:text="sdnfjsdnjfnsdjnf"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"></TextView>
                    <TextView
                        android:text="sdnfjsdnjfnsdjnf"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"></TextView>
                    <TextView
                        android:text="sdnfjsdnjfnsdjnf"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"></TextView>
                </LinearLayout>

            </ScrollView>

        </com.google.android.material.appbar.CollapsingToolbarLayout>


    </com.google.android.material.appbar.AppBarLayout>

    <androidx.appcompat.widget.LinearLayoutCompat
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.google.android.material.tabs.TabLayout
            android:background="#0000ff"
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <androidx.viewpager.widget.ViewPager
            android:background="#00ff00"
            android:id="@+id/view_pager2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </androidx.appcompat.widget.LinearLayoutCompat>

<!--    <androidx.recyclerview.widget.RecyclerView-->
<!--        android:id="@+id/recyclerView"-->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="match_parent"-->
<!--         />-->
</androidx.coordinatorlayout.widget.CoordinatorLayout>

MainActivity:

package com.example.myapplication;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ViewPager viewPager = findViewById(R.id.view_pager2);
        TabLayout tab = findViewById(R.id.tab_layout);
        BlankFragment blankFragment = new BlankFragment();
        BlankFragment2 blankFragment2 = new BlankFragment2();
        List<Fragment> list = new ArrayList<>();
        List<String> stringList = new ArrayList<>();
        list.add(blankFragment);
        list.add(blankFragment2);
        stringList.add("技能鉴定");
        stringList.add("是大家都");


        ViewPageAdapter viewPageAdapter = new ViewPageAdapter(getSupportFragmentManager(), list, stringList);
        viewPager.setAdapter(viewPageAdapter);

        tab.setupWithViewPager(viewPager);


    }
}

一些副用的layout:(widget.xml 用于RecyclerViewAdapter中填充TextView数据所用)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/texts"
        android:layout_width="wrap_content"
        android:layout_height="100dp"></TextView>

</LinearLayout>

BlankFragment:(只有一个RecyclerView)注:另外一个BlankFragment2 是默认创建的,其中毫无操作,大家自行创建就行

package com.example.myapplication;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;


public class BlankFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_blank, container, false);
        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerview);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2, RecyclerView.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return true;
            }
        };

        recyclerView.setLayoutManager(gridLayoutManager);

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add("为所欲为");
        }

        TabScrollDoubleAdapter tabScrollDoubleAdapter = new TabScrollDoubleAdapter(getContext(), list);
        recyclerView.setAdapter(tabScrollDoubleAdapter);

        return inflate;
    }
}

fragment_blank:(Fragment布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".BlankFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>

</LinearLayout>

ViewPageAdapter:(Viewpager适配器,另:ViewPager也可与动态创建的Linearlayout联用)

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import java.util.List;


public class ViewPageAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> mViewList;
    private List<String> mStringList;

    public ViewPageAdapter(@NonNull FragmentManager fm, List<Fragment> list, List<String> lists) {
        super(fm);
        this.mViewList = list;
        this.mStringList = lists;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return mViewList.get(position);
    }


    @Override
    public int getCount() {
        return mStringList.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mStringList.get(position);
    }
}

TabScrollDoubleAdapter(RecyclerView适配器)

package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;



import java.util.List;

public class TabScrollDoubleAdapter extends RecyclerView.Adapter<TabScrollDoubleAdapter.MyHolder> {

    private Context context;
    private List<String> data;


    public TabScrollDoubleAdapter(Context context, List<String> data) {
        this.context = context;
        this.data = data;

    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.widget, null);

        return new MyHolder(inflate);
    }


    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.textView.setText(data.get(position));


    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    class MyHolder extends RecyclerView.ViewHolder{
        TextView textView;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.texts);
        }
    }
}


  • 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 的滚动事件。 关于CoordinatorLayoutAppBarLayoutCollapsingToolbarLayout 和 RecyclerView 的结合使用,可以参考以下代码: ```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、付费专栏及课程。

余额充值