RadioGroup配合切换Fragment实现主界面

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载,违者必究。 https://blog.csdn.net/Cricket_7/article/details/89374633

【1】布局文件中添加

  • 样式抽取

viewpage_selector_slide_title

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:state_pressed="true" android:color="#0b9a27"/>

    <item android:state_selected="true" android:color="#0b9a27"/>

    <item android:state_checked="true" android:color="#0b9a27"/>

    <item android:color="#999"/>

</selector>
<style name="tab_style">

        <item name="android:textColor">@color/viewpage_selector_slide_title</item>

        <item name="android:textSize">14sp</item>

        <item name="android:button">@null</item>

        <item name="android:gravity">center</item>

        <item name="android:layout_width">0dp</item>

        <item name="android:layout_weight">1</item>

        <item name="android:layout_height">match_parent</item>

    </style>
  • selector 选择器文件

tab_icon_new

<?xml version="1.0" encoding="utf-8"?>

<selector

  xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@drawable/widget_bar_news_over" />

    <item android:state_checked="true" android:drawable="@drawable/widget_bar_news_over" />

    <item android:state_selected="true" android:drawable="@drawable/widget_bar_news_over" />

    <item android:drawable="@drawable/widget_bar_news_nor" />

</selector>

tab_icon_tweet

<?xml version="1.0" encoding="utf-8"?>

<selector

  xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@drawable/widget_bar_tweet_over" />

    <item android:state_checked="true" android:drawable="@drawable/widget_bar_tweet_over" />

    <item android:state_selected="true" android:drawable="@drawable/widget_bar_tweet_over" />

    <item android:drawable="@drawable/widget_bar_tweet_nor" />

</selector>

tab_icon_explore

<?xml version="1.0" encoding="utf-8"?>

<selector

  xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@drawable/widget_bar_explore_over" />

    <item android:state_checked="true" android:drawable="@drawable/widget_bar_explore_over" />

    <item android:state_selected="true" android:drawable="@drawable/widget_bar_explore_over" />

    <item android:drawable="@drawable/widget_bar_explore_nor" />

</selector>

tab_icon_me

<?xml version="1.0" encoding="utf-8"?>

<selector

  xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@drawable/widget_bar_me_over" />

    <item android:state_checked="true" android:drawable="@drawable/widget_bar_me_over" />

    <item android:state_selected="true" android:drawable="@drawable/widget_bar_me_over" />

    <item android:drawable="@drawable/widget_bar_me_nor" />

</selector>

btn_quickoption_selector

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_quickoption_nor" android:state_focused="false" android:state_pressed="false"/>

    <item android:drawable="@drawable/btn_quickoption_pressed" android:state_pressed="true"/>

</selector>
  • 布局

  

<FrameLayout

            android:id="@+id/fl_container"

            android:layout_width="match_parent"

            android:layout_height="0dp"

            android:layout_weight="1"/>

        <RadioGroup

            android:id="@+id/rg_group"

            android:background="@color/white"

            android:layout_width="match_parent"

            android:orientation="horizontal"

            android:gravity="center_vertical"

            android:layout_height="60dp">

            <RadioButton

                style="@style/tab_style"

                android:id="@+id/rb_all"

                android:text="综合"

                android:drawableTop="@drawable/tab_icon_new"/>

            <RadioButton

                style="@style/tab_style"

                android:id="@+id/rb_tweet"

                android:text="动弹"

                android:drawableTop="@drawable/tab_icon_tweet"/>

            <TextView

                style="@style/tab_style"

                android:id="@+id/rb_add"

                android:paddingTop="10dp"

                android:drawableTop="@drawable/btn_quickoption_selector"/>

            <RadioButton

                style="@style/tab_style"

                android:id="@+id/rb_explore"

                android:text="发现"

                android:drawableTop="@drawable/tab_icon_explore"/>

            <RadioButton

                style="@style/tab_style"

                android:id="@+id/rb_me"

                android:text="我"

                android:drawableTop="@drawable/tab_icon_me"/>

        </RadioGroup>

【2】RadioGroup配合切换Fragment实现主界面

  • RadioGroup设置监听器

   rgGroup.setOnCheckedChangeListener(this);
  • 接口实现

public class MainActivity extends AppCompatActivity implements

        RadioGroup.OnCheckedChangeListener{}
  • 实现对应的点击事件

