Android 选项卡页面实现
来源:互联网 发布:数据管家 编辑:程序博客网 时间:2024/06/14 21:50
首先要添加项目的库依赖:
compile 'com.android.support:design:26.0.0-alpha1'
一、MainActivity页面代码:
package com.tabfragment;import android.support.design.widget.TabLayout;import android.support.v4.app.Fragment;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { private TabLayout tabLayout; private ViewPager vpContent; private List<Fragment> fragmentList = new ArrayList<>(); private List<String> titleList = new ArrayList<>(); private VpContentAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWidget(); } private void initWidget() { tabLayout = (TabLayout) findViewById(R.id.tl_title); vpContent = (ViewPager) findViewById(R.id.vp_content); //传递参数 Bundle bundle1 = new Bundle(); bundle1.putString(ContentFragment.TAG, "111111111"); fragmentList.add(ContentFragment.getInstance(bundle1)); titleList.add("第一页"); //传递参数 Bundle bundle2 = new Bundle(); bundle2.putString(ContentFragment.TAG, "222222222"); fragmentList.add(ContentFragment.getInstance(bundle2)); titleList.add("第二页"); //传递参数 Bundle bundle3 = new Bundle(); bundle3.putString(ContentFragment.TAG, "33333333"); fragmentList.add(ContentFragment.getInstance(bundle3)); titleList.add("第三页"); //传递参数 Bundle bundle4 = new Bundle(); bundle4.putString(ContentFragment.TAG, "44444444"); fragmentList.add(ContentFragment.getInstance(bundle4)); titleList.add("第四页"); //传递参数 Bundle bundle5 = new Bundle(); bundle5.putString(ContentFragment.TAG, "5555555"); fragmentList.add(ContentFragment.getInstance(bundle5)); titleList.add("第五页"); //传递参数 Bundle bundle6 = new Bundle(); bundle6.putString(ContentFragment.TAG, "666666"); fragmentList.add(ContentFragment.getInstance(bundle6)); titleList.add("第六页"); tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorAccent)); tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); tabLayout.setupWithViewPager(vpContent); adapter = new VpContentAdapter(getSupportFragmentManager(), fragmentList, titleList); vpContent.setAdapter(adapter); vpContent.setOffscreenPageLimit(fragmentList.size() - 1); vpContent.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); }}二、activity_mian布局文件代码是:
<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="com.tabfragment.MainActivity"> <android.support.design.widget.TabLayout android:id="@+id/tl_title" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabSelectedTextColor="@color/colorAccent" app:tabTextColor="@color/colorPrimary" /> <android.support.v4.view.ViewPager android:id="@+id/vp_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/tl_title" /></RelativeLayout>三、VpContentAdapter适配器的代码是:
package com.tabfragment;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import java.util.ArrayList;import java.util.List;public class VpContentAdapter extends FragmentPagerAdapter { private List<Fragment> fragmentList = new ArrayList<>(); private List<String> titleList = new ArrayList<>(); public VpContentAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titleList) { super(fm); this.fragmentList = fragmentList; this.titleList = titleList; } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return titleList.size(); } @Override public CharSequence getPageTitle(int position) { return titleList.get(position); }}四、ContentFragment页面的代码:
package com.tabfragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class ContentFragment extends Fragment { public static final String TAG = "tag"; private String mTextId = "000000"; private TextView tvContent; public static ContentFragment getInstance(Bundle bundle) { ContentFragment contentFragment = new ContentFragment(); contentFragment.setArguments(bundle); return contentFragment; } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_content, null, false); //获取传递过来的参数 mTextId = getArguments().getString(TAG); tvContent = (TextView) view.findViewById(R.id.content); tvContent.setText(mTextId); return view; }}
阅读全文
0 0
- Android 选项卡页面实现
- Android-Prefrence实现选项页面简介
- Android 选项卡实现
- ViewPager+PagerTabStrip实现页面选项卡滑动
- FragmentTabHost+Fragment实现选项卡页面
- Android FragmentTabhost实现选项卡
- jsp 页面选项卡
- 任务一:使用选项卡实现页面切换
- CSS+JS实现同一页面可以重复使用的选项卡
- 点击选项卡后页面标题切换的实现
- Android选项卡Tab的实现
- Android中选项卡功能的实现
- Android 多个Activity选项卡实现
- Android ViewPager Fragment实现选项卡
- android RadioButton实现选项卡的效果
- 【Android】利用TabHost实现选项卡效果
- Android 选项卡功能的实现
- Android使用TabLayout实现选项卡
- Thread VS Runnable
- 倒计时(android)
- 引用——C++:bitset类的使用与简介
- JQuery频繁修改select的选中时的失效问题
- git的使用总结
- Android 选项卡页面实现
- Oracle使用实例:exp和expdp导出数据
- Intel四子星 高密度服务器介绍适合IDC HPC
- 用iframe 引入整个html页面
- 求数组所有元素和
- 浅谈移动端之js touch事件 手势滑动事件
- numpy函数之random.choice
- Select 的绑定值为对象类型时用法 同时获取option选项的label和value值
- 增、改生产订单组件BAPI BAPI_ALM_ORDER_MAINTAIN