1704B第7天ViewPagerViewPager+Fragment+TabLayout新闻头部标题导航(*****)

匿名 (未验证) 提交于 2019-12-02 23:42:01

第7天ViewPagerViewPager+Fragment+TabLayout新闻头部标题导航(*****)

ViewPager

一.效果


二.思路:1个ViewPager+5个Fragment+1个FragmentPagerAdapter+TabLayout

1.容器:ViewPager
2.数据:ArrayList<Fragment ArrayList<String 标题
3.适配器:FragmentViewPager
4.TabLayout:需要导入依赖

重写的方法:
(1)getCount():返回数据源的总数量
(2)getItem():
(3)getPageTitle():返回标题

三.代码

(1)导入依赖:implementation 'com.android.support:design:27.0.0’或者
implementation ‘com.android.support:design:28.0.0’
(2)布局activity_main.xml:ViewPager+TabLayout

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical">     <!--tabLayout依赖-->     <android.support.design.widget.TabLayout         app:tabSelectedTextColor="#E5AF35"         app:tabTextColor="#969696"         app:tabIndicatorColor="#E5AF35"         app:tabIndicatorHeight="5dp"         app:tabMode="scrollable"         android:id="@+id/tb"         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1">     </android.support.design.widget.TabLayout>     <android.support.v4.view.ViewPager         android:id="@+id/vp"         android:layout_weight="8"         android:layout_height="0dp"         android:layout_width="match_parent">      </android.support.v4.view.ViewPager> </LinearLayout> 

(3)Java代码:MainActivity.java

public class MainActivity extends AppCompatActivity {     //视图     private ViewPager vp;     private TabLayout tb;     //数据     private ArrayList<Fragment> fragment_list=new ArrayList<>();     private ArrayList<String> title_list=new ArrayList<>();//标题     //适配器     private MyFragmentPagerAdapter myFragmentPagerAdapter;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         supportRequestWindowFeature(Window.FEATURE_NO_TITLE);         setContentView(R.layout.activity_main);         initview();         initdata();         initadpater();         //TODO tablayout和viewpager联系在一起         tb.setupWithViewPager(vp);     }          private void initadpater() {         myFragmentPagerAdapter=new MyFragmentPagerAdapter(getSupportFragmentManager(),fragment_list,title_list);         vp.setAdapter(myFragmentPagerAdapter);     }      private void initdata() {         fragment_list.add(new Fragment1());         fragment_list.add(new Fragment2());         fragment_list.add(new Fragment3());         fragment_list.add(new Fragment4());         title_list.add("我的");         title_list.add("新闻");         title_list.add("最热");         title_list.add("最强");     }      private void initview() {         vp=findViewById(R.id.vp);         tb=findViewById(R.id.tb);      } }  

(4)适配器代码:MyFragmentPagerAdapter.java

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {     private ArrayList<Fragment> fragment_list;     private ArrayList<String> title_list;      public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragment_list,ArrayList<String> titile_list) {         super(fm);         this.fragment_list=fragment_list;         this.title_list=titile_list;     }     @Override     public Fragment getItem(int i) {         return fragment_list.get(i);     }     @Override     public int getCount() {         return fragment_list.size();     }     //TODO 返回对应的标题     @Nullable     @Override     public CharSequence getPageTitle(int position) {         return title_list.get(position);     } }  

源代码:

文章来源: https://blog.csdn.net/qq_34178710/article/details/91842139
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!