Android Fragment 简单实例
这里用的是import android.support.v4.app.FragmentTransaction
,而不是import android.app.FragmentTransaction
,后者会报错的。
Fragment
在 onCreateView
这里初始化控件
1 2 3 4 5 6 7 8 9 10
| public class MessageFragment extends Fragment{ private TextView tv; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View messageLayout = inflater.inflate(R.layout.message, container, false); tv=(TextView) messageLayout.findViewById(R.id.message); tv.setText("哈哈哈哈哈哈"); return messageLayout; } }
|
Activity
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| public class MainActivity extends Activity implements OnClickListener {
private MessageFragment messageFragment;
private ContactsFragment contactsFragment;
private NewsFragment newsFragment;
private SettingFragment settingFragment;
private View messageLayout;
private View contactsLayout;
private View newsLayout;
private View settingLayout;
private FragmentManager fragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); fragmentManager = getFragmentManager(); setTabSelection(0); }
private void initViews() { messageLayout = findViewById(R.id.message_layout); contactsLayout = findViewById(R.id.contacts_layout); newsLayout = findViewById(R.id.news_layout); settingLayout = findViewById(R.id.setting_layout); messageLayout.setOnClickListener(this); contactsLayout.setOnClickListener(this); newsLayout.setOnClickListener(this); settingLayout.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.message_layout: setTabSelection(0); break; case R.id.contacts_layout: setTabSelection(1); break; case R.id.news_layout: setTabSelection(2); break; case R.id.setting_layout: setTabSelection(3); break; default: break; } }
private void setTabSelection(int index) { clearSelection(); FragmentTransaction transaction = fragmentManager.beginTransaction(); hideFragments(transaction); switch (index) { case 0: messageLayout.setBackgroundColor(0xff0000ff); if (messageFragment == null) { messageFragment = new MessageFragment(); transaction.add(R.id.content, messageFragment); } else { transaction.show(messageFragment); } break; case 1: contactsLayout.setBackgroundColor(0xff0000ff); if (contactsFragment == null) { contactsFragment = new ContactsFragment(); transaction.add(R.id.content, contactsFragment); } else { transaction.show(contactsFragment); } break; case 2: newsLayout.setBackgroundColor(0xff0000ff); if (newsFragment == null) { newsFragment = new NewsFragment(); transaction.add(R.id.content, newsFragment); } else { transaction.show(newsFragment); } break; case 3: default: settingLayout.setBackgroundColor(0xff0000ff); if (settingFragment == null) { settingFragment = new SettingFragment(); transaction.add(R.id.content, settingFragment); } else { transaction.show(settingFragment); } break; } transaction.commit(); }
private void hideFragments(FragmentTransaction transaction) { if (messageFragment != null) { transaction.hide(messageFragment); } if (contactsFragment != null) { transaction.hide(contactsFragment); } if (newsFragment != null) { transaction.hide(newsFragment); } if (settingFragment != null) { transaction.hide(settingFragment); } }
private void clearSelection() { messageLayout.setBackgroundColor(0xffffffff); contactsLayout.setBackgroundColor(0xffffffff); newsLayout.setBackgroundColor(0xffffffff); settingLayout.setBackgroundColor(0xffffffff); } }
|
参考:https://www.jb51.net/article/138944.htm