[android] 练习viewpagerindicator的使用(二)
2023-02-18 15:47:23 时间
主要还是想实现滑动的tab切换效果
MainActivity.java
package com.example.csdn; import com.viewpagerindicator.TabPageIndicator; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MainActivity extends FragmentActivity { private TabPageIndicator tpi_tab; private ViewPager vp_content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tpi_tab = (TabPageIndicator) findViewById(R.id.vpi_tab); vp_content = (ViewPager) findViewById(R.id.vp_content); FragmentManager fm = getSupportFragmentManager(); TabAdapter adapter = new TabAdapter(fm); // ViewPager设置适配器 vp_content.setAdapter(adapter); // 指示器绑定ViewPager tpi_tab.setViewPager(vp_content, 0); } } /** * 标签部分适配器 * * @author taoshihan * */ class TabAdapter extends FragmentPagerAdapter { public static final String[] TITLES = new String[] { "业界", "移动", "研发","PHP","Android","Java","前端" }; public TabAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int arg0) { return new ContentFragment(arg0); } @Override public int getCount() { return TITLES.length; } @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return TITLES[position % TITLES.length]; } } /** * 内容部分 * @author taoshihan * */ class ContentFragment extends Fragment{ private int index; public ContentFragment(int index) { this.index=index; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=View.inflate(getActivity(), R.layout.main_content, null); TextView tv_content=(TextView) view.findViewById(R.id.tv_content); String[] titles=TabAdapter.TITLES; tv_content.setText(titles[index]); return view; } }
样式就是在清单文件中给activity填上主题
<activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/Theme.PageIndicatorDefaults"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
相关文章
- Bootstrap 排版
- Android 意图(Intent)和过滤器(Filter)
- Android 概述
- 开发H5程序或者小程序的时候,后端Web API项目在IISExpress调试中使用IP地址,便于开发调试
- 基于SqlSugar的开发框架循序渐进介绍(10)-- 利用axios组件的封装,实现对后端API数据的访问和基类的统一封装处理
- 使用uView UI+UniApp开发微信小程序--微信授权绑定和一键登录系统
- 使用uView UI+UniApp开发微信小程序--判断用户是否登录并跳转
- 使用uView UI+UniApp开发微信小程序
- 循序渐进BootstrapVue,开发公司门户网站(6)--- 门户网站后端内容管理
- 循序渐进BootstrapVue,开发公司门户网站(5)--- 使用实际数据接口代替本地Mock数据
- 循序渐进BootstrapVue,开发公司门户网站(4)--- 使用b-carousel-slide组件实现图片轮播以及vue-awesome-swiper实现图片滑动展示
- 循序渐进BootstrapVue,开发公司门户网站(3)--- 结合邮件发送,收集用户反馈信息
- 在Bootstrap开发框架基础上增加WebApi+Vue&Element的前端
- 在Bootstrap开发框架中使用Grid++报表
- 基于Metronic的Bootstrap开发框架--资产编码打印处理
- 基于Metronic的Bootstrap开发框架经验总结(14)--条码和二维码的生成及打印处理
- 基于Metronic的Bootstrap开发框架经验总结(10)--优化Bootstrap图标管理
- 基于Metronic的Bootstrap开发框架经验总结(7)--数据的导入、导出及附件的查看处理
- 基于Metronic的Bootstrap开发框架经验总结(6)--对话框及提示框的处理和优化
- 基于Metronic的Bootstrap开发框架经验总结(5)--Bootstrap文件上传插件File Input的使用