您的位置:首页 > 移动开发 > Android开发

Android Fragment

2016-01-20 23:56 399 查看
package com.tz.fragmentreplace;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

// 2.2 FragmentActivity
public class MainActivity extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}


<?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" >

<fragment
android:id="@+id/title"
android:name="com.tz.fragmentreplace.MyListFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2" />

<FrameLayout
android:id="@+id/content1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>

</LinearLayout>
package com.tz.fragmentreplace;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.BackStackEntry;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;

public class MyListFragment extends ListFragment implements OnItemClickListener {
String[] days = new String[] { "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday" };
private FragmentManager fm;

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
this.setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, days));
this.getListView().setOnItemClickListener(this);
fm = getFragmentManager();
fm.addOnBackStackChangedListener(new OnBackStackChangedListener() {

public void onBackStackChanged() {
// TODO Auto-generated method stub
int backStackEntryCount = fm.getBackStackEntryCount();
for (int i = 0; i < backStackEntryCount; i++) {
BackStackEntry backStackEntryAt = fm.getBackStackEntryAt(i);
String name = backStackEntryAt.getName();
Log.i("INFO", name);
}
}
});
super.onActivityCreated(savedInstanceState);
}

public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// some item been clieked
showContent(position);
}

/**
* replace fragment
*
* @param position
*/
private void showContent(int position) {
// 新的fragment
MyContentFragment contentFragment = MyContentFragment
.getInstance(position);
// 旧的fragment
MyContentFragment mf = (MyContentFragment) fm
.findFragmentById(R.id.content1);

if (mf == null || mf.getMindex() != position) {
// 需要将新的fragment替换成旧的
// 开启事务
FragmentTransaction ft = fm.beginTransaction();//
ft.replace(R.id.content1, contentFragment);
ft.addToBackStack("fragment" + position);
// 提交
ft.commit();
}
}

}

package com.tz.fragmentreplace;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyContentFragment extends Fragment {
private int mIndex;

public int getMindex() {
return mIndex;
}

public void setMindex(int mIndex) {
this.mIndex = mIndex;
}

private String[] contents = new String[] { "today is Monday",
"today is Tuesday", "today is Wednesday", "today is Thursday",
"today is Friday", "today is Saturday", "today is Sunday" };

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
// 根据星期日期不同,显示不同的内容
Bundle bundle = this.getArguments();
int index = bundle.getInt("index");
this.mIndex = index;
String name = bundle.getString("name");
String content = contents[index];
TextView tv = new TextView(getActivity());
tv.setText(content);
tv.setGravity(Gravity.CENTER);
return tv;
}

public static MyContentFragment getInstance(int index) {
MyContentFragment contentFragment = new MyContentFragment();// 创建fragment
Bundle bundle = new Bundle();//
bundle.putInt("index", index);
bundle.putString("name", "坑总");
contentFragment.setArguments(bundle);// 设置参数进去
return contentFragment;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: