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
- Android使用(TabLayout+ViewPager+fragment)與(FragmentTabHost+ViewPager+Fragment)實現底部狀態列切換AndroidTabLayoutViewpagerFragment
- TabLayout + ViewPager +Fragment 實現頁面的動態重新整理TabLayoutViewpagerFragment
- ToolBar、TabLayout、Fragment+ViewPager的開發實踐TabLayoutFragmentViewpager
- TabLayout+ViewPager+Fragment實現懶載入完全解決方案TabLayoutViewpagerFragment
- 安卓開發:viewpager + fragment 實現滑動切換安卓ViewpagerFragment
- Android tabLayout+viewPager+fragment處理懶載入AndroidTabLayoutViewpagerFragment
- 解決viewpager+fragment+tablayout的動態重新整理ViewpagerFragmentTabLayout
- Tablayout+Viewpager+Fragment組合使用以及懶載入機制TabLayoutViewpagerFragment
- 用CSS實現Tab頁切換效果CSS
- CSS實現頁面切換時的滑動效果CSS
- 手機直播原始碼,使用ViewPager2實現頁面滑動切換原始碼Viewpager
- 一行程式碼實現ViewPager卡片效果行程Viewpager
- ViewPager巢狀fragment簡單使用Viewpager巢狀Fragment
- Android ViewPager2 + Fragment 聯動AndroidViewpagerFragment
- Android ViewPager2 + Fragment + BottomNavigationView 聯動AndroidViewpagerFragmentNavigation
- android使用TabLayout+NestedScrollView 實現詳情介面tab頁 關聯 上下滑動檢視的效果AndroidTabLayoutView
- ViewPager(通過反射修改viewpager切換速度)Viewpager反射
- Vue 全家桶仿原生App切換效果和頁面快取實踐VueAPP快取
- 修改 support 包 TabLayout,實現新浪微博/即刻 APP 蚯蚓導航效果TabLayoutAPP
- 028、Vue3+TypeScript基礎,使用路由功能實現頁面切換效果VueTypeScript路由
- Android 在 ViewPager 中使用 Fragment 的懶載入AndroidViewpagerFragment
- 小程式標籤頁切換效果
- WPF手動實現切換頁面
- 【Android】 banner+tab吸頂+viewpager切換+重新整理載入之實現AndroidViewpager
- vue2.0實現底部導航切換效果Vue
- 簡單實現一個全面屏切換效果
- Vue實現內部元件輪播切換效果Vue元件
- html兩種方法來實現tab切換效果HTML
- vue+Element-ui實現分頁效果VueUI
- Web 頁面如何實現動畫效果Web動畫
- 教你如何使用MvcPager實現分頁效果MVC
- 自定義Navigator切換fragmentFragment
- 【Android Fragment】解決ViewPager巢狀時Fragment的mUserVisibleHint屬性不同步的問題AndroidFragmentViewpager巢狀
- 網頁佈局------輪播圖效果實現網頁
- iOS 類知乎”分頁”效果的實現?iOS