我正在嘗試創建帶有片段的平板電腦應用程序。屏幕左側有四個按鈕,屏幕的右側部分將根據點擊按鈕而改變。 我已經創建了主要活動和四個片段。每個片段都有其自己的佈局以及多個TextView字段。當應用程序啓動時,它將所有片段加載到RAM中 - 這樣它可以保持片段狀態,以便當用戶從一個片段切換到另一個片段時,所有文本字段都保留其文本值,直到他單擊最後的提交按鈕。該應用程序基於SDK 4.1。該應用程序有點慢,特別是它啓動時。我想知道它是否設計得當,是否有改進它的方法?帶有幾個片段的Android應用程序設計
下面是主要的活動類:
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button buttonOne;
private Button buttonTwo;
private Button buttonThree;
private Button buttonFour;
private Fragment fragmentOne;
private Fragment fragmentTwo;
private Fragment fragmentThree;
private Fragment fragmentFour;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonOne = (Button) findViewById(R.id.button_one);
buttonTwo = (Button) findViewById(R.id.button_two);
buttonThree = (Button) findViewById(R.id.button_three);
buttonFour = (Button) findViewById(R.id.button_four);
fragmentOne = new FragmentOne();
fragmentTwo = new FragmentTwo();
fragmentThree = new FragmentThree();
fragmentFour = new FragmentFour();
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
fragmentTransaction.add(R.id.frameLayout_one, fragmentOne);
fragmentTransaction.add(R.id.frameLayout_one, fragmentTwo);
fragmentTransaction.add(R.id.frameLayout_one, fragmentThree);
fragmentTransaction.add(R.id.frameLayout_one, fragmentFour);
fragmentTransaction.commit();
buttonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
fragmentTransaction.show(fragmentOne);
fragmentTransaction.hide(fragmentTwo);
fragmentTransaction.hide(fragmentThree);
fragmentTransaction.hide(fragmentFour);
fragmentTransaction.commit();
}
});
buttonTwo.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
fragmentTransaction.hide(fragmentOne);
fragmentTransaction.show(fragmentTwo);
fragmentTransaction.hide(fragmentThree);
fragmentTransaction.hide(fragmentFour);
fragmentTransaction.commit();
}
});
buttonThree.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
fragmentTransaction.hide(fragmentOne);
fragmentTransaction.hide(fragmentTwo);
fragmentTransaction.show(fragmentThree);
fragmentTransaction.hide(fragmentFour);
fragmentTransaction.commit();
}
});
buttonFour.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
fragmentTransaction.hide(fragmentOne);
fragmentTransaction.hide(fragmentTwo);
fragmentTransaction.hide(fragmentThree);
fragmentTransaction.show(fragmentFour);
fragmentTransaction.commit();
}
});
}
謝謝JoshE。我會去嘗試一下。 –