Android底部菜单(Fragment控制切换多个页面)

第一次写博客,希望对大家有所帮助

关于Android底部菜单、侧滑菜单现在用的都比较多,我有时间会给大家多整理这类似的资料,希望能够帮助有需要的人!

首先讲一下思路,就是在主页面里面用一个线性布局把所有的类容都包住,然后分为上下两个部分,上面是FrameLayout,用来存放不同的Fragment,下面是一个线性布局,线性布局里面有多个小的线性布局,这些小的线性布局就是所谓的“菜单”,点击不同的“菜单”,上面切换不同的Fragment进行显示。

好了,下面直接附上代码

首先是菜单栏include_main.xml



    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    


这里用到的图片( @drawable/home)大家随便找一张代替就可以了,主要是看一下效果就好!

有了菜单栏之后便是我们需要的几个Fragment了,因为一个菜单对应了便是一个Fragment,所以你写一个菜单选项就需要几个Fragment了!名字可以随便取,我取的名字分别是:FirstFrament、TwoFrament、ThreeFrament、FourFrament、FiveFrament;就是五个Fragment而已,自己写的时候区别一下就好了,好吧,还是上源码。

public class FirstFrament extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frament_first,null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }
}
 
  
public class TwoFrament extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frament_two,null);
    }
}
public class ThreeFrament extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frament_three,null);
    }
}
public class FourFrament extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frament_four,null);
    }
}
public class FiveFrament extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frament_five,null);
    }
}
当然每一个Fragment也对应这一个xml文件,分别是
frament_first、frament_two、frament_three、frament_four、frament_five,布局文件就不贴出来了,自己想怎么写都行,里面有一点区别方便自己区分确实是切换了页面就行了。
然后这些辅助行了页面和类写好了之后,便是我们的首页的布局文件和Action了;
主页的布局文件是:activity_main


    
    
主方法是 MainActivity.java,这里的 BaseActivity 里面其实什么都没有只是继承了Activity而已。 @InjectView是注解,Android Studio引用了
compile 'com.jakewharton:butterknife:6.1.0'
之后的。大家也可以自己手动findViewById进行导入!

public class MainActivity extends BaseActivity {

    @InjectView(R.id.include_main_first_image)
    ImageView includeMainFirstImage;
    @InjectView(R.id.include_main_first_text)
    TextView includeMainFirstText;
    @InjectView(R.id.include_main_first_linear)
    LinearLayout includeMainFirstLinear;
    @InjectView(R.id.include_main_two_image)
    ImageView includeMainTwoImage;
    @InjectView(R.id.include_main_two_text)
    TextView includeMainTwoText;
    @InjectView(R.id.include_main_two_linear)
    LinearLayout includeMainTwoLinear;
    @InjectView(R.id.include_main_three_image)
    ImageView includeMainThreeImage;
    @InjectView(R.id.include_main_three_text)
    TextView includeMainThreeText;
    @InjectView(R.id.include_main_three_linear)
    LinearLayout includeMainThreeLinear;
    @InjectView(R.id.include_main_four_image)
    ImageView includeMainFourImage;
    @InjectView(R.id.include_main_four_text)
    TextView includeMainFourText;
    @InjectView(R.id.include_main_four_linear)
    LinearLayout includeMainFourLinear;
    @InjectView(R.id.include_main_five_image)
    ImageView includeMainFiveImage;
    @InjectView(R.id.include_main_five_text)
    TextView includeMainFiveText;
    @InjectView(R.id.include_main_five_linear)
    LinearLayout includeMainFiveLinear;
    public FirstFrament firstFrament = new FirstFrament();
    public TwoFrament twoFrament = new TwoFrament();
    public ThreeFrament threeFrament = new ThreeFrament();
    public FourFrament fourFrament = new FourFrament();
    public FiveFrament fiveFrament = new FiveFrament();
    public Fragment fragment;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.main_frame, firstFrament).show(firstFrament).commit();
            fragment = firstFrament;
        }
        
    /**
     * 菜单栏的点击事件
     *
     * @param v
     */
    public void onMainClick(View v) {
        switch (v.getId()) {
            case R.id.include_main_first_linear:
                if (fragment != firstFrament) {
                    if (firstFrament.isAdded()) {
                        getFragmentManager().beginTransaction().show(firstFrament).hide(fragment).commit();
                    } else {
                        getFragmentManager().beginTransaction().add(R.id.main_frame, firstFrament).show(firstFrament).hide(fragment).commit();
                    }
                    fragment = firstFrament;
                }
                break;
            case R.id.include_main_two_linear:
                if (fragment != twoFrament) {
                    if (twoFrament.isAdded()) {
                        getFragmentManager().beginTransaction().show(twoFrament).hide(fragment).commit();
                    } else {
                        getFragmentManager().beginTransaction().add(R.id.main_frame, twoFrament).show(twoFrament).hide(fragment).commit();
                    }
                    fragment = twoFrament;
                }
                break;
            case R.id.include_main_three_linear:
                if (fragment != threeFrament) {
                    if (threeFrament.isAdded()) {
                        getFragmentManager().beginTransaction().show(threeFrament).hide(fragment).commit();
                    } else {
                        getFragmentManager().beginTransaction().add(R.id.main_frame, threeFrament).show(threeFrament).hide(fragment).commit();
                    }
                    fragment = threeFrament;
                }
                break;
            case R.id.include_main_four_linear:
                if (fragment != fourFrament) {
                    if (fourFrament.isAdded()) {
                        getFragmentManager().beginTransaction().show(fourFrament).hide(fragment).commit();
                    } else {
                        getFragmentManager().beginTransaction().add(R.id.main_frame, fourFrament).show(fourFrament).hide(fragment).commit();
                    }
                    fragment = fourFrament;
                }
                break;
            case R.id.include_main_five_linear:
                if (fragment != fiveFrament) {
                    if (fiveFrament.isAdded()) {
                        getFragmentManager().beginTransaction().show(fiveFrament).hide(fragment).commit();
                    } else {
                        getFragmentManager().beginTransaction().add(R.id.main_frame, fiveFrament).show(fiveFrament).hide(fragment).commit();
                    }
                    fragment = fiveFrament;
                }
                break;
        }

    }
}
这样就实现了菜单栏的页面切换,大家还可以在点击事件里面添加自己需要的图片切换和字体的颜色切换,我相信大家在认真看完之后,应该自己自己需要怎么改了,如果大家写的好,请点个赞,谢谢!

你可能感兴趣的:(Android,UI)