成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

利用TabLayout+ViewPager+Fragment實(shí)現(xiàn)首頁(yè)側(cè)滑

chenatu / 1492人閱讀

摘要:一簡(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 {

   List 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);
   }
 }

準(zhǔn)備相關(guān)數(shù)據(jù)(fragment,title)

 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;
       }
   }

設(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;
    List titleList = 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 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);
    }
}
六、項(xiàng)目github地址

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

相關(guān)文章

  • Android實(shí)現(xiàn)底部狀態(tài)欄切換

    摘要:開(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...

    UsherChen 評(píng)論0 收藏0
  • TabLayout的簡(jiǎn)單運(yùn)用和若干問(wèn)題的解決

    摘要:初步實(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...

    PiscesYE 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<