滑动吸顶+ViewPager+Tablayout+Fragment

先上效果图

在这里插入图片描述

首先我们需要导入依赖:这个是为了引用里面的 Tablayout

 implementation 'com.google.android.material:material:1.1.0'

然后是MainActivity.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/coll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#2196F3"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="exitUntilCollapsed|scroll">

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

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/gui" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="异地鸡"
                    android:textColor="#000"
                    android:textSize="25sp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="异地鸡"
                    android:textColor="#000"
                    android:textSize="25sp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="异地鸡"
                    android:textColor="#000"
                    android:textSize="25sp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="异地鸡"
                    android:textColor="#000"
                    android:textSize="25sp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="异地鸡"
                    android:textColor="#000"
                    android:textSize="25sp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="异地鸡"
                    android:textColor="#000"
                    android:textSize="25sp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="异地鸡"
                    android:textColor="#000"
                    android:textSize="25sp" />
            </LinearLayout>

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

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:background="#FFF"
            app:tabIndicatorColor="@android:color/holo_blue_dark"
            app:tabSelectedTextColor="@color/colorAccent"
            app:tabTextColor="#000" />


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

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>

其次是MainActivity.java的代码:主要是实现tablayout+viewpager+fragment的关联

public class MainActivity extends AppCompatActivity {
    
    

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> list = new ArrayList<>();
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private List<String> title = new ArrayList<>();
    CollapsingToolbarLayout collapsingToolbarLayout;

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

        tabLayout = findViewById(R.id.tablayout);
        viewPager = findViewById(R.id.viewpager);
        collapsingToolbarLayout = findViewById(R.id.coll);


        title.add("异地鸡");
        title.add("一滴鸡");

        oneFragment = new OneFragment();
        twoFragment = new TwoFragment();
        list.add(oneFragment);
        list.add(twoFragment);

        collapsingToolbarLayout.setTitle("返回");

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
    
    
            @NonNull
            @Override
            public Fragment getItem(int position) {
    
    
                return list.get(position);
            }

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

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

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
    
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
    

            }

            @Override
            public void onPageSelected(int position) {
    
    

            }

            @Override
            public void onPageScrollStateChanged(int state) {
    
    

            }
        });

        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }
}

最主要的就是这两个类的代码。两个Fragment里的区别是:第一个页面使用的Recycleview列表;

第二个页面使用的是:

<androidx.core.widget.NestedScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    
</androidx.core.widget.NestedScrollView>

都可以实现滑动效果,没有太多的逻辑,十分简单
希望能够帮助各位读者

猜你喜欢

转载自blog.csdn.net/m0_46366678/article/details/121210690