項目中遇到一個尷尬的問題,由於初學Android,因此用了大量Activity,但如今想要改爲Fragment,可是Activity太多,感受很頭大,研究了好久怎麼改,沒有看到合適的文章,但好在最後仍是成功了android
這裏舉一個例子,省得忘記作法了:緩存
那麼就隨意貼一個須要修改的Activity:併發
public class SetActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_set); Resources resourse = this.getResources(); String[] data = resourse.getStringArray(R.array.set); //android.R.layout.simple_list_item_1這是Android內置的佈局文件,裏面只有一個TextView,可用於簡單顯示一段文本 ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, data ); ListView listview_set = findViewById(R.id.listview_set); listview_set.setAdapter(adapter); UsersInfo user = UsersInfo.getCurrentUser(UsersInfo.class); //設置item監聽事件 listview_set.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position){ case 0://修改密碼 Intent intent_pwdchange = new Intent(SetActivity.this, PwdchangeActivity.class); startActivity(intent_pwdchange); break; case 1://修改註冊郵箱併發送驗證 Intent intent_changeemail = new Intent(SetActivity.this, ChangeemailActivity.class); startActivity(intent_changeemail); break; case 2://修改手機號碼 Intent intent_changephone = new Intent(SetActivity.this, ChangephoneActivity.class); startActivity(intent_changephone); break; case 3://退出登陸,返回到登陸界面 UsersInfo.logOut();//退出登陸,同時清除緩存用戶對象。 Intent intent_logout = new Intent(SetActivity.this, LoginActivity.class); startActivity(intent_logout); break; case 4: Intent intent_verifyphone = new Intent(SetActivity.this, VerifyphoneActivity.class); startActivity(intent_verifyphone); break; } } }); } }
下面是對應的activity_set.xml文件app
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".set.SetActivity"> <ListView android:id="@+id/listview_set" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout>
複製一份activity_set.xml,仍是放在layout文件裏,改名爲fragment_set.xmlide
接下來新建一個fragment,名爲SetFragment佈局
把activity裏的邏輯放到frgment裏面去,注意這裏很關鍵this
View view = View.inflate(getActivity(),R.layout.fragment_set,null);
其實沒有什麼改變的,就是把本來activity中onCreate()中的邏輯放到了Fragment的onCreateView()裏面去,
一、佈局是經過inflate來的,
二、把this替換爲getActivity()
三、findViewById->view.findViewById
public class SetFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = View.inflate(getActivity(),R.layout.fragment_set,null); Resources resourse = getActivity().getResources(); String[] data = resourse.getStringArray(R.array.set); //android.R.layout.simple_list_item_1這是Android內置的佈局文件,裏面只有一個TextView,可用於簡單顯示一段文本 ArrayAdapter<String> adapter = new ArrayAdapter<String>( getActivity(), android.R.layout.simple_list_item_1, data ); ListView listview_set = view.findViewById(R.id.listview_set); listview_set.setAdapter(adapter); UsersInfo user = UsersInfo.getCurrentUser(UsersInfo.class); //設置item監聽事件 listview_set.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position){ case 0://修改密碼 Toast.makeText(getActivity(), "修改密碼!", Toast.LENGTH_SHORT).show(); // Intent intent_pwdchange = new Intent(SetActivity.this, PwdchangeActivity.class); // startActivity(intent_pwdchange); break; case 1://修改註冊郵箱併發送驗證 Toast.makeText(getActivity(), "點擊1", Toast.LENGTH_SHORT).show(); // Intent intent_changeemail = new Intent(SetActivity.this, ChangeemailActivity.class); // startActivity(intent_changeemail); break; case 2://修改手機號碼 Toast.makeText(getActivity(), "點擊2", Toast.LENGTH_SHORT).show(); // Intent intent_changephone = new Intent(SetActivity.this, ChangephoneActivity.class); // startActivity(intent_changephone); break; case 3://退出登陸,返回到登陸界面 Toast.makeText(getActivity(), "點擊3", Toast.LENGTH_SHORT).show(); // UsersInfo.logOut();//退出登陸,同時清除緩存用戶對象。 // Intent intent_logout = new Intent(SetActivity.this, LoginActivity.class); // startActivity(intent_logout); break; case 4: Toast.makeText(getActivity(), "點擊4", Toast.LENGTH_SHORT).show(); // Intent intent_verifyphone = new Intent(SetActivity.this, VerifyphoneActivity.class); // startActivity(intent_verifyphone); break; } } }); // Inflate the layout for this fragment return view; } }
最後在須要添加fragment的主activity中:spa
getSupportFragmentManager().beginTransaction().add(R.id.main_body,new SetFragment()).commit();
由於我是想要一個底部導航欄的效果,那我這裏直接用了android studio中寫好的一個底部導航欄,而後稍微改了一點兒:

嗯仍是貼一下代碼吧,MainActivity:code
public class MainActivity extends AppCompatActivity { private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.navigation_home: //getSupportFragmentManager().beginTransaction().replace(R.id.main_body,new HomeFragment()).commit(); return true; case R.id.navigation_dashboard: getSupportFragmentManager().beginTransaction().replace(R.id.main_body,new SetFragment()).commit(); return true; case R.id.navigation_notifications: return true; } return false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setMain(); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); } //用於打開初始頁面 private void setMain() { //getSupportFragmentManager() -> beginTransaction() -> add -> (R.id.main_boy, new Fragment() this.getSupportFragmentManager().beginTransaction().add(R.id.main_body,new HomeFragment()).commit(); } }
下面是activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:orientation="vertical" android:background="@android:color/white" android:layout_width="match_parent" android:layout_height="match_parent"> <!--標題欄--> <!--<include layout="@layout/main_title_bar" />--> <!--放置Fragment的main_body--> <RelativeLayout android:id="@+id/main_body" android:background="@android:color/white" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> </RelativeLayout> </LinearLayout> <android.support.design.widget.BottomNavigationView android:id="@+id/navigation" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" android:background="?android:attr/windowBackground" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/navigation" /> </android.support.constraint.ConstraintLayout>
嗯 大體就是這樣啦xml