@Override

    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId){

            case R.id.rb_all:

                switchFragment(0);

                break;

            case R.id.rb_tweet:

                switchFragment(1);

                break;

            case R.id.rb_add:

                Toast.makeText(this, "弹出对话框", Toast.LENGTH_SHORT).show();

                break;

            case R.id.rb_explore:

                switchFragment(2);

                break;

            case R.id.rb_me:

                switchFragment(3);

                break;

        }

    }

先进行初始化添加Fragment 

  •  
  ArrayList<Fragment> fragments = new ArrayList<>();

    private void prepareFragments() {

        fragments.add(new AllFragment());

        fragments.add(new TweetFragment());

        fragments.add(new ExploreFragment());

        fragments.add(new MeFragment());

    }

切换点中的fargment

  •  
/**

     * 切换Fragment

     */

    private void switchFragment(int posi){

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        for (int i = 0; i < fragments.size(); i++) {

            Fragment fragment = fragments.get(i);

            if(i==posi){

                //说明要显示这个fragment

                if(fragment.isAdded()){

                    transaction.show(fragment);

                }else {

                    //如果没加进来。那么就添加,

                    transaction.add(R.id.fl_container,fragment);

                }

            }else {

                //说明要隐藏这个fragment

                if(fragment.isAdded()){

                    transaction.hide(fragment);

                }

            }

        }





        //最后提交

        transaction.commitAllowingStateLoss();

    }

设置默认被选中的

  •  

 

       //切换Fragment代码

        prepareFragments();

        //设置默认被选中的RadioButton

        rgGroup.check(R.id.rb_all);

        //默认选中的flagment

        switchFragment(0);

3,全部代码

public class MainActivity extends AppCompatActivity implements

        RadioGroup.OnCheckedChangeListener{

    @Bind(R.id.toolbar)

    Toolbar toolbar;

    @Bind(R.id.fl_container)

    FrameLayout flContainer;

    @Bind(R.id.rb_all)

    RadioButton rbAll;

    @Bind(R.id.rb_tweet)

    RadioButton rbTweet;

    @Bind(R.id.rb_add)

    TextView rbAdd;

    @Bind(R.id.rb_explore)

    RadioButton rbExplore;

    @Bind(R.id.rb_me)

    RadioButton rbMe;

    @Bind(R.id.rg_group)

    RadioGroup rgGroup;

    @Bind(R.id.drawerLayout)

    DrawerLayout drawerLayout;

    private ActionBarDrawerToggle drawerToggle;

    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        //切换Fragment代码

        prepareFragments();

        //设置默认被选中的RadioButton

        rgGroup.check(R.id.rb_all);

        switchFragment(0);

        rgGroup.setOnCheckedChangeListener(this);

        rbAdd.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Toast.makeText(MainActivity.this, "弹出对话框onClick", Toast.LENGTH_SHORT).show();

            }

        });

    }

    ArrayList<Fragment> fragments = new ArrayList<>();

    private void prepareFragments() {

        fragments.add(new AllFragment());

        fragments.add(new TweetFragment());

        fragments.add(new ExploreFragment());

        fragments.add(new MeFragment());

    }

    /**

     * 切换Fragment

     */

    private void switchFragment(int posi){

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        for (int i = 0; i < fragments.size(); i++) {

            Fragment fragment = fragments.get(i);

            if(i==posi){

                //说明要显示这个fragment

                if(fragment.isAdded()){

                    transaction.show(fragment);

                }else {

                    //如果没加进来。那么就添加,

                    transaction.add(R.id.fl_container,fragment);

                }

            }else {

                //说明要隐藏这个fragment

                if(fragment.isAdded()){

                    transaction.hide(fragment);

                }

            }

        }





        //最后提交

        transaction.commitAllowingStateLoss();

    }

    @Override

    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId){

            case R.id.rb_all:

                switchFragment(0);

                break;

            case R.id.rb_tweet:

                switchFragment(1);

                break;

            case R.id.rb_add:

                Toast.makeText(this, "弹出对话框", Toast.LENGTH_SHORT).show();

                break;

            case R.id.rb_explore:

                switchFragment(2);

                break;

            case R.id.rb_me:

                switchFragment(3);

                break;

        }

    }

}

猜你喜欢

转载自blog.csdn.net/Cricket_7/article/details/89374633