标签:
http://blog.csdn.net/harvic880925/article/details/44927375
http://blog.csdn.net/harvic880925/article/details/44948027
fragment1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff00f0" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is fragment 1" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>
fragment2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffff00" android:orientation="vertical" > <TextView android:id="@+id/fragment2_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is fragment 2" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>
Fragment1:
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false); } }
Fragment2:
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, container, false); } }
activity_main.xml
<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/btn_add_frag1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ADD Fragment1" /> <Button android:id="@+id/btn_add_frag2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ADD Fragment2" /> <Button android:id="@+id/btn_remove_frag2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Remove Fragment2" /> <Button android:id="@+id/btn_repalce_frag1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="replace Fragment1" /> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
import java.util.ArrayList; import java.util.List; import android.graphics.Color; import android.os.Bundle; import android.app.Activity; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout.DrawerListener; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; public class MainActivity extends FragmentActivity { /** DrawerLayout */ private DrawerLayout mDrawerLayout; /** 左边栏 */ private RelativeLayout leftLayout; /** 右边栏 */ private RelativeLayout rightLayout; /** 左边栏菜单 */ private ListView mMenuListView; /** 菜单列表 */ private String[] mMenuTitles = {"新闻","订阅","图片","视频","跟帖","投票"}; private List<ContentModel> list; private ContentAdapter adapter; private Button button1; private Button button2; private Button button3; private Button button4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.btn_add_frag1); button2 = (Button) findViewById(R.id.btn_add_frag2); button3 = (Button) findViewById(R.id.btn_remove_frag2); button4 = (Button) findViewById(R.id.btn_repalce_frag1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment1 fragment1 = new Fragment1(); addFragment(fragment1, "fragment1"); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment2 fragment2 = new Fragment2(); addFragment(fragment2, "fragment2"); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { removeFragment2(); } }); button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { replaceFragment1(); } }); /* mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout); leftLayout=(RelativeLayout) findViewById(R.id.left); rightLayout=(RelativeLayout) findViewById(R.id.right); mMenuListView =(ListView) leftLayout.findViewById(R.id.left_listview); initData(); adapter=new ContentAdapter(this, list); mMenuListView.setAdapter(adapter); mMenuListView.setOnItemClickListener(new DrawerItemClickListener());*/ } private void initData() { list=new ArrayList<ContentModel>(); list.add(new ContentModel(R.drawable.doctoradvice2, mMenuTitles[0])); list.add(new ContentModel(R.drawable.infusion_selected, mMenuTitles[1])); list.add(new ContentModel(R.drawable.mypatient_selected, mMenuTitles[2])); list.add(new ContentModel(R.drawable.mywork_selected, mMenuTitles[3])); list.add(new ContentModel(R.drawable.nursingcareplan2, mMenuTitles[4])); list.add(new ContentModel(R.drawable.personal_selected, mMenuTitles[5])); } /** * ListView上的Item点击事件 * */ private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position); } } /** * 切换主视图区域的Fragment * * @param position */ private void selectItem(int position) { // TODO Auto-generated method stub Fragment fragment = new ContentFragment(); Bundle args = new Bundle(); switch (position) { case 0: args.putString("key", mMenuTitles[position]); break; case 1: args.putString("key", mMenuTitles[position]); break; case 2: args.putString("key", mMenuTitles[position]); break; case 3: args.putString("key", mMenuTitles[position]); break; case 4: args.putString("key", mMenuTitles[position]); break; case 5: args.putString("key", mMenuTitles[position]); break; default: break; } fragment.setArguments(args); // FragmentActivity将点击的菜单列表标题传递给Fragment FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.fragment_layout, fragment).commit(); // 更新选择后的item和title,然后关闭菜单 mMenuListView.setItemChecked(position, true); setTitle(mMenuTitles[position]); mDrawerLayout.closeDrawer(mMenuListView); } private void addFragment(Fragment fragment, String tag) { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.add(R.id.fragment_container, fragment, tag); transaction.commit(); } private void removeFragment2() { FragmentManager manager = getSupportFragmentManager(); Fragment fragment = manager.findFragmentByTag("fragment2"); FragmentTransaction transaction = manager.beginTransaction(); transaction.remove(fragment); transaction.commit(); } private void replaceFragment1() { FragmentManager manager = getSupportFragmentManager(); Fragment2 fragment2 = new Fragment2(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.fragment_container, fragment2); transaction.commit(); } }
标签:
原文地址:http://www.cnblogs.com/qlky/p/5415679.html