简体   繁体   English

如何在Android的同一片段中实现关系视图?

[英]How to implement relational views in same fragment in Android?

I have 3 screen in my UI design. 我的UI设计中有3个屏幕。

Screen 1 : There will be a progress bar and information text in it. 屏幕1:其中将显示一个进度条和信息文本。

Screen 2 : If operation is success, this screen will open and success message will be shown. 屏幕2:如果操作成功,此屏幕将打开并显示成功消息。

Screen 3 : If operation is fail, this screen will open and fail message will be shown. 屏幕3:如果操作失败,此屏幕将打开,并显示失败消息。

These screens already have additional parts but I want to keep explanation simple. 这些屏幕已经包含其他部分,但我想保持简单的解释。 As you can see, actually they don't have any hard operation, only show some informational texts and components. 如您所见,实际上它们没有任何困难的操作,仅显示一些参考性文本和组件。

There are 3 way in my mind. 我有3种方法。

Approach 1 : Create 3 fragments for every single operation. 方法1:为每个操作创建3个片段。

Approach 2 : Create 1 fragment, create 1 layout, draw all views in the flow (progress, error, success) and manage them visible or gone. 方法2:创建1个片段,创建1个布局,绘制流程中的所有视图(进度,错误,成功),并对其进行可见或消失管理。

Approach 3 : Create 1 fragment, create 3 different layout, in every next step, call onCreateView and inflate fragment with different layout. 方法3:创建1个片段,创建3个不同的布局,在每个下一步中,调用onCreateView并为具有不同布局的片段充气。

Is there best approach for this kind of practices? 是否有针对这种做法的最佳方法?

I will chose the second Approach. 我将选择第二种方法。

In the first Approach I don't think is usual to manage 3 fragments to display just simple views, like TextView messages, you will complicate a simple task. 在第一种方法中,我认为管理3个片段以仅显示简单视图(如TextView消息)并不常见,您将使一个简单任务复杂化。

The third Approach is a good way too. 第三种方法也是一种好方法。

But I still think that is easier and simple you just make tha views visible or gone than every time inflate a new layout. 但我仍然认为,与每次充气一个新布局相比,使视图可见或消失都更容易和简单。

I chose for the second approach because is easier and clean to use. 我选择第二种方法是因为它更容易使用而且干净。 You only need to set a global variable 您只需要设置一个全局变量

private LinearLayout mMyView1
private LinearLayout mMyView2
private LinearLayout mMyView3

mMyView1 = (LinearLayout) findViewById(R.id.myid1);
mMyView2 = (LinearLayout) findViewById(R.id.myid2);

//than set visible or gone
mMyView1.setVisibility(View.GONE);
mMyView2.setVisibility(View.VISIBLE);

Actually, I use your second approach in my app GitHub . 实际上,我在我的应用程序GitHub中使用了第二种方法。 So I recommend. 所以我建议。 But keep in mind, if you want do more complex methods, need to review which approach is better. 但是请记住,如果您想使用更复杂的方法,则需要查看哪种方法更好。

EDIT 编辑

As @uguboz commented, you can use other ways show a simple message, like Dialogs or Snackbar 正如@uguboz所评论的那样,您可以使用其他方式显示简单消息,例如DialogsSnackbar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM