Android中的Fragment页面切换和selector选择器

Posted 巫山老妖

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android中的Fragment页面切换和selector选择器相关的知识,希望对你有一定的参考价值。

效果如图:

提示:下面是用的整个的图片

下面看代码:

//--------------------这是主页面布局文件-----------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="shaoshan.bwie.com.shaoshan.activity.HomeActivity">

<FrameLayout
android:id="@+id/home_frameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>

<RadioGroup
android:id="@+id/home_rg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="2dp"
android:background="#FFFFFF"
android:orientation="horizontal">

<RadioButton
android:id="@+id/home_rb_personalcenter"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/radio_person"
android:button="@null" />

<RadioButton
android:id="@+id/home_rb_search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/radio_seach"
android:button="@null" />

<RadioButton
android:id="@+id/home_rb_home"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/radio_home"
android:button="@null" />

<RadioButton
android:id="@+id/home_rb_map"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/radio_map"
android:button="@null" />

<RadioButton
android:id="@+id/home_rb_more"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/radio_more"
android:button="@null" /> </RadioGroup>
</LinearLayout>

//--------------------这是我的主页面--------------------------

package shaoshan.bwie.com.shaoshan.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import shaoshan.bwie.com.shaoshan.R;
import shaoshan.bwie.com.shaoshan.activity.fragment.HomePageFragment;
import shaoshan.bwie.com.shaoshan.activity.fragment.MapFragment;
import shaoshan.bwie.com.shaoshan.activity.fragment.MoreFragment;
import shaoshan.bwie.com.shaoshan.activity.fragment.PersonalCenterFragment;
import shaoshan.bwie.com.shaoshan.activity.fragment.SeekFragment;

public class HomeActivity extends AppCompatActivity {


private RadioButton home_rb_personalcenter;
private RadioButton home_rb_search;
private RadioButton home_rb_home;
private RadioButton home_rb_map;
private RadioButton home_rb_more;
private RadioGroup home_rg;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//自动找控件 快捷键 Alt+insert 或 工具中Code——LayoutCreator
initView();

}

private void initView() {
home_rb_personalcenter = (RadioButton) findViewById(R.id.home_rb_personalcenter);
home_rb_search = (RadioButton) findViewById(R.id.home_rb_search);
home_rb_home = (RadioButton) findViewById(R.id.home_rb_home);
home_rb_map = (RadioButton) findViewById(R.id.home_rb_map);
home_rb_more = (RadioButton) findViewById(R.id.home_rb_more);
home_rg = (RadioGroup) findViewById(R.id.home_rg);
//创建Fragment
FragmentManager homePagemanager = getFragmentManager();
FragmentTransaction homePagetransaction = homePagemanager.beginTransaction();
HomePageFragment homePageFragment = new HomePageFragment();
homePagetransaction.replace(R.id.home_frameLayout, homePageFragment, "homePageFragment");
homePagetransaction.commit();
//设置默认选中
home_rb_home.setChecked(true);

//设置RadioGroup的选中监听
home_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (group.getCheckedRadioButtonId()) {
case R.id.home_rb_personalcenter:
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
PersonalCenterFragment personalCenterFragment = new PersonalCenterFragment();
transaction.replace(R.id.home_frameLayout, personalCenterFragment, "personalCenterFragment");
transaction.commit();
break;
case R.id.home_rb_search:
FragmentManager seekmanager = getFragmentManager();
FragmentTransaction seektransaction = seekmanager.beginTransaction();
SeekFragment seekFragment = new SeekFragment();
seektransaction.replace(R.id.home_frameLayout, seekFragment, "seekFragment");
seektransaction.commit();
break;
case R.id.home_rb_home:
FragmentManager homePagemanager = getFragmentManager();
FragmentTransaction homePagetransaction = homePagemanager.beginTransaction();
HomePageFragment homePageFragment = new HomePageFragment();
homePagetransaction.replace(R.id.home_frameLayout, homePageFragment, "homePageFragment");
homePagetransaction.commit();
break;
case R.id.home_rb_map:
FragmentManager mapmanager = getFragmentManager();
FragmentTransaction maptransaction = mapmanager.beginTransaction();
MapFragment mapFragment = new MapFragment();
maptransaction.replace(R.id.home_frameLayout, mapFragment, "mapFragment");
maptransaction.commit();
break;
case R.id.home_rb_more:
FragmentManager moremanager = getFragmentManager();
FragmentTransaction moretransaction = moremanager.beginTransaction();
MoreFragment moreFragment = new MoreFragment();
moretransaction.replace(R.id.home_frameLayout, moreFragment, "moreFragment");
moretransaction.commit();
break;
}
}
});


}


}

//------------下面这是我的背景选择器------------这只是其中一个,其他四个也类同,在drawable下创建XML文件--------------------

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/bt_home" android:state_checked="false"></item>
<item android:drawable="@drawable/bt_home2" android:state_checked="true"></item>
</selector>


//-------------如下------------------------

 

注释:其中的几个Fragment我就不写了,很简单,创建一个类继承Fragment,实现onCreateView的方法

如下:这是其中一个

public class MapFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.map_fragment_item,null);
return view;
}
}

 

以上是关于Android中的Fragment页面切换和selector选择器的主要内容,如果未能解决你的问题,请参考以下文章

Android——Fragment实例精讲——底部导航栏+ViewPager滑动切换页面

Android 一个Activity 里面放置多个 Fragment 实现点击切换的Tab 页面效果

Android中如何为Fragment中的按钮添加监听事件

ViewPager+Fragment实现页面的切换

Android UI-实现底部切换标签(fragment)

android------页面切换到传递数据