413
技术社区[云栖]
Activity与在Android的碎片Fragment
Android的API 3.0版本,被称为蜂窝,引入了新的概念,叫做“记忆碎片”。什么是片断,为什么你想使用一个?请继续阅读。在Android中,配置的变化(包括将手机侧向转动,以查看屏幕在横向)导致当前正在运行的活动停止,重载,并使用新的配置参数重新呈现。(我敢肯定,iOS开发者将指出这个“功能”中缺少自己的堆栈)。在这样做时,您的应用程序将失去所有的状态信息从最初的活动实例。那岂不是更好,让解决?输入片段。片段是,有能力之间保持状态的配置更改的UI组件(虽然它可能有非UI片段)。片段必须始终与活动相关联,但一个简短的电话setRetainInstance(真),与活动相关联的片段重新连接到新的实例的活动,并保留其原来的状态。 为了证明片段呆在身边,我们将创建两个独立的片段,每一个选择时,构建一个随机ID。然后,我们将两个片段连接到一个活动。第一个片段最初看到的第二个片段会被隐藏。当你点击一个按钮上的第一个片段,它会显示你的第二,反之亦然。然后,您可以检查,以确保原片段真的是住周围的ID,这是一个有用的技术,可以使用较小的设备上显示UI在一块。在大型设备,你可以定义一个布局一次显示两个片段的。片段允许你构建你 的UI一系列较小的,可重复使用的图形元素,你安排需要根据设备的能力,让我们的开始通过创建一个FirstFragment类及SecondFragment类。我假设读者熟悉与处理onClick事件,所以不会打扰他解释说。因此,它是你正在寻找在哪个片段,每个片段都会有一个id。
public class FirstFragment extends Fragment {
public static final String TAG = "FirstFragmentTag";
int key;
public FirstFragment() {
this.setRetainInstance(true);
Random random = new Random();
key = random.nextInt();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i(TAG, "onCreateView");
View view = inflater.inflate(R.layout.first_fragment_layout, container, false);
TextView textView = (TextView) view.findViewById(R.id.firstFragmentTextView);
textView.setText(textView.getText() + "" + key);
Button button = (Button) view.findViewById(R.id.goToSecondFragmentButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SecondFragment secondFragment = (SecondFragment) FirstFragment.this.getActivity()
.getSupportFragmentManager().findFragmentById(R.id.secondFragment);
secondFragment.getView().setVisibility(View.VISIBLE);
FirstFragment.this.getView().setVisibility(View.GONE);
}
});
return view;
}
}
ublic class SecondFragment extends Fragment {
protected static final String TAG = "SecondFragmentTag";
int key;
public SecondFragment() {
this.setRetainInstance(true);
Random random = new Random();
key = random.nextInt();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i(TAG, "onCreateView");
View view = inflater.inflate(R.layout.second_fragment_layout, container, false);
TextView textView = (TextView) view.findViewById(R.id.secondFragmentTextView);
textView.setText(textView.getText() + "" + key);
Button button = (Button) view.findViewById(R.id.goToFirstFragmentButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FirstFragment firstFragment = (FirstFragment) SecondFragment.this.getActivity()
.getSupportFragmentManager().findFragmentById(R.id.firstFragment);
firstFragment.getView().setVisibility(View.VISIBLE);
SecondFragment.this.getView().setVisibility(View.GONE);
}
});
return view;
}
每个片段的布局将包括一个TextView(显示的ID),一个ImageView(显示不同的图形),和一个按钮(导航到另一个片段)。主要活动布局将包括的LinearLayout,包含的FirstFragment和SecondFragment的。FirstFragment将其可见性设置为“可见”,SecondFragment将有它的可见性设置为“水涨船高”。在这里签出文件 。
FirstFragment布局和SecondFragment布局定义每个片段将如何看。
为了证明这些碎片保持其状态,运行的程序,然后把你的手机,把它变成横向模式。检查看看的ID保持不变,为每一个片段后,切换到横向。他们应该的。然后取出setRetainInstance()调用,然后重新运行该程序。你会发现,IDS改变每次你改变手机的方向。
片段是目前被认为是最好的做法在Android UI设计,很容易明白为什么,因为他们提供了很大的灵活性。
最后更新:2017-04-02 15:15:00