Android初级学习笔记2-fragment 入门


        在Android 3.0之后,sdk已经开始支持fragment 。Framgment 是一个轻量级的activity ,不需要在清单文件中配置。

        基本用法:

一.  静态使用fragment:

1、继承Fragment,重写onCreateView决定Fragemnt的布局

2、在Activity中声明此Fragment,就当和普通的View一样


    示例:

package com.zhy.zhy_fragments;

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

public class ContentFragment extends Fragment
{

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

}


好处:把Fragment当成普通的View一样声明在Activity的布局文件中,然后所有控件的事件处理等代码都由各自的Fragment去处理。


二. 动态使用fragment

        直接看代码 

package com.zhy.zhy_fragments;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnClickListener
{
	private LinearLayout mTabWeixin;
	private LinearLayout mTabFriend;

	private ContentFragment mWeixin;
	private FriendFragment mFriend;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		// 初始化控件和声明事件
		mTabWeixin = (LinearLayout) findViewById(R.id.tab_bottom_weixin);
		mTabFriend = (LinearLayout) findViewById(R.id.tab_bottom_friend);
		mTabWeixin.setOnClickListener(this);
		mTabFriend.setOnClickListener(this);

		// 设置默认的Fragment
		setDefaultFragment();
	}

	private void setDefaultFragment()
	{
		FragmentManager fm = getFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		mWeixin = new ContentFragment();
		transaction.replace(R.id.id_content, mWeixin);
		transaction.commit();
	}

	@Override
	public void onClick(View v)
	{
		FragmentManager fm = getFragmentManager();
		// 开启Fragment事务
		FragmentTransaction transaction = fm.beginTransaction();

		switch (v.getId())
		{
		case R.id.tab_bottom_weixin:
			if (mWeixin == null)
			{
				mWeixin = new ContentFragment();
			}
			// 使用当前Fragment的布局替代id_content的控件
			transaction.replace(R.id.id_content, mWeixin);
			break;
		case R.id.tab_bottom_friend:
			if (mFriend == null)
			{
				mFriend = new FriendFragment();
			}
			transaction.replace(R.id.id_content, mFriend);
			break;
		}
		// transaction.addToBackStack();
		// 事务提交
		transaction.commit();
	}

}


        参考: http://blog.csdn.net/lmj623565791/article/details/37970961


三、 fragment之间通信

fragment之间的通信主要就是以MainActivtiy作为桥梁得到fragment,然后再fragment中进行对点击事件的操作。 

public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                FragmentManager fm = getFragmentManager();
                Fragment1 fragment1 = (Fragment1) fm.findFragmentById(R.id.fragment1);
        }
}


public class Fragment1 extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.fragment1, null);
                Button button = (Button) view.findViewById(R.id.button1);
                button.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                Fragment2 fragment2 = (Fragment2) getActivity()
                                                .getFragmentManager().findFragmentById(R.id.fragment2);
                                fragment2.setText("内容改变啦...");
                        }
                });
                return view;
        }
}
public class Fragment2 extends Fragment {
        private TextView textview;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.fragment2, null);
                textview = (TextView) view.findViewById(R.id.textView1);
                return view;
        }
        public void setText(String text) {
                textview.setText(text);
        }
}


activity 和 fragment 之间通信 ,请看 : http://www.shangxueba.com/jingyan/1846234.html



转载于:https://my.oschina.net/u/2470495/blog/646104

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值