Fragment的使用(一)


我们先学会使用静态加载的方式用activity加载Fragment.


关于fragment,个人感觉不太好理解。个人比较脑残,花了近一周的时间才基本明白了一点难过。 

ps:我每天学习android的时间都是下班后地铁上看一章内容,回家再敲一下,理解一下 。


首先我们得创建一个fragment控件。

我们在main_activity的布局文件中创建一个fragment控件。这个控件我们必须给一个id,或者tag也可以,就是唯一标识一下

然后我们还需要一个name,这个name就是指向一个fragment。

<span style="font-family:Microsoft YaHei;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment"
        android:name="com.example.fragment.MyFragment"
        />
</LinearLayout></span>

然后我们看看在main_activity布局文件中name指向的那个fragment:

这里我们创建一个叫MyFragment的class,这个class继承自Fragment,然后我们重写onCreateView。

这里我们用inflater加载一个布局文件,就是fragment对应的布局文件。

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

public class MyFragment extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment, container, false);
		Button button =  (Button) view.findViewById(R.id.button);	
		button.setText("静态加载按钮");
		return view;
	}
}



接下来我们看看Fragment对应的布局文件:

里面就包含了一个TextView和一个Button,在上面的Fragment中,我们就给button设置了显示的文本,值得注意的是获取控件的时候使用inflater加载的view对象来获取控件,直接findViewByID会报错的。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>


最后我们再来看看MainActivity:

这里我们可以发现,在MainActivity里,我们直接findViewBYid就可以获取Fragment里面的控件,我们可以理解成fragment就是MainActivity的一部分了。。

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	//  定义fragment里面的控件
	private TextView text;
	private Button button;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 初始化fragment中的控件
		text = (TextView) findViewById(R.id.text);
		button = (Button) findViewById(R.id.button);
		// 给button设置点击事件   操作fragment中的控件
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				text.setText("MainActivity静态加载并操作fragment中的控件");
			}
		});
	}
}



这样简单的静态加载fragment就实现了 。


Fragment不太好理解,唯一的理解办法就是动手敲一敲,看一下效果,再思考一下,应该就能有点收货了。总结了一下就是:

我们可以这样考虑:如果Activity是一个房子,那么Fragment就是一个房间,我们就是一个房子加载了一个房间而已 。




ps:今天还是配置了eclipse的android开发环境,还是习惯eclipse,AndroidStudio还是不太喜欢用,虽然很好看,也很强大,但是习惯了eclipse的快捷键,操作方式。微笑
















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值