Android 开发Fragment类似微信主页切换

Fragment(碎片)

Fragment可以实现在一个Activity下显示多个视图,类似于微信首页底部栏切换。

一、在需要用到Fragment视图中的Activity

public class NewsActivity extends FragmentActivity implements OnClickListener {
	

    // 底部菜单3个Linearlayout
    private LinearLayout message;
    private LinearLayout fair;
    private LinearLayout question;

    // 3个Fragment
    private Fragment first_f;
    private Fragment two_f;
    private Fragment three_f;
    

    private FragmentManager manager;
    private FragmentTransaction transaction;
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news);
		
		 initView();       // 初始化控件

        
                 initEvent();// 初始化按钮事件

                 // 初始化并设置当前Fragment
                initFragment(0);
    }
	

    private void initFragment(int index) {
        // 由于是引用了V4包下的Fragment,所以这里的管理器要用getSupportFragmentManager获取
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 隐藏所有Fragment
        hideFragment(transaction);
        switch (index) {
        case 0:
            if (first_f == null) {
                first_f = new NewsOne();      //NewsOne是fragment视图1
                transaction.add(R.id.fl_content, first_f);
            } else {
                transaction.show(first_f);
            }
            break;
        case 1:
            if (two_f == null) {
            	two_f = new NewsTwo();         //NewsTwo是fragment视图2
                transaction.add(R.id.fl_content, two_f);
            } else {
                transaction.show(two_f);
            }

            break;
        case 2:
            if (three_f == null) {
            	three_f = new NewsThree();         //Newsthree是fragment视图3
                transaction.add(R.id.fl_content, three_f);
            } else {
                transaction.show(three_f);
            }

            break;
   
        default:
            break;
        }
        // 提交事务
        transaction.commit();

    }

    //隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (first_f != null) {
            transaction.hide(first_f);
        }
        if (two_f != null) {
            transaction.hide(two_f);
        }
        if (three_f != null) {
            transaction.hide(three_f);
        }

    }

    private void initEvent() {
        // 设置按钮监听
        message.setOnClickListener(this);
        fair.setOnClickListener(this);
        question.setOnClickListener(this);

    }

    private void initView() {

        // 底部菜单4个ImageView
        this.message = (LinearLayout) findViewById(R.id.message);
        this.fair = (LinearLayout) findViewById(R.id.fair);
        this.question = (LinearLayout) findViewById(R.id.question);

    }

    @Override
    public void onClick(View v) {

        // 在每次点击后将所有的底部按钮(ImageView,TextView)颜色改为灰色,然后根据点击着色
       restartButton();
        // ImageView和TetxView置为绿色,页面随之跳转
        switch (v.getId()) {
        case R.id.message:
            message.setBackgroundResource(R.drawable.msg_after_bg);
            initFragment(0);
            break;
        case R.id.fair:
        	fair.setBackgroundResource(R.drawable.msg_after_bg);
            initFragment(1);
            break;
        case R.id.question:
        	question.setBackgroundResource(R.drawable.msg_after_bg);
            initFragment(2);
            break;
        default:
            break;
        }

    }

    private void restartButton() {
        // ImageView置为灰色
        message.setBackgroundResource(R.drawable.msg_before_bg);
        fair.setBackgroundResource(R.drawable.msg_before_bg);
        question.setBackgroundResource(R.drawable.msg_before_bg);

    }


}




二、对应的Fragment视图NewsOne、NewsTwo、NewsThree

在对应的Fragment中:

    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_news_three, container, false);
    	return view;
        
    }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值