Android Fragment 学习<一>
程序包含一个继承Activity的主类,另外两个继承Fragment类,并且两个Fragment共用一个xml layout文件
主类程序如下:
package com.example.androidfragmentd01test; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; @SuppressLint("NewApi") public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm=getFragmentManager(); addShowHideListener(R.id.frag1hide,fm.findFragmentById(R.id.fragment1)); addShowHideListener(R.id.frag2hide,fm.findFragmentById(R.id.fragment2)); } private void addShowHideListener(int button1, final Fragment findFragmentById) { // TODO Auto-generated method stub final Button button; button=(Button)findViewById(button1); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub FragmentTransaction ft=getFragmentManager().beginTransaction(); ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out); if(findFragmentById.isHidden()){ ft.show(findFragmentById); button.setText("Hide"); }else{ ft.hide(findFragmentById); button.setText("Show"); } ft.commit(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
主类的相关配置文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Demonstration of hiding and showing fragments." /> <LinearLayout android:orientation="horizontal" android:padding="4dip" android:gravity="center_vertical" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/frag1hide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hide" /> <fragment android:name="com.example.androidfragmentd01test.FirstFragment" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="0px" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:padding="4dip" android:gravity="center_vertical" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/frag2hide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hide" /> <fragment android:name="com.example.androidfragmentd01test.SecondFragment" android:id="@+id/fragment2" android:layout_weight="1" android:layout_width="0px" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
新建Fragmentone和Fragmenttwo:
Fragmentone如下:
package com.example.androidfragmentd01test; import android.annotation.SuppressLint; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; @SuppressLint("NewApi") public class FirstFragment extends Fragment{ private TextView mTextView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //从文件 example_fragment.xml 加载了一个layout View v = inflater.inflate(R.layout.text_edit, container, false); View tv = v.findViewById(R.id.msg); ((TextView)tv).setText("The fragment saves and restores this text."); mTextView = (TextView)v.findViewById(R.id.saved); if (savedInstanceState != null) { mTextView.setText(savedInstanceState.getCharSequence("text")); } return v; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putCharSequence("text", mTextView.getText()); } }
Fragmenttwo如下:
package com.example.androidfragmentd01test; import android.annotation.SuppressLint; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; @SuppressLint("NewApi") public class SecondFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //从文件 example_fragment.xml 加载了一个layout View v = inflater.inflate(R.layout.text_edit, container, false); View tv = v.findViewById(R.id.msg); ((TextView)tv).setText("The TextView saves and restores this text."); //另外一种TextView的保存模式 ((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true); return v; } }
xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/msg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:paddingBottom="4dip" /> <EditText android:id="@+id/saved" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/initial_text" android:freezesText="true"> <requestFocus /> </EditText> </LinearLayout>
欲行即可.
<Linker from : http://blog.csdn.net/flying_tao/article/details/6596518>

浙公网安备 33010602011771号