两个简单实例,学会使用fragment
来源:互联网 发布:济南锋刃网络 编辑:程序博客网 时间:2024/06/10 08:27
实例一,简单Fragment
MainActivity
package com.example.demo;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTransaction;import android.view.View;public class MainActivity extends FragmentActivity {public static String temp="weesdf";public static String getTemp() {return temp;}public static void setTemp(String temp) {MainActivity.temp = temp;}public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//Intent intent=new Intent(MainActivity.this,);FragmentTransaction ft = getSupportFragmentManager().beginTransaction();FirstFragment first = new FirstFragment(); //如果用add()的话会出现不完全覆盖效果ft.replace(R.id.container, first);ft.commit();}//切换到第二个fragmentpublic void onClick_btn(View v){FragmentTransaction ft = getSupportFragmentManager().beginTransaction();SecondFragment second = new SecondFragment();ft.replace(R.id.container, second);ft.commit();}}
Activity_Second
package com.example.demo;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTransaction;public class Activity_Second extends FragmentActivity {public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);}}
FirstFragment
package com.example.demo;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;public class FirstFragment extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_first, container, false);Button bt_first = (Button) view.findViewById(R.id.bt_first);bt_first.setText(MainActivity.temp);bt_first.setOnClickListener(new OnClickListener() {public void onClick(View v) {Intent intent=new Intent(getActivity(),Activity_Second.class);startActivity(intent);}});return view;}}
SecondFragment
package com.example.demo;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentTransaction;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.View.OnClickListener;import android.widget.Button;public class SecondFragment extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_second, container, false);Button bt_second = (Button) view.findViewById(R.id.bt_second);bt_second.setOnClickListener(new OnClickListener() {public void onClick(View v) {FragmentTransaction ft = getFragmentManager().beginTransaction();ft.setCustomAnimations(R.animator.move_in, R.animator.move_out);FirstFragment first = new FirstFragment();ft.replace(R.id.container, first);ft.commit();}});return view;}}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick_btn" android:text="@string/app_name" /> <RelativeLayout android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="200dp" /></LinearLayout>fragmentfirst.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#cba" > <TextView android:id="@+id/tv_first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="这是第一个Fragment" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/bt_first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="点我进入下一个" /></LinearLayout>fragmentsecond.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#abc" android:orientation="vertical" > <TextView android:id="@+id/tv_second" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="这是第二个Fragment" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/bt_second" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="点我回到第一个" /></LinearLayout>
实例二, Fragment与activity通信
首先,如果你想在android3.0及以下版本使用fragment,你必须引用android-support-v4.jar这个包
然后你写的activity不能再继承自Activity类了,而是要继承android.support.v4.app.FragmentActivity,一些其他的父类也有相应的变化.
由于在android的实现机制中fragment和activity会被分别实例化为两个不相干的对象,他们之间的联系由activity的一个成员对象fragmentmanager来维护.fragment实例化后会到activity中的fragmentmanager去注册一下,这个动作封装在fragment对象的onAttach中,所以你可以在fragment中声明一些回调接口,当fragment调用onAttach时,将这些回调接口实例化,这样fragment就能调用各个activity的成员函数了,当然activity必须implements这些接口,否则会包classcasterror
fragment和activity的回调机制又是OOP的一次完美演绎!
下面通过一个例子来说明:
我把Activity的UI分为两个部分,左边和右边,左边用来放置点击的按钮(LeftFragment),右边用来放置对应点击后显示的信息(RightFragment).
Activity的布局layout文件:main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <LinearLayout android:id="@+id/left_layout" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/right_layout" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="10" android:orientation="vertical" > </LinearLayout> </LinearLayout>
LeftFragment的布局layout:leftfragment.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" > <Button android:id="@+id/first_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/first_button" /> <Button android:id="@+id/second_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/second_button" /> <Button android:id="@+id/third_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/third_button" /> </LinearLayout>
RightFragment的布局layout:rightfragment.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" > <TextView android:id="@+id/right_show_message" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/holo_orange_dark" android:textColor="@android:color/white" /> </LinearLayout>
public class FirstActivity extends Activity implements MyListener { /** * 实现MyListener,当LeftFragment中点击第一页的时候,让RightFragment显示第一页信息,同理当点击第二页的时候,RightFragment显示第二页信息 * * @param index * 显示的页数 */ public void showMessage(int index) { if (1 == index) showMessageView.setText(R.string.first_page); if (2 == index) showMessageView.setText(R.string.second_page); if (3 == index) showMessageView.setText(R.string.third_page); } /** 得到RightFragment中显示信息的控件 */ private TextView showMessageView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); System.out.println("Activity--->onCreate"); FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); // 动态增加Fragment RightFragment rightFragment = new RightFragment(); LeftFragment leftFragment = new LeftFragment(); transaction.add(R.id.left_layout, leftFragment, "leftfragment"); transaction.add(R.id.right_layout, rightFragment, "rightfragment"); transaction.commit(); } @Override protected void onResume() { super.onResume(); System.out.println("Activity--->onResume"); //注意:findview放到这里 showMessageView = (TextView) findViewById(R.id.right_show_message); } }
LeftFragment:
public class LeftFragment extends Fragment { /** Acitivity要实现这个接口,这样Fragment和Activity就可以共享事件触发的资源了 */ public interface MyListener { public void showMessage(int index); } private MyListener myListener; private Button firstButton; private Button secondButton; private Button thirdButton; /** Fragment第一次附属于Activity时调用,在onCreate之前调用 */ @Override public void onAttach(Activity activity) { super.onAttach(activity); System.out.println("LeftFragment--->onAttach"); myListener = (MyListener) activity; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.out.println("LeftFragment--->onCreate"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { System.out.println("LeftFragment--->onCreateView"); return inflater.inflate(R.layout.leftfragment, container, false); } @Override public void onResume() { super.onResume(); System.out.println("LeftFragment--->onResume"); firstButton = (Button) getActivity().findViewById(R.id.first_button); secondButton = (Button) getActivity().findViewById(R.id.second_button); thirdButton = (Button) getActivity().findViewById(R.id.third_button); MyButtonClickListener clickListener = new MyButtonClickListener(); firstButton.setOnClickListener(clickListener); secondButton.setOnClickListener(clickListener); thirdButton.setOnClickListener(clickListener); } /** 按钮的监听器 */ class MyButtonClickListener implements OnClickListener { public void onClick(View v) { Button button = (Button) v; if (button == firstButton) myListener.showMessage(1); if (button == secondButton) myListener.showMessage(2); if (button == thirdButton) myListener.showMessage(3); } } }
RightFragment
public class RightFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { System.out.println("RightFragment--->onCreate"); super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { System.out.println("RightFragment--->onCreateView"); return inflater.inflate(R.layout.rightfragment, container, false); } }
这里最关键的就是Fragment要有一个接口和这个接口的引用,而这个接口需要Activity去实现它。当Fragment调用onAttach(Activity acitivity)方法的时候,将这个activity传递给这个接口引用,这样,就可以和Activity进行交互了.
- 两个简单实例,学会使用fragment
- Fragment 简单实例详解
- Android Fragment 简单实例
- Fragment简单实例
- 两个Fragment简单跳转示例
- 简单学会.net remoting,基础讲解+实例
- Fragment的简单使用
- Android Fragment简单使用
- Fragment的简单使用
- Fragment简单使用
- Fragment的简单使用
- Fragment 的简单使用
- 简单的Fragment使用
- Fragment使用简单示例
- fragment简单使用
- fragment简单使用
- Fragment的简单使用
- Fragment的简单使用
- git常用命令
- Android高效加载大图、多图解决方案,有效避免程序OOM
- IOS常用布局展示框架优缺点
- Android-使用Theme来解决启动app是出现的空白屏
- hadoop上作业执行方法
- 两个简单实例,学会使用fragment
- HOOK Openprocess时间进程防查杀
- linux系统中推出ping命令
- brk(), sbrk() 用法详解
- 判断网页是否修改
- transform属性
- Python 提取两个列表的交集
- R语言数据框
- UIScrollView 实践经验