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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章