ViewPage+RadioGroup实现Tab切换

2023-05-16

以前写过一篇也是Tab切换的文章,当时做安卓一年左右,单纯觉得这样能实现功能,但是没有注意到性能和代码简洁性的问题,文章为  http://blog.csdn.net/nzzl54/article/details/52537320

今天的文章些许借鉴了这篇文章,这里面也介绍了几种Tab切换的设计,有兴趣的可以去看看:

http://blog.csdn.net/u010583599/article/details/52056894

现在有更好的方法来实现Tab的切换,这里我用的是ViewPage+RadioGroup的方法,当然,最快捷的一定是ViewPage+TabLayout的方法,只是TabLayout的切换支持的效果比较少,用RadioGroup我们可以设计一些自定义的图标或者别的花样,这里我打算简单点,设计上图标下文字的Tab,这里给出主要代码。
一、设计点击时的文字颜色切换(放在res文件夹的color文件夹下,如果没有自己建立文件夹)radio_button_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/blue" android:state_checked="true"/>
    <item android:color="@color/ps_999999"/>
</selector>  

radioButton的样式:

<style name="rb_bottom_style">
    <item name="android:textSize">@dimen/font_12</item>
    <item name="android:textColor">@color/gray_666666</item>
    <item name="android:ellipsize">marquee</item>
    <item name="android:gravity">center</item>
    <item name="android:button">@null</item>
    <item name="android:singleLine">true</item>
    <item name="android:background">@null</item>
    <item name="android:drawablePadding">@dimen/space_5</item>
    <item name="android:paddingTop">@dimen/space_5</item>
</style>  

二、布局文件
(1)main_botton_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:id="@+id/flBottom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="@dimen/space_5"
        android:paddingBottom="@dimen/space_5"
        android:background="#F7F7F7">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <RadioGroup
                android:id="@+id/main_radio"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:gravity="center_vertical"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/rbtab0"
                    style="@style/rb_bottom_style"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:checked="true"
                    android:drawableTop="@drawable/rb_main_news_selector"
                    android:text="嘻嘻"
                    android:textColor="@color/radio_button_text"/>

                <RadioButton
                    android:id="@+id/rbtab1"
                    style="@style/rb_bottom_style"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:drawableTop="@drawable/rb_main_news_selector"
                    android:text="哈哈"
                    android:textColor="@color/radio_button_text"/>

                <RadioButton
                    android:id="@+id/rbtab2"
                    style="@style/rb_bottom_style"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:drawableTop="@drawable/rb_main_news_selector"
                    android:text="呵呵"
                    android:textColor="@color/radio_button_text"/>

                <RadioButton
                    android:id="@+id/rbtab3"
                    style="@style/rb_bottom_style"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:drawableTop="@drawable/rb_main_news_selector"
                    android:text="其他"
                    android:textColor="@color/radio_button_text"/>
            </RadioGroup>
        </RelativeLayout>
    </FrameLayout>
</RelativeLayout>

(2)fragment_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white_ffffff">

    <LinearLayout
        android:id="@+id/layout_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">
        <TextView
            android:id="@+id/tv_fragment"
            android:padding="@dimen/space_10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="测试Fragment"
            android:textColor="@color/white_ffffff"
            android:gravity="center"
            android:background="@color/red"/>
    </LinearLayout>

</LinearLayout>

(3)activity_main_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white_ffffff">

    <include layout="@layout/include_toolbar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/test_slight_blue"/>

    <include layout="@layout/main_botton_tab" />
</LinearLayout>

三、代码
(1)Fragment:就展示TextView,布局已在上面给出

  
/**
 * Created by lan.zheng on 2018/3/5.
 *
 */

@SuppressLint("ValidFragment")
public class tabTestFragment extends BaseFragment{
    private TextView tv_fragment;
    private String setText = "";

    @SuppressLint("ValidFragment")
    public tabTestFragment(String s){
        setText = s;
    }

    private View view;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_test, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView(){
        tv_fragment = (TextView)view.findViewById(R.id.tv_fragment);
        tv_fragment.setText(setText);
        tv_fragment.setBackgroundColor(getResources().getColor(R.color.test_green));
    }

}

(2)Activity:四个Fragement和Viewpage+Radiogroup的用法
/**
 * Created by lan.zheng on 2018/3/5.
 * tab切换的首页
 */
public class MainTabActivity extends AppCompatActivity {

    public Toolbar mToolbar;
    public ToolBarManager mToolBarManager;
    private ViewPager viewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments = new ArrayList<Fragment>();

