Fragment+RadioButton实现Tab页切换
来源:互联网 发布:淘宝跑腿办事 编辑:程序博客网 时间:2024/06/17 07:26
- 总所周知,实现一个App的Tab页切换的方式有很多种。例如使用ViewPager+Fragment,RadioButton+Fragment。今天主要来利用RadioButton来实现。
首先我们绘制一个布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/activity_main" android:background="@color/page_bg" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/fl_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <RadioGroup android:id="@+id/main_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:background="@color/white" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_information" android:button="@null" android:checked="true" android:text="资讯" android:drawablePadding="3dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:drawableTop="@drawable/main_information_selector" /> <RadioButton android:id="@+id/rb_video" android:button="@null" android:text="短视频" android:drawablePadding="3dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:drawableTop="@drawable/main_video_selector" /> <RadioButton android:id="@+id/rb_me" android:button="@null" android:text="我的" android:drawablePadding="3dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:drawableTop="@drawable/main_me_selector" /> </RadioGroup></LinearLayout>
好吧,直接java代码吧
package com.wj.gamingheadlines;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.FrameLayout;import android.widget.RadioButton;import android.widget.RadioGroup;import com.wj.gamingheadlines.fragments.InformationFragment;import com.wj.gamingheadlines.fragments.MeFragment;import com.wj.gamingheadlines.fragments.VideoFragment;import java.lang.reflect.InvocationTargetException;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;/** * 主页面 */public class MainActivity extends AppCompatActivity { @BindView(R.id.rb_information) RadioButton rbInformation; @BindView(R.id.rb_video) RadioButton rbVideo; @BindView(R.id.rb_me) RadioButton rbMe; private Fragment mShowFragment; private FragmentManager fm; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); fm=getSupportFragmentManager(); initCheck(); } private void initCheck() { FragmentTransaction transaction = fm.beginTransaction(); mShowFragment=new InformationFragment(); transaction.add(R.id.fl_content,mShowFragment,InformationFragment.TAG); transaction.commit(); } @OnClick({R.id.rb_information, R.id.rb_video, R.id.rb_me}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.rb_information: switchPages(InformationFragment.TAG,InformationFragment.class); break; case R.id.rb_video: switchPages(VideoFragment.TAG,VideoFragment.class); break; case R.id.rb_me: switchPages(MeFragment.TAG,MeFragment.class); break; } } //切换显示Fragment private void switchPages(String tag, Class<? extends Fragment> cls) {// FragmentManager fm = getSupportFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); //隐藏显示页面 transaction.hide(mShowFragment); //根据TAG去FragmentManager寻找碎片 mShowFragment = fm.findFragmentByTag(tag); if (mShowFragment != null) { transaction.show(mShowFragment); } else { try { //使用反射实例化一个对象 mShowFragment = cls.getConstructor().newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } transaction.add(R.id.fl_content, mShowFragment, tag); } transaction.commit(); }}
- 其中使用的是ButterKnife,减少findViewById的繁复工作。切换是不是很简单,就是一个方法的事儿。
0 0
- Fragment+RadioButton实现Tab页切换
- ViewPager、Fragment、RadioButton实现顶部切换Tab页面
- RadioButton实现Fragment切换
- 底部RadioButton实现Fragment切换
- RadioGroup+Fragment实现Tab切换
- fragment+Radiobutton实现顶部导航切换
- Fragment+RadioButton实现点击切换页面效果
- Fragment+RadioButton实现点击切换页面效果
- 使用Fragment+ViewPager,仿微信实现多页Tab切换
- RadioGroup+RadioButton切换Fragment
- android:使用fragment实现tab切换
- 实现滚动tab切换fragment + viewpager
- 用Fragment实现微信Tab切换
- Fragment 实现tab页卡切换并保存界面状态,动态添加Fragment
- Android ViewPager RadioButton Fragment FragmentPagerAdapter Tab页滑动
- fragment中添加ViewPager实现界面切换,动态添加radiobutton
- Fragment实现Tab页切换,滑动和点击都可切换。
- Fragment+RadioButton点击切换页面
- 灰度处理,二值化,卷积滤镜等基本算法汇总
- java核心技术 程序清单4-5 ConstructorTest
- 按home建 和跳转到主界面
- HTTP协议详解(真的很牛逼)
- ssh:对于java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I错误解决
- Fragment+RadioButton实现Tab页切换
- 设计模式之桥接模式
- Ecshop模板开发(十):商品详情页商品属性参数
- android studio中requestWindowFeature(Window.FEATURE_NO_TITLE)隐藏标题栏失效的解决方案
- 6.4(fact方法声明异常,主方法捕获异常)求n!,阶乘值是byte类型
- oninput、onchange、onpropertychange的区别
- Java数据库连接——JDBC基础知识(操作数据库:增删改查)
- PAT L3-013. 非常弹的球 高中物理题
- BZOJ 1070 [SCOI2007]修车