赵雅智_通过fragment对布局进行改变

项目需求

设置两个片段,昨天片段对右边进行颜色更改,片段替换的操作

实现效果

点击片段1:改变片段1的颜色值

点击片段2替换片段1

实现步骤

  • 新建主activity并在布局添加两个片段
  • 左片段
    • 对片段1进行颜色值改变的点击事件

      • 获取FragmentManager对象,只要获取FragmentManager对象就能获取fragment对象
      • 根据FragmentManager对象的findFragmentById方法来获取指定的fragment
      • 获取Fragment中的布局文件
      • 获取view中任何控件
      • 改变颜色背景值
    • 对片段2进行替换的点击事件
      • 获取FragmentManager对象
      • 获取fragment的事务操作 代表:activity对fragment执行的多个改变的操作
      • 执行替换
      • 提交事务
  • 右片段
    • 设置布局

项目源码

布局文件

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的布局动态文件

赵雅智_通过fragment对布局进行改变,布布扣,bubuko.com

时间: 2024-07-31 14:32:53

赵雅智_通过fragment对布局进行改变的相关文章

赵雅智_运用Bitmap和Canvas实现图片显示,缩小,旋转,水印

上一篇已经介绍了Android种Bitmap和Canvas的使用,下面我们来写一个具体实例 http://blog.csdn.net/zhaoyazhi2129/article/details/32136179 运行效果: 主要代码 package com.example.guaguale; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import and

赵雅智_使用SQLiteDatabase提供的增删改查方法及事务

知识点详解:http://blog.csdn.net/zhaoyazhi2129/article/details/9026093 MainActivity.java,User.java,BaseDao.java,UserDao.java同上篇 http://blog.csdn.net/zhaoyazhi2129/article/details/28640195 UserDaoImple.java package com.example.android_sqlite.dao.impl; impor

赵雅智_使用sqlite创建数据库

DatabaseHelper.java package com.example.android_sqlite.database; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelp

赵雅智_名片夹(2)_自动登录

功能介绍:刚安装应用的时候进行登录,当用户开启自动登录的时候,进入程序自动进入主界面 LoginAct.java package com.cards.activity; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import com.c

赵雅智_使用SQLiteDatabase操作SQLite数据库及事务

知识点详解:http://blog.csdn.net/zhaoyazhi2129/article/details/9025995 具体代码: MainActivity.java package com.example.android_sqlite; import android.app.Activity; import android.os.Bundle; import com.example.android_sqlite.dao.impl.UserDaoImpls; import com.ex

赵雅智_名片夹(6)_仿微信底导航栏

效果图如下 使用TabHost布局,并使用单选按钮组和FrameLayout相结合 布局文件代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_

赵雅智_android实例_当监听类有数据更新时下拉刷新

之前两篇文章分别介绍了OnScrollListener的实现和ContentProvider监听数据的变化,下面我们就结合者两个知识点实现一个小项目 项目需求 使用当ContentProvider监听类有数据更新时,在当前界面进行提示,并用OnScrollListener实现下拉刷新 实现效果 通过ContentProvider显示数据在界面 当监听类发生变化时 下拉刷新后显示数据 实现步骤 android_sqlite项目 定义操作标识 匹配结果码 继承ContentProvider类重写方法

赵雅智_Android案例_刮刮乐

实现效果 主要代码 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <I

赵雅智:android教学大纲

带下划线为具体内容链接地址,点击后可跳转,希望给大家尽一些微薄之力,目前还在整理中 教学章节 教学内容 学时安排 备注 1 Android快速入门 2 Android模拟器与常见命令 3 Android用户界面设计 4 Android网络通信及开源框架引用 5 线程与消息处理 6 数据存储及访问 7 Android基本单元应用activity 8 Android应用核心Intent 9 资源访问 10 ContentProvider实现数据共享 11 BroadcastReceiver 12 S