    /**
     * fragment页面
     */
    private Fragment fragment0;
    private Fragment fragment1;
    private Fragment fragment2;
    private Fragment fragment3;

    /**
     * tab切换控制
     */
    private RadioGroup main_radio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tab);
        findView();
        initToolBar();
        initData();
        initTab();
    }

    private void findView(){
        main_radio = (RadioGroup)findViewById(R.id.main_radio);
        main_radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbtab0:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.rbtab1:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.rbtab2:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.rbtab3:
                        viewPager.setCurrentItem(3);
                        break;
                    default:
                        break;
                }

            }
        });

    }

    private void initToolBar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolBarManager = new ToolBarManager(mToolbar, this);
        mToolBarManager.init()
                .showBack(true).setTitle("测试");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        viewPager = (ViewPager)findViewById(R.id.container);
        viewPager.setOffscreenPageLimit(4); //防止Fragment被回收
    }

    private void initData(){
        fragment0 = new tabTestFragment("第一个页面");
        fragment1 = new tabTestFragment("第二个页面");
        fragment2 = new tabTestFragment("第三个页面");
        fragment3 = new tabTestFragment("第四个页面");
        mFragments.add(fragment0);
        mFragments.add(fragment1);
        mFragments.add(fragment2);
        mFragments.add(fragment3);
        mAdapter = new FragmentPagerAdapter( getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return mFragments.get(arg0);
            }
        };
        viewPager.setAdapter(mAdapter);
    }

    private void initTab(){
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                //当前选中的Fragment 下标
                int checkButtonId = 0;
                switch (main_radio.getCheckedRadioButtonId()){
                    case R.id.rbtab0:
                        checkButtonId = 0;
                        LogUtils.LogError("lenita","currentItem = "+arg0+",checkButtonId = "+checkButtonId);
                        break;
                    case R.id.rbtab1:
                        checkButtonId = 1;
                        LogUtils.LogError("lenita","currentItem = "+arg0+",checkButtonId = "+checkButtonId);
                        break;
                    case R.id.rbtab2:
                        checkButtonId = 2;
                        LogUtils.LogError("lenita","currentItem = "+arg0+",checkButtonId = "+checkButtonId);
                        break;
                    case R.id.rbtab3:
                        checkButtonId = 3;
                        LogUtils.LogError("lenita","currentItem = "+arg0+",checkButtonId = "+checkButtonId);
                        break;
                }
                if(arg0 == checkButtonId){
                    //当一样的时候,不需要改变tab项,原因是在点击的时候已经把页面也定位到了对应的Fragment
                    return;
                }
                setCheckButton(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }


    private void setCheckButton(int position){
        switch (position){
            case 0:
                main_radio.check(R.id.rbtab0);
                break;
            case 1:
                main_radio.check(R.id.rbtab1);
                break;
            case 2:
                main_radio.check(R.id.rbtab2);
                break;
            case 3:
                main_radio.check(R.id.rbtab3);
                break;
        }
    }

}
注意:fragment过多的时候,有可能被回收,因此要写 viewPager.setOffscreenPageLimit(4)
具体可以参考这篇文章,方法很多,可以参考文章 点击打开链接http://blog.csdn.net/nzzl54/article/details/68944000

