Android Fragment+NestedRadioGroup仿微信主界面(带底部图标切换页面无滑动)
Posted 彬sir哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Fragment+NestedRadioGroup仿微信主界面(带底部图标切换页面无滑动)相关的知识,希望对你有一定的参考价值。
1.实现仿微信(带底部图标切换页面无滑动),如下图:
2.做的项目如下图:
3.activity_main.xml
中,布局代码如下:
3.1 三个页面每一个界面
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0" >
</FrameLayout>
3.2 底部带的三个图标
<com.demo.test.view.NestedRadioGroup
android:id="@+id/rg_main_tabbar"
style="@style/Bottom.MainTabBar" >
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0" >
<RadioButton
android:id="@+id/rb_new_msg"
style="@style/Bottom.MainTabBar.NestedButton"
android:button="@null"
android:drawableTop="@drawable/selector_main_tabbar_newmsg"
android:checked="true"
android:text="消息" />
</FrameLayout>
<RadioButton
android:id="@+id/rb_index"
style="@style/Bottom.MainTabBar.Button"
android:button="@null"
android:drawableTop="@drawable/selector_main_tabbar_index"
android:text="首页" />
<RadioButton
android:id="@+id/rb_profile"
style="@style/Bottom.MainTabBar.Button"
android:button="@null"
android:drawableTop="@drawable/selector_main_tabbar_profile"
android:text="个人" />
</com.demo.test.view.NestedRadioGroup>
4.selector_main_tabbar_newmsg.xml
代码如下:
5.MainActivity.java
中代码如下,我不说多,直接理解就可以
......
private Fragment newMsgFragment;
private Fragment indexFragment;
private Fragment profileFragment;
......
public void init(){
newMsgFragment=new NewMsgFragment();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.add(R.id.container, newMsgFragment, "newMsgFragment");
transaction.addToBackStack("newMsgFragment");
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
NestedRadioGroup tabBarRg=(NestedRadioGroup) findViewById(R.id.rg_main_tabbar);
tabBarRg.setOnCheckedChangeListener(new NestedRadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(NestedRadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_new_msg:
if (newMsgFragment == null) {
newMsgFragment = new NewMsgFragment();
}
switchFragment(newMsgFragment, "newMsgFragment");
break;
case R.id.rb_index:
if (indexFragment == null) {
indexFragment = new IndexFragment();
}
switchFragment(indexFragment, "indexFragment");
break;
case R.id.rb_profile:
if (profileFragment == null) {
profileFragment = new ProfileFragment();
}
switchFragment(profileFragment, "profileFragment");
break;
default:
break;
}
}
});
}
public void switchFragment(Fragment fragment,String tag){
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment, tag);
transaction.addToBackStack(tag);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
}
6.源代码下载:
https://download.csdn.net/download/qq_35091074/19498799
以上是关于Android Fragment+NestedRadioGroup仿微信主界面(带底部图标切换页面无滑动)的主要内容,如果未能解决你的问题,请参考以下文章