摘要:一簡(jiǎn)介使用起來(lái)就是我們通過(guò)創(chuàng)建給它填充多個(gè),左右滑動(dòng)時(shí),切換不同的。
一、簡(jiǎn)介
Viewpager使用起來(lái)就是我們通過(guò)創(chuàng)建adapter給它填充多個(gè)view,左右滑動(dòng)時(shí),切換不同的view。Google官方是建議我們使用Fragment來(lái)填充ViewPager的,這樣 可以更加方便的生成每個(gè)Page,以及管理每個(gè)Page的生命周期二、引入相關(guān)依賴
implementation "com.android.support:design:28.0.0"三、效果圖 四、代碼實(shí)現(xiàn)
xml
activity中通過(guò)id來(lái)尋找控件
viewPager = findViewById(R.id.viewPager); tabLayout = findViewById(R.id.tabLayout);
創(chuàng)建ViewPager適配器
public class PageAdapter extends FragmentPagerAdapter { ListfragmentList = new ArrayList<>(); List titleList = new ArrayList<>(); public PageAdapter(FragmentManager fm,List fragmentList,List titleList) { super(fm); this.fragmentList = fragmentList; this.titleList = titleList; } @Override public Fragment getItem(int i) { return fragmentList.get(i); } @Override public int getCount() { return fragmentList.size(); } @Nullable @Override public CharSequence getPageTitle(int position) { return titleList.get(position); } }
準(zhǔn)備相關(guān)數(shù)據(jù)(fragment,title)
private ListgetTitleList() { titleList.add("Tab1"); titleList.add("Tab2"); return titleList; } private List getFragmentList() { pageModels.add(new PageModel(R.layout.examplelayout1)); pageModels.add(new PageModel(R.layout.examplelayout2)); for (int i = 0; i < pageModels.size(); i++) { pageFragment = PageFragment.newInstance(pageModels.get(i).exampleLayoutRes); fragmentList.add(pageFragment); } return fragmentList; } class PageModel { @LayoutRes int exampleLayoutRes; public PageModel(@LayoutRes int exampleLayoutRes) { this.exampleLayoutRes = exampleLayoutRes; } }
設(shè)置viewPager適配器
viewPager.setAdapter(new PageAdapter(getSupportFragmentManager(), getFragmentList(), getTitleList()));
設(shè)置tab標(biāo)題
如果不設(shè)置此項(xiàng),tab將無(wú)法顯示
tabLayout.setupWithViewPager(viewPager);五、附上源碼
布局相關(guān):
activity_main
fragment_page
examplelayout1
examplelayout2
功能代碼
MainActivity
public class MainActivity extends AppCompatActivity { ViewPager viewPager; TabLayout tabLayout; ListtitleList = new ArrayList<>(); List fragmentList = new ArrayList<>(); List pageModels = new ArrayList<>(); PageFragment pageFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); doBisness(); } private void doBisness() { viewPager = findViewById(R.id.viewPager); tabLayout = findViewById(R.id.tabLayout); viewPager.setAdapter(new PageAdapter(getSupportFragmentManager(), getFragmentList(), getTitleList())); tabLayout.setupWithViewPager(viewPager); } private List getTitleList() { titleList.add("Tab1"); titleList.add("Tab2"); return titleList; } private List getFragmentList() { pageModels.add(new PageModel(R.layout.examplelayout1)); pageModels.add(new PageModel(R.layout.examplelayout2)); for (int i = 0; i < pageModels.size(); i++) { pageFragment = PageFragment.newInstance(pageModels.get(i).exampleLayoutRes); fragmentList.add(pageFragment); } return fragmentList; } class PageModel { @LayoutRes int exampleLayoutRes; public PageModel(@LayoutRes int exampleLayoutRes) { this.exampleLayoutRes = exampleLayoutRes; } } }
PageFragment
public class PageFragment extends Fragment { @LayoutRes int exampleLayoutRes; /** * Use this factory method to create a new instance of * this fragment using the provided parameters. */ public static PageFragment newInstance(@LayoutRes int exampleLayoutRes) { PageFragment fragment = new PageFragment(); Bundle args = new Bundle(); args.putInt("exampleLayoutRes", exampleLayoutRes); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { exampleLayoutRes = getArguments().getInt("exampleLayoutRes"); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_page, container, false); ViewStub exampleStub = view.findViewById(R.id.exampleViewStub); exampleStub.setLayoutResource(exampleLayoutRes); exampleStub.inflate(); return view; } }
PageAdapter
public class PageAdapter extends FragmentPagerAdapter { List六、項(xiàng)目github地址fragmentList = new ArrayList<>(); List titleList = new ArrayList<>(); public PageAdapter(FragmentManager fm,List fragmentList,List titleList) { super(fm); this.fragmentList = fragmentList; this.titleList = titleList; } @Override public Fragment getItem(int i) { return fragmentList.get(i); } @Override public int getCount() { return fragmentList.size(); } @Nullable @Override public CharSequence getPageTitle(int position) { return titleList.get(position); } }
https://github.com/fr1014/vie...
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/72694.html
摘要:開(kāi)發(fā)過(guò)程中,特別是新開(kāi)的項(xiàng)目,底部狀態(tài)欄的切換使用的頻率非常的高,主要的實(shí)現(xiàn)方式有這里我先介紹前面兩種實(shí)現(xiàn)方式,后面兩種后期再貼出實(shí)現(xiàn)方式。代碼地址底部公眾號(hào)回復(fù)底部狀態(tài)欄切換即可獲取。 Android開(kāi)發(fā)過(guò)程中,特別是新開(kāi)的項(xiàng)目,底部狀態(tài)欄的切換使用的頻率非常的高,主要的實(shí)現(xiàn)方式有: (1)、TabLayout + Fragment (2)、FragmentTabHo...
摘要:初步實(shí)現(xiàn)之前在知乎上看到有人對(duì)微信的設(shè)計(jì)改動(dòng)將使用頻率高的朋友圈消息提醒和公眾號(hào)這三個(gè)功能獨(dú)立出來(lái)放在首頁(yè)。 1、介紹和準(zhǔn)備 我們?cè)谑褂檬謾C(jī)App時(shí)不難會(huì)看到這樣的頁(yè)面上面是一組起導(dǎo)航作用的標(biāo)簽,點(diǎn)擊標(biāo)簽就會(huì)切換到相應(yīng)的頁(yè)面;在不同的頁(yè)面中滑動(dòng)時(shí),標(biāo)簽的樣式(文字大小或者顏色)也會(huì)發(fā)生變化。這樣你任何時(shí)候都能一眼看出自己停留在哪個(gè)頁(yè)面。這個(gè)布局出鏡率實(shí)在太高了,我甚至敢說(shuō)每個(gè)學(xué)Andr...
閱讀 3311·2021-09-30 09:54
閱讀 3808·2021-09-22 15:01
閱讀 3116·2021-08-27 16:19
閱讀 2580·2019-08-29 18:39
閱讀 2168·2019-08-29 14:09
閱讀 638·2019-08-26 10:23
閱讀 1346·2019-08-23 12:01
閱讀 1876·2019-08-22 13:57