Android实现底部图标与Fragment的联动实例
时间:2021-07-19 07:59:53|栏目:Android代码|点击: 次
本文介绍了ndroid实现底部图标与Fragment的联动,分享给大家,希望此文章对各位有所帮助。
效果如下:
1.首先在res下的drawable下新建四个图标的xml,分别把图标的选中和未选中的状态设置好,所有的图片可以放在res下新建的一个drawable-xhdpi目录下,这里仅展示一个图标的xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/ic_nav_home_press"/> <item android:state_checked="false" android:drawable="@drawable/ic_nav_home"/> <item android:drawable="@drawable/ic_nav_home"/> </selector>
2.在布局中开始布局:
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.baway.lizongshu.view.activity.MainActivity"> <FrameLayout android:id="@+id/framelayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioGroup android:id="@+id/rg" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/fenlei" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="分类" android:button="@null" android:checked="true" android:drawableTop="@drawable/fenlei" android:gravity="center" android:textSize="12sp" android:tag="0" /> <RadioButton android:id="@+id/gouwuche" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="购物车" android:button="@null" android:drawableTop="@drawable/gouwuche" android:gravity="center" android:textSize="12sp" android:tag="1" /> <RadioButton android:id="@+id/qita" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="其他" android:button="@null" android:drawableTop="@drawable/qita" android:gravity="center" android:textSize="12sp" android:tag="2" /> <RadioButton android:id="@+id/wode" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="我的" android:button="@null" android:drawableTop="@drawable/wode" android:gravity="center" android:textSize="12sp" android:tag="3" /> </RadioGroup> </LinearLayout> </LinearLayout>
3.新建四个Fragment类,这里仅展示一个
public class FenleiFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fenlei, container, false); return view; } }
4. 主界面中:
public class MainActivity extends AppCompatActivity { private RadioGroup rg; private Fragment[] mfragments; private FragmentManager fm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); initdata(); } private void initdata() { //定义一个Fragment数组,存放四个Fragment mfragments=new Fragment[4]; mfragments[0]=new FenleiFragment(); mfragments[1]=new GouwucheFragment(); mfragments[2]=new QitaFragment(); mfragments[3]=new WodeFragment(); //获得Fragment管理者 fm = getSupportFragmentManager(); //处理 FragmentTransaction ft = fm.beginTransaction(); ft.add(R.id.framelayout,mfragments[0],"0"); ft.commit(); } private void initview() { rg=(RadioGroup) findViewById(R.id.rg); //RadioGroup的监听事件 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //找到当前选中的图标 RadioButton rb= (RadioButton) group.findViewById(checkedId); //找到所选图标的标签并转换为整数类型放到下面的方法中 int i = Integer.parseInt(rb.getTag().toString().trim()); showAndHideFragment(i); } }); } //展示和隐藏Fragment的方法 private void showAndHideFragment(int position) { FragmentTransaction transaction = fm.beginTransaction(); //如果没有fragment就在framelayout里面加上 if (!mfragments[position].isAdded()){ transaction.add(R.id.framelayout,mfragments[position],""+position); } //把所有的fragment设为隐藏 for (Fragment fragment:mfragments){ transaction.hide(fragment); } //把选中的设为显示 transaction.show(mfragments[position]); transaction.commit(); } }
上一篇:Android Webview与ScrollView的滚动兼容及留白处理的方法
栏 目:Android代码
下一篇:Android读取properties配置文件的实例详解
本文标题:Android实现底部图标与Fragment的联动实例
本文地址:http://www.codeinn.net/misctech/159076.html