项目需求
设置两个片段,昨天片段对右边进行颜色更改,片段替换的操作
实现效果
点击片段1:改变片段1的颜色值
点击片段2替换片段1
实现步骤
- 新建主activity并在布局添加两个片段
- 左片段
- 对片段1进行颜色值改变的点击事件
- 获取FragmentManager对象,只要获取FragmentManager对象就能获取fragment对象
- 根据FragmentManager对象的findFragmentById方法来获取指定的fragment
- 获取Fragment中的布局文件
- 获取view中任何控件
- 改变颜色背景值
- 获取FragmentManager对象,只要获取FragmentManager对象就能获取fragment对象
- 对片段2进行替换的点击事件
- 获取FragmentManager对象
- 获取fragment的事务操作 代表:activity对fragment执行的多个改变的操作
- 执行替换
- 提交事务
- 获取FragmentManager对象
- 对片段1进行颜色值改变的点击事件
- 右片段
- 设置布局
项目源码
布局文件
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.android_fragment.other.MyFragment1"
android:layout_width="100dp"
android:layout_height="match_parent"
/>
<fragment
android:id="@+id/fragment2"
android:name="com.example.android_fragment.other.MyFragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/fragment1"
/>
</RelativeLayout>
fragment_list_item1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:background="#DCB5FF">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="片段1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="片段2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="片段3" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="片段4" />
</LinearLayout>
fragment_list_item2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFBFFF"
android:orientation="vertical"
android:paddingTop="10dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView" />
</LinearLayout>
fragment_list_replace.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFE66F"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="10dp"
android:text="替换的textview" />
</LinearLayout>
MainActivity.java
package com.example.android_fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
/**
* 主activity,包含2个片段
* @author zhaoyazhi
*
* 2014-6-13
*/
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_main);
}
}
MyFragment1.java
package com.example.android_fragment.other;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.android_fragment.R;
/**
* 左边片段
* @author zhaoyazhi
*
* 2014-6-13
*/
public class MyFragment1 extends Fragment {
private FragmentActivity activity;
/**
* 把activity造型为FragmentActivity
*/
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
this.activity = (FragmentActivity) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//设置布局
View view = inflater.inflate(R.layout.fragment_list_item1, container,
false);
//查找控件并设置点击事件
TextView tv1 = (TextView) view.findViewById(R.id.textView1);
tv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//改变背景值
changeFragmentColor();
}
});
//查找控件并设置点击事件
TextView tv2 = (TextView) view.findViewById(R.id.textView2);
tv2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//改变整个Fragment
changeFragment();
}
});
return view;
}
//改变整个Fragment
private void changeFragment() {
// 1.获取FragmentManager对象
FragmentManager manager = getActivity()
.getSupportFragmentManager();
// 2.获取fragment的事务操作 代表:activity对fragment执行的多个改变的操作
FragmentTransaction transaction = manager.beginTransaction();
// 添加替换或删除Fragment这时候就需要FragmentTransaction的布局动态文件
// 执行替换
//参数1:父元素的id值,参数2:替换新fragment对象
transaction.replace(R.id.fragment2, new MyFragment3());
// 3.提交事务
transaction.commit();
}
//改变控件的颜色
private void changeFragmentColor() {
// 1.获取FragmentManager对象,只要获取FragmentManager对象就能获取fragment对象
FragmentManager manager = getActivity()
.getSupportFragmentManager();
// 2.根据FragmentManager对象的findFragmentById方法来获取指定的fragment
Fragment fragment2 = manager.findFragmentById(R.id.fragment2);
// 3.获取Fragment中的布局文件
View v = fragment2.getView();
// 4.获取view中任何控件
LinearLayout layout = (LinearLayout) v
.findViewById(R.id.ll_item);
// 5.改变颜色背景值
layout.setBackgroundColor(Color.YELLOW);
}
}
MyFragment2.java
package com.example.android_fragment.other;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.android_fragment.R;
/**
* 右边片段
* @author zhaoyazhi
*
* 2014-6-13
*/
public class MyFragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_list_item2, container, false);
return v;
}
}
MyFragment3.java
package com.example.android_fragment.other;
import com.example.android_fragment.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* 全部替换的右边片段
* @author zhaoyazhi
*
* 2014-6-13
*/
public class MyFragment3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_list_replace, container, false);
}
}
知识点解析
为了兼容android1.6,MainActivity继承于FrameActivityFragment
片段类引用android.support.v4.app.*;包中的组件
实现片段要继承Fragment类,实现onCreateView方法
inflater.inflate(R.layout.layout1,container,false);
- 第一个参数把某一布局文件转换成view对象;
- 第二参数是把这个view对象放入container容器中;这个container容器其实就是activity_main的根节点,可以通过在根节点下设置tag属性,并通过container.getTag()获取值,再将其打印出来。
- 第三个参数代表是否把这个view对象添加到container容器内部,在xml中我们已经将它添加到容器内部。经测试,如果此时你再将之设置为true,其依然能够正确执行。
fragment中name属性
name值是片段所对应的fragment类
FragmentTransaction
获取fragment的事务操作 代表:activity对fragment执行的多个改变的操作
添加替换或删除Fragment这时候就需要FragmentTransaction的布局动态文件