Fragment
Fragment 的三个包:(互斥,A 类和 B 类需要使用相同的包。)
- android.app. Fragment
- android.app.v4.Fragment
- androidx.fragment.app.Fragment
Fragment 的关键类:
- FragmentManager。碎片化的管理。
- FragmentTransation。操作 Fragment,做一些具体的事务。
Fragment 的切换方式
- replace。替换,会重新走生命周期。
- show / hide。显示和隐藏。
示例一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| private LinearLayout ll_chat; private ChatFragment mChatFragment = null; private FragmentTransaction mChatTransaction = null;
private LinearLayout ll_me; private MeFragment mMeFragment = null; private FragmentTransaction mMeTransaction = null;
private void initFragment() { if (mChatFragment == null) { mChatFragment = new ChatFragment(); mChatTransaction = getSupportFragmentManager().beginTransaction(); mChatTransaction.add(R.id.mMainLayout, mChatFragment); mChatTransaction.commit(); }
if (mMeFragment == null) { mMeFragment = new MeFragment(); mMeTransaction = getSupportFragmentManager().beginTransaction(); mMeTransaction.add(R.id.mMainLayout, mMeFragment); mMeTransaction.commit(); } }
private void showFragment(Fragment fragment) { if (fragment != null) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); hideAllFragment(transaction); transaction.show(fragment); transaction.commitAllowingStateLoss(); } }
private void hideAllFragment(FragmentTransaction transaction) { if (mChatFragment != null) { transaction.hide(mChatFragment); } if (mMeFragment != null) { transaction.hide(mMeFragment); } }
@Override public void onClick(View view) { switch (view.getId()) { case R.id.ll_chat: checkMainTab(2); break; case R.id.ll_me: checkMainTab(3); break; } }
private void checkMainTab(int index) { switch (index) { case 2: showFragment(mChatFragment); break; case 3: showFragment(mMeFragment); break; } }
@Override public void onAttachFragment(Fragment fragment) { if (mChatFragment == null && fragment instanceof ChatFragment) { mChatFragment = (ChatFragment) fragment; } if (mMeFragment == null && fragment instanceof MeFragment) { mMeFragment = (MeFragment) fragment; } }
|
###示例二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| public class FragmentActivity extends AppCompatActivity{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); showFragment(0); }
int currIndex = -1; Fragment[] fragmentList = new Fragment[1];
public void showFragment(int index) { if (currIndex == index) return; FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); if (currIndex != -1) { ft.detach(fragmentList[currIndex]); } if (fragmentList[index] != null) { ft.attach(fragmentList[index]); } else { creatFragment(index); ft.replace(R.id.layout_fragment, fragmentList[index]); }
currIndex = index; ft.commit(); } public String[] str = {"ShareFragment"};
public void creatFragment(int index) { try { Fragment fragment = (Fragment) Class.forName("com.example.share." + str[index]).newInstance(); fragmentList[index] = fragment; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
|
示例(Kotlin)
生命周期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
class LifeCycleFragment :Fragment() {
companion object{ const val TAG = "TAG_LifeCycleFragment" }
override fun onAttach(context: Context) { super.onAttach(context) }
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) }
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return super.onCreateView(inflater, container, savedInstanceState) }
override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) }
override fun onStart() { super.onStart() }
override fun onResume() { super.onResume() }
override fun onPause() { super.onPause() }
override fun onStop() { super.onStop() }
override fun onDestroyView() { super.onDestroyView() }
override fun onDestroy() { super.onDestroy() }
override fun onDetach() { super.onDetach() }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
class FragmentActivity :BaseActivity(){
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_fragment)
btn.setOnClickListener{ replaceFragment(RightFragment()) }
replaceFragment(AnotherRightFragment()) }
private fun replaceFragment(fragment: Fragment) { val fragmentManager = supportFragmentManager val transaction = fragmentManager.beginTransaction() transaction.replace(R.id.rightLayout,fragment)
transaction.commit() }
companion object{ fun actionStart(context: Context){ val intent = Intent(context, FragmentActivity::class.java) context.startActivity(intent) } } }
|
res / layout / activity_fragment.xml
activity_fragment.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent">
<fragment android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/leftFrag" android:name="com.example.myapplication.fragment.LeftFragment"/>
<FrameLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/rightLayout" />
</LinearLayout>
|
res / layout-large / activity_fragment.xml
activity_fragment.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent">
<fragment android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/leftFrag" android:name="com.example.myapplication.fragment.LeftFragment"/>
<fragment android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:id="@+id/rightLayout" android:name="com.example.myapplication.fragment.RightFragment"/>
|
res / layout-sw600dp / activity_fragment.xml
activity_fragment.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent">
<fragment android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/leftFrag" android:name="com.example.myapplication.fragment.LeftFragment"/>
<fragment android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:id="@+id/rightLayout" android:name="com.example.myapplication.fragment.RightFragment"/>
</LinearLayout>
|
Fragment 切换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
class LeftFragment : Fragment(){
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.fragment_left,container,false) } }
|
fragment_left.xml1 2 3 4 5 6 7 8 9 10 11 12
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:text="button" android:layout_gravity="center_horizontal"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
class RightFragment : Fragment(){
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.fragment_right,container,false) } }
|
fragment_right.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="text" android:textSize="24sp" android:id="@+id/tvText" /> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
class AnotherRightFragment : Fragment(){
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.fragment_anotherright,container,false) } }
|
fragment_anotherright.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ffff">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="text" android:textSize="24sp" android:id="@+id/tvText" /> </LinearLayout>
|