Android有关tablayout向上滑动停留页面顶部(有待完善)

xiaoxiao2021-03-01  32

activity_main布局:

注:在使用AppBarLayout控件时需要导design包

implementation'com.android.support:design:27.+'

<?xml version="1.0" encoding="utf-8"?> <android.support.design.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" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout" android:layout_width="match_parent" android:layout_height="250dp" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/happyxueche" /> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:title="Toolbar"> </android.support.v7.widget.Toolbar> </android.support.design.widget.CollapsingToolbarLayout> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:tabIndicatorColor="#FFFFFFFF" app:tabSelectedTextColor="#FF888888" app:tabTextColor="#FFFFFFFF" /> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:id="@+id/nestedScrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout>

MainActivity中:

public class MainActivity extends AppCompatActivity { private Toolbar toolbar; private CollapsingToolbarLayout toolbar_layout; private TabLayout tabLayout; private AppBarLayout app_bar; private NestedScrollView nestedScrollView; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); //设置返回主页的按钮 getSupportActionBar().setDisplayHomeAsUpEnabled(true); toolbar_layout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout); tabLayout = (TabLayout) findViewById(R.id.tabLayout); app_bar = (AppBarLayout) findViewById(R.id.app_bar); nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView); viewPager = findViewById(R.id.viewPager); nestedScrollView.setFillViewport(true); ResumeAdapter rusumeAdapter = new ResumeAdapter(getSupportFragmentManager()); viewPager.setAdapter(rusumeAdapter); tabLayout.setupWithViewPager(viewPager); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { nestedScrollView.scrollTo(0,0); } @Override public void onPageScrollStateChanged(int state) { } }); } }

适配器:

public class ResumeAdapter extends FragmentPagerAdapter{ public ResumeAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { Fragment fragment = null; if (0 == position) { fragment = new AFragment(); } else if (1 == position) { fragment = new BFragment(); } else if (2 == position) { fragment = new CFragment(); } else if (3 == position) { fragment = new DFragment(); } return fragment; } @Override public int getCount() { return 4; } @Nullable @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "头条"; case 1: return "热点"; case 2: return "娱乐"; case 3: return "体育"; } return null; } }
转载请注明原文地址: https://www.6miu.com/read-4150008.html

最新回复(0)