2018-07-24

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

activity_main布局:

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

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


    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:id="@+id/app_bar"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:fitsSystemWindows="true">

    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">

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="@mipmap/happyxueche"/>

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

app:layout_collapseMode="pin"

    app:title="Toolbar"/>

android:id="@+id/tabLayout"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

app:tabIndicatorColor="#FFFFFFFF"

app:tabSelectedTextColor="#FF888888"

app:tabTextColor="#FFFFFFFF"/>

    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:id="@+id/viewPager"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"/>

mainActivity中:

package com.example.lijunlu.image724;

import android.os.Bundle;

import android.support.design.widget.AppBarLayout;

import android.support.design.widget.CollapsingToolbarLayout;

import android.support.design.widget.TabLayout;

import android.support.v4.view.ViewPager;

import android.support.v4.widget.NestedScrollView;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import com.example.lijunlu.image724.adapter.ResumeAdapter;

public class MainActivityextends AppCompatActivity {

private Toolbartoolbar;

private CollapsingToolbarLayouttoolbar_layout;

private TabLayouttabLayout;

private AppBarLayoutapp_bar;

private NestedScrollViewnestedScrollView;

private ViewPagerviewPager;

@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) {

}

});

}

}

适配器:

package com.example.lijunlu.image724.adapter;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentPagerAdapter;

import com.example.lijunlu.image724.AFragment;

import com.example.lijunlu.image724.BFragment;

import com.example.lijunlu.image724.CFragment;

import com.example.lijunlu.image724.DFragment;

public class ResumeAdapterextends 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;

}

}

你可能感兴趣的:(2018-07-24)