先看一下效果吧
点击底部导航可以切换到相应的Fragment上,先看下布局文件吧:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 主界面布局 -->
<RadioGroup
android:id="@+id/group"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_gravity="top"
android:background="@drawable/buttom_background"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingTop="2dp">
<RadioButton
android:id="@+id/home"
style="@style/main_tab_bottom"
android:checked="true"
android:drawableTop="@drawable/home"
android:text="首页" />
<RadioButton
android:id="@+id/find"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/find"
android:text="发现"
/>
<RadioButton
android:id="@+id/publish"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/publish"
/>
<RadioButton
android:id="@+id/message"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/message"
android:text="消息" />
<RadioButton
android:id="@+id/person_center"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/person_center"
android:text="我的" />
</RadioGroup>
<!--由于5个Fragment,用户感觉不好,所以自定义ViewPager,禁止了ViewPager的滑动,如果想滑动可以直接 用ViewPager -->
<com.example.administrator.myapplication14.My.MyViewPager
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/group"
android:layout_alignParentTop="true" />
</RelativeLayout>
activity:
package com.example.administrator.myapplication14.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.administrator.myapplication14.Fragment.Fragment_Home;
import com.example.administrator.myapplication14.Fragment.Fragment_Find;
import com.example.administrator.myapplication14.Fragment.Fragment3;
import com.example.administrator.myapplication14.Fragment.Fragment4;
import com.example.administrator.myapplication14.Fragment.Fragment5;
import com.example.administrator.myapplication14.My.MoreWindow;
import com.example.administrator.myapplication14.My.MyViewPager;
import com.example.administrator.myapplication14.R;
import java.util.ArrayList;
import java.util.List;
/**
* ----------主页-----------
*/
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener, View.OnClickListener {
MyViewPager vp;
RadioGroup rg;
List<Fragment> fragmentList;
MoreWindow mMoreWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//把Fragment添加到集合里面去
fragmentList = new ArrayList<>();
fragmentList.add(new Fragment_Home());
fragmentList.add(new Fragment_Find());
fragmentList.add(new Fragment3());
fragmentList.add(new Fragment4());
fragmentList.add(new Fragment5());
//隐藏ActionBar
getSupportActionBar().hide();
//解析控件
vp = (MyViewPager) findViewById(R.id.content);
rg = (RadioGroup) findViewById(R.id.group);
//设置点击事件(多了个change,导包用RadioGroup.OnCheckedChangeListener)
but.setOnClickListener(this);
rg.setOnCheckedChangeListener(this);
vp.addOnPageChangeListener(this);
//通过适配器把Fragment添加到主界面上
//vp.setOffscreenPageLimit(3);
vp.setAdapter(new Myadapter(getSupportFragmentManager(), fragmentList));
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
//声明一个下标,并赋值
int index = -1;
switch (checkedId) {
case R.id.home:
index = 0;
break;
case R.id.find:
index = 1;
break;
case R.id.publish:
index = 2;
break;
case R.id.message:
index = 3;
break;
case R.id.person_center:
index = 4;
break;
}
//设置vp选项,与ViewButton关联起来
vp.setCurrentItem(index);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
RadioButton radioButton = (RadioButton) rg.getChildAt(position);
radioButton.setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
class Myadapter extends FragmentPagerAdapter {
List<Fragment> fragmentList;
public Myadapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return (fragmentList == null) ? 0 : fragmentList.size();
}
}
}
viewPager:
package com.example.administrator.myapplication14.My;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* Created by Administrator on 2016/7/13.
* 描述:禁止了ViewPager的滑动
*/
public class MyViewPager extends ViewPager {
//true 代表不能滑动 //false 代表能滑动
private boolean noScroll = true;
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setNoScroll(boolean noScroll) {
this.noScroll = noScroll;
}
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
}
@Override
public boolean onTouchEvent(MotionEvent arg0) {
/* return false;//super.onTouchEvent(arg0); */
if (noScroll)
return false;
else
return super.onTouchEvent(arg0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if (noScroll)
return false;
else
return super.onInterceptTouchEvent(arg0);
}
@Override
public void setCurrentItem(int item, boolean smoothScroll) {
super.setCurrentItem(item, smoothScroll);
}
@Override
public void setCurrentItem(int item) {
super.setCurrentItem(item,false);//表示切换的时候,不需要切换时间。
}
}
最后奉上参考地址:
[https://blog.csdn.net/kuaizilanqiu/article/details/52197631]