ViewPager、Fragment和TabLayout實現切頁效果
ViewPager、Fragment和TabLayout實現切頁效果
不會上傳動圖,心裡哭唧唧。但是是完整程式碼!新建一個專案,完全拷貝貼上就可以用!
1.佈局檔案
(1)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:tabIndicatorColor="@color/colorAccent"
app:tabIndicatorHeight="2dp"
app:tabBackground="@android:color/white"
app:tabTextColor="@android:color/black"
app:tabSelectedTextColor="@android:color/holo_orange_dark">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</android.support.v4.view.ViewPager>
</RelativeLayout>
(2)新建fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment1"
android:textSize="20sp"/>
</LinearLayout>
2.新建MyPagerAdapter
public class MyPagerAdapter extends FragmentPagerAdapter {
private Context context;
private List<Fragment> fragments;
private List<String> strings;
public MyPagerAdapter(List<Fragment> fragments, List<String> strings, FragmentManager fragmentManager, Context context){
super(fragmentManager);
this.strings = strings;
this.context = context;
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments != null ? fragments.size() : 0;
}
@Override
public CharSequence getPageTitle(int position) {
return strings.get(position);
}
}
3.新建4個Fragment,這裡的個數由自己需要所定。這裡我只建了Fragment1,其他幾個只需把Fragment1改成Fragment2、Fragment3……
public class Fragment1 extends Fragment {
private View rootView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = (View) inflater.inflate(R.layout.fragment1, container, false);
return rootView;
}
}
4.MainActivity.java
public class MainActivity extends BaseActivity {
ViewPager viewPager;
TabLayout tabLayout;
private List<String> strings = new ArrayList<String>();;
private List<Fragment> fragments = new ArrayList<Fragment>();;
private MyPagerAdapter myPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=findViewById(R.id.view_pager);
tabLayout=findViewById(R.id.tab_layout);
initView();
}
private void initView(){
viewPager.setAdapter(new MyPagerAdapter(fragments,strings,
getSupportFragmentManager(),this));
tabLayout.setupWithViewPager(viewPager);
Fragment1 fragment1 = new Fragment1();
fragments.add(fragment1);
strings.add("頁面1");
Fragment2 fragment2 = new Fragment2();
fragments.add(fragment2);
strings.add("頁面2");
Fragment3 fragment3 = new Fragment3();
fragments.add(fragment3);
strings.add("頁面3");
Fragment4 fragment4 = new Fragment4();
fragments.add(fragment4);
strings.add("頁面4");
myPagerAdapter=new MyPagerAdapter(fragments,strings,getSupportFragmentManager(),MainActivity.this);
viewPager.setAdapter(myPagerAdapter);
}
}
注:在build.gradle裡配置
implementation 'com.android.support:design:28.0.0'
5.新建menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:context="com.strivestay.tablayoutdemo.MainActivity">
<item android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="Settings"
app:showAsAction="never"/>
</menu>
6.dimens.xml
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="appbar_padding_top">8dp</dimen>
</resources>
相關文章
- TabLayout+ViewPager+Fragment實現切頁展示TabLayoutViewpagerFragment
- 利用ViewPager和Fragment實現頁卡切換ViewpagerFragment
- 使用ViewPager和TabLayout來實現滑動切換效果ViewpagerTabLayout
- TabLayout + ViewPager +Fragment 實現頁面的動態重新整理TabLayoutViewpagerFragment
- Android使用(TabLayout+ViewPager+fragment)與(FragmentTabHost+ViewPager+Fragment)實現底部狀態列切換AndroidTabLayoutViewpagerFragment
- TabLayout+ViewPager+fragment實現懶載入TabLayoutViewpagerFragment
- TabLayout+ViewPager+Fragment懶載入實現TabLayoutViewpagerFragment
- TabLayout-ViewPager填充Fragment和懶載入TabLayoutViewpagerFragment
- tablayout+viewpager+fragment懶載入TabLayoutViewpagerFragment
- TabLayout+ViewPager+Fragment實現懶載入完全解決方案TabLayoutViewpagerFragment
- ToolBar、TabLayout、Fragment+ViewPager的開發實踐TabLayoutFragmentViewpager
- 安卓開發:viewpager + fragment 實現滑動切換安卓ViewpagerFragment
- Tablayout+ViewPager動態新增fragment懶載入TabLayoutViewpagerFragment
- viewpager實現畫廊(一屏多個Fragment)效果ViewpagerFragment
- Android tabLayout+viewPager+fragment處理懶載入AndroidTabLayoutViewpagerFragment
- ViewPager+TabLayout+Fragment懶載入機制完全解析ViewpagerTabLayoutFragment
- 解決viewpager+fragment+tablayout的動態重新整理ViewpagerFragmentTabLayout
- Tablayout+Viewpager+Fragment組合使用以及懶載入機制TabLayoutViewpagerFragment
- 用CSS實現Tab頁切換效果CSS
- TabLayout+ViewPager 懶載入TabLayoutViewpager
- 三行程式碼實現TabLayout+ViewPager的Tab滑動效果,從封裝到開源行程TabLayoutViewpager封裝
- CSS實現頁面切換時的滑動效果CSS
- 在Fragment裡使用viewpagerFragmentViewpager
- 敲酷炫的 ViewPager 切換效果和彈性指示器。Viewpager
- Android開發丶使用懶載入修復tablayout+viewpager+fragment的預載入AndroidTabLayoutViewpagerFragment
- 手機直播原始碼,使用ViewPager2實現頁面滑動切換原始碼Viewpager
- Android開發之ViewPager+Fragment+FragmentTabHost實現底部選單AndroidViewpagerFragment
- 一行程式碼實現ViewPager卡片效果行程Viewpager
- 點選按鈕實現切換頁面背景顏色效果
- Android Fragment實現按鈕間的切換AndroidFragment
- 安卓仿微信Tab頁用Fragment實現安卓Fragment
- ViewPager巢狀fragment簡單使用Viewpager巢狀Fragment
- Android中使用RecyclerView + SnapHelper實現類似ViewPager效果AndroidViewpager
- 修改 support 包 TabLayout,實現新浪微博/即刻 APP 蚯蚓導航效果TabLayoutAPP
- Android ViewPager2 + Fragment 聯動AndroidViewpagerFragment
- ViewPager+Fragment LazyLoad 最優解ViewpagerFragment
- Android開發之ViewPager+FragmentAndroidViewpagerFragment
- ViewPager(通過反射修改viewpager切換速度)Viewpager反射