Fragment小實例

        一個Fragment的問題搞了好幾天了,一直搞不明白是怎麼回事,主要是想在低版本2.2上跑android3.0的效果,找了好幾處例子,下載下來Demo就是打不到效果,不是這個出問題就是那個出問題,今天總算搞定了一個簡單的小應用了,從別人那下載下來編譯老出問題,最後還是修改成功了,不過還是沒有達到自己想要的效果,先記載下來吧。main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:orientation = "vertical"
    android:layout_width = "fill_parent"
    android:layout_height = "fill_parent"
    >
    
    <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/firstButton"
            android:layout_width = "wrap_content" 
            android:layout_height = "wrap_content"
            android:text = "Hide" 
            />
        <fragment 
        	android:name = "cn.fuli.damai.FirstFragment"
            android:id = "@+id/firstFragment" 
            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/secondButton"
            android:layout_width = "wrap_content" 
            android:layout_height = "wrap_content"
            android:text = "Hide"
            />
        <fragment 
        	android:name = "cn.fuli.damai.SecondFragment"
            android:id = "@+id/secondFragment" 
            android:layout_weight = "1"
            android:layout_width = "0px" 
            android:layout_height = "wrap_content" 
            />
    </LinearLayout>
    
</LinearLayout>

再定義Fragment的時候用到的一句關鍵的語句
android:name = "cn.fuli.damai.SecondFragment"  
android:name = "cn.fuli.damai.FirstFragment"
這兩句是加載了兩個Fragment  FragmentTestActivity繼承FragmentActivity,這個類中找到main.xml  對於Fragment提供的有FragmentManager對插入的Fragment進行管理。如果想進行一些增加、移除、替換的功能還需要使用getSupportFragmentManager().beginTransaction(),在進行上述相應操作的時候還可以加入動畫的ft.setCustomAnimations,這裏使用了系統提供的動畫。等操作完這些之後需要ft.commit()進行提交方能生效


public class FragmentTestActivity extends FragmentActivity {
@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		FragmentManager fm = getSupportFragmentManager();
		addListener(R.id.firstButton, fm.findFragmentById(R.id.firstFragment));
		addListener(R.id.secondButton, fm.findFragmentById(R.id.secondFragment));
	}
	
	private void addListener(int buttonID, final Fragment fragment) {
         final Button button = (Button)findViewById(buttonID);
         button.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                 ft.setCustomAnimations(android.R.anim.fade_in,
                         android.R.anim.fade_out);
                 if (fragment.isHidden()) {
                     ft.show(fragment);
                     button.setText("隱藏");
                 } else {
                     ft.hide(fragment);
                     button.setText("顯示");
                 }
                 ft.commit();
             }
         });
    }
	

}

最後的就是有關Fragment的兩個類了
public class FirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.first_fragment, container, false);
        TextView tv = (TextView)v.findViewById(R.id.msg);
        tv.setText("This is first fragment.");
        tv.setBackgroundColor(Color.GREEN);
        return v;
    }
}

public class SecondFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.second_fragment, container, false);
        TextView tv = (TextView)v.findViewById(R.id.msg);
        tv.setText("This is second fragment.");
        tv.setBackgroundColor(Color.RED);
        return v;
    }
}

兩個Fragment類使用的到的.xml就是一些簡單的定義了 first_fragment.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" 
        />
</LinearLayout>
second_fragment.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"
  	>
  	
    <TextView 
    	android:id = "@+id/msg"
        android:layout_width = "match_parent" 
        android:layout_height = "wrap_content"
        android:layout_weight = "0"
        android:paddingBottom = "4dip" 
        />
  	
</LinearLayout>

Demo下載地址


發佈了32 篇原創文章 · 獲贊 3 · 訪問量 55萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章