我正在尝试创建一个带有片段的平板电脑应用程序。屏幕左侧将有四个按钮,屏幕右侧将根据单击的按钮而变化。我创建了主要活动和四个片段。每个片段都有自己的布局,带有几个 TextView 字段。当应用程序启动时,它将所有片段加载到 RAM - 这样它可以保持片段状态,以便当用户从一个片段切换到另一个片段时,所有文本字段都会保留其文本值,直到他单击最终提交按钮。该应用程序基于 SDK 4.1。该应用程序有点慢,尤其是在启动时。我想知道它是否设计得当,是否有一些方法可以改进它?
以下是主要活动类:
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();
}
});
}