Android Fragment解析及UI底部导航栏实例

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;



public class MainActivity extends FragmentActivity implements OnClickListener{

    //定义3个Fragment的对象
    private Fragment1 fg1;
    private Fragment2 fg2;
    private Fragment3 fg3;
    //帧布局对象,就是用来存放Fragment的容器
    private FrameLayout flayout;
    //定义底部导航栏的三个布局  每个布局对应一个选项按钮
    private RelativeLayout course_layout;
    private RelativeLayout found_layout;
    private RelativeLayout settings_layout;
    //定义底部导航栏中的ImageView与TextView
    private ImageView course_image;
    private ImageView found_image;
    private ImageView settings_image;
    private TextView course_text;
    private TextView settings_text;
    private TextView found_text;
    //定义要用的颜色值
    private int whirt = 0xFFFFFFFF;
    private int gray = 0xFF7597B3;
    private int blue =0xFF0AB2FB;
    //定义FragmentManager对象
    FragmentManager fManager;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fManager = getSupportFragmentManager(); //获取Manager以对fragment进行管理
        initViews();  //初始化布局控件  使代码结构更为清晰
    }
    
    
    //完成组件的初始化
    public void initViews()
    {
        course_image = (ImageView) findViewById(R.id.course_image);
        found_image = (ImageView) findViewById(R.id.found_image);
        settings_image = (ImageView) findViewById(R.id.setting_image);
        course_text = (TextView) findViewById(R.id.course_text);
        found_text = (TextView) findViewById(R.id.found_text);
        settings_text = (TextView) findViewById(R.id.setting_text);
        course_layout = (RelativeLayout) findViewById(R.id.course_layout);
        found_layout = (RelativeLayout) findViewById(R.id.found_layout);
        settings_layout = (RelativeLayout) findViewById(R.id.setting_layout);
        course_layout.setOnClickListener(this);  //绑定监听器
        found_layout.setOnClickListener(this); 
        settings_layout.setOnClickListener(this);
    }
    
    //重写onClick事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.course_layout:
            setChioceItem(0);
            break;
        case R.id.found_layout:
            setChioceItem(1);
            break;
        case R.id.setting_layout:
            setChioceItem(2);
            break;
        default:
            break;
        }
        
    }
    
    
    //定义一个选中一个item后的处理
    public void setChioceItem(int index)
    {
        //重置选项+隐藏所有Fragment
        FragmentTransaction transaction = fManager.beginTransaction();  
        clearChioce(); //每次获取响应事件后都要初始化整个布局
        hideFragments(transaction);
        switch (index) {
        case 0:
            course_image.setImageResource(R.drawable.ic_tabbar_course_pressed);  
            course_text.setTextColor(blue);
            
            if (fg1 == null) {  
                // 如果fg1为空,则创建一个并添加到界面上    这段代码是用来初始化创建fragment 
                fg1 = new Fragment1();  
                transaction.add(R.id.content, fg1);  
            } else {  
                // 如果MessageFragment不为空,则直接将它显示出来  
                transaction.show(fg1);  
            }  
            break;  

        case 1:
            found_image.setImageResource(R.drawable.ic_tabbar_found_pressed);  
            found_text.setTextColor(blue);
            
            if (fg2 == null) {  
                // 如果fg1为空,则创建一个并添加到界面上  
                fg2 = new Fragment2();  
                transaction.add(R.id.content, fg2);  
            } else {  
                // 如果MessageFragment不为空,则直接将它显示出来  
                transaction.show(fg2);  
            }  
            break;      
        
         case 2:
            settings_image.setImageResource(R.drawable.ic_tabbar_settings_pressed);  
            settings_text.setTextColor(blue);
            
            if (fg3 == null) {  
                // 如果fg1为空,则创建一个并添加到界面上  
                fg3 = new Fragment3();  
                transaction.add(R.id.content, fg3);  
            } else {  
                // 如果MessageFragment不为空,则直接将它显示出来  
                transaction.show(fg3);  
            }  
            break;                 
        }
        transaction.commit();//提交一个事务
    }
    
    //隐藏所有的Fragment,避免fragment混乱 如果没有隐藏的话  运行后结果会出先点击前一个选项无法正确显示相应的布局
    private void hideFragments(FragmentTransaction transaction) {  
        if (fg1 != null) {  
            transaction.hide(fg1);  
        }  
        if (fg2 != null) {  
            transaction.hide(fg2);  
        }  
        if (fg3 != null) {  
            transaction.hide(fg3);  
        }  
    }  
        
    
    //定义一个重置所有选项的方法
    public void clearChioce()
    {
        course_image.setImageResource(R.drawable.ic_tabbar_course_normal);
        course_layout.setBackgroundColor(whirt);
        course_text.setTextColor(gray);
        found_image.setImageResource(R.drawable.ic_tabbar_found_normal);
        found_layout.setBackgroundColor(whirt);
        found_text.setTextColor(gray);
        settings_image.setImageResource(R.drawable.ic_tabbar_settings_normal);
        settings_layout.setBackgroundColor(whirt);
        settings_text.setTextColor(gray);
    }

}
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg1, container,false);
        return view;
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。