效果图如下:


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ViewPage+RadioGroup实现Tab切换 的相关文章

  • ViewPage+RadioGroup实现Tab切换

    以前写过一篇也是Tab切换的文章 xff0c 当时做安卓一年左右 xff0c 单纯觉得这样能实现功能 xff0c 但是没有注意到性能和代码简洁性的问题 xff0c 文章为 http blog csdn net nzzl54 article
  • Visual Studio 中 Tab 转换为空格的设置

    在 Visual Studio 中写代码时 xff0c 按 Tab 键 xff0c 会自动进行缩进 有时希望实现按 Tab 键 xff0c 出现多个空格的效果 Visual Studio 提供了这样的功能 xff0c 具体设置方法为 xff
  • Notepad++ 中 tab 转 空格

    目录 一 引言 二 tab转空格 一 引言 为了便于观看 xff0c 以及防止程序出bug xff0c 一般不用tab键 xff0c 而是用4个空格代替 因为tab键在不同的软件里代表不同的空格 xff0c 有的代表4个空格 xff0c 有
  • 将linux文件中的tab更换为空格的三种方法

    将linux文件中的tab更换为空格的三种方法 1 xff0c 用sed命令 sed s t g filename gt filename1 2 用tr命令 cat filename tr 34 t 34 34 34 gt filename
  • viewpage+radiogroup

    lt xml version 61 34 1 0 34 encoding 61 34 utf 8 34 gt lt LinearLayout xmlns android 61 34 http schemas android com apk
  • Linux中将tab与空格互换

    在Vim中 xff0c 有时需要将tab转换成space 使用ret命令 xff08 replace tab xff09 range ret ab new tabstop 1 tab替换为空格 set ts 61 4 set expandt
  • Linux远程界面中Tab键不能补全

    我们在使用嵌入式Linux板子的时候 xff0c 时常需要使用到远程界面 xff0c 可以通过本地电脑对板子进行操作 xff0c 显得相对便捷 在远程界面的使用中 xff0c 不可避免地要在终端进行命令输入 xff0c 这时可能出现Tab键
  • 解决 debian TAB 键不能自动补全命令的原因

    weixin 33928137 2015 04 23 10 46 00 512 收藏 文章标签 xff1a 操作系统 版权 预约直播 xff1a 9月9日 12日 xff0c 携手众开源社区 xff0c 开发者们的年度盛会 开源大咖与开发者
  • HTML中的空格、Tab、书名号大于号以及常用特殊符号

    HTML字符实体 在HTML页面中 xff0c 有一些特殊的符号我们想使用 xff0c 但是呢又不方便直接使用 xff0c 那么我们就可以用一些实体名称来代替 注 xff1a 实体名称对大小写敏感 特殊字符描述实体名称 空格 空格 amp
  • 正则表达式匹配连续多个空格或tab空格

    Pattern p 61 Pattern compile 34 s 2 t 34 Matcher m 61 p matcher str String strNoBlank 61 m replaceAll 34 34 System out p
  • 软件测试tab键是必须实现的吗,C++ Tab键实现自动补全输入功能

    include include include include usingnamespacestd include include include tab键获取相关值 xff0c 并打印到屏幕上 voidtab find char std
  • JS中~ 面向对象编程制作tab栏

    面向对象编程制作tab栏 利用constructor属性接收实例对象传递过来的参数去获取和htm结构中的元素 xff0c 在类函数中对各种不同的功能封装成不同的函数 xff0c 在制作过程中相互调用 xff0c 第一步的点击上边按钮与此同时
  • Android_UI开发总结(一):RadioButton与RadioGroup使用

    关于RadioButton与RadioGroup的API详解 gt https www cnblogs com Im Victor p 6238437 html 下面记录在使用RadioButton和RadioGroup中遇到的三点问题 1
  • Android 动态 RadioGroup/RadioButtons 作为平面按钮

    这与a中提到的问题类似相关帖子但我认为它的不同足以提出自己的问题 事情是这样的 通过将单选按钮的按钮属性设置为 null 在 xml 中声明单选按钮时 我已经能够让 单选圆圈 消失 没有问题 如下所示
  • 在 SWT 中设置/获取 RadioGroupFieldEditor 的值

    我正在尝试在我正在开发的 Eclipse RCP 应用程序中添加 RadioGroupFieldEditor 但似乎无法做两件关键的事情 设置单选按钮的值 即当打开对话框 窗口时 我想将默认值设置为 button1 获取所选单选按钮的当前值
  • 从 ViewPager 获取焦点视图

    我使用 ViewPager 通过左 右滑动来切换视图 ViewPager 需要一个适配器 所以我构建了这个 public class ListViewPagerAdapter extends PagerAdapter protected s
  • MVC 4 Razor 中的多个单选按钮组

    I need to have multiple radio button groups in my form like this 我知道这只是通过指定相同的 name 每个组的 html 属性 HOWEVER在使用 html 帮助器时 MV
  • 如何从服务器在 ViewPager 内的 VideoView 上播放视频

    我尝试开发一个应用程序从服务器检索视频并在 viewpager 内的 videoview 上播放 原始文件夹中的视频工作正常 但有两个问题 1 部分视频无法播放 或黑色活动显示 2 页面滚动时视频不停止 那么如何使用 URL 而不是 and
  • 一次仅选择一个单选按钮

    我的单选按钮组中有两个单选按钮 我还有 2 androd button checkbox 用于取消选择单选按钮时 以及 checkbox v 用于用户选择复选框时 我还实现了一个方法onRadioButtonClick为了确保只有一个单选按
  • 在ListView.builder() flutter中动态创建单选按钮Group

    我想创建一个这样的用户界面 最后 我得到了所有选定单选按钮的详细信息 这是我的完整代码 当我尝试这样做时 所有单选按钮都转向一侧 import package flutter cupertino dart import package fl

随机推荐