可重用视图?

Reusable views?

我是 Android 开发的新手,但我有相当多的 Swing/WPF(C#) GUI 经验。我想做的是以下内容:

我有一个 BottomNavigationView 有 3 个独立的视图。每个视图都作为片段实现,通过 OnNavigationItemSelectedListener:

在 MainActivity java 代码中显示/隐藏
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.one:
                findViewById(R.id.one).setVisibility(View.VISIBLE);
                findViewById(R.id.two).setVisibility(View.GONE);
                findViewById(R.id.threee).setVisibility(View.GONE);
                return true;
    // ... and so on

我的问题是两个片段的界面的一部分是相同的,由一组 EditTextImageButton 组成。所以我想创建一些可重用的东西而不是加倍代码。我的选择是在片段中制作片段(但在互联网上我看到这是不可取的)或制作可重复使用的视图 (https://developer.android.com/training/improving-layouts/reusing-layouts.html, https://developer.android.com/guide/topics/ui/custom-components.html)。最好的方法是什么?我想要一个带有连接 java 代码的 xml 布局,我可以在其中收听与 EditTextButtons 的交互并采取相应的行动。也许我对我的 WPF 经验有偏见,但我试图让它像 xaml + 代码隐藏,这可能不是构建 Android 应用程序时解决问题的正确方法。

您将必须为您将多次使用的视图定义一个单独的 XML 布局并且

在XML

您将在布局中使用 <include> 标签,例如

 ...
<include layout="@layout/your_frequent_used_layout"/>

<TextView android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/hello"
          android:padding="10dp" />
....

更多信息here in the documentation training!

在Java

对于 Java 侧 如果您想获得特定视图并添加到当前布局,您将需要一个布局 Inflater,然后像这样在 activity 中添加到您的 View (在你的片段中考虑上下文,如果它抛出任何错误)

第一个选项:

View view = null;
LayoutInflater inflater = 
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.your_reusable_view, null);
your_main_root_view.addView(view);

第二个选项:

View child = getLayoutInflater().inflate(R.layout.your_reusable_view, null);
your_main_root_view.addView(child);

使用 CustomView 是解决此问题的方法。您不仅可以在此使用相同的布局文件,还可以在您的自定义视图实现中定义与此视图的类似交互。

根据问题中的代码,您只是 showing/hiding 布局中的视图。
因此,首先我建议将 BottomNavigationViewViewPager 结合使用,并为每个导航项创建独立的片段。
本文演示如何开始
http://droidmentor.com/bottomnavigationview-with-viewpager-android/

其次,对于包含公共项的所有片段,您将创建一个抽象 BaseFragment 来管理所有公共逻辑。
扩展此抽象片段以创建您的个人片段并膨胀独特的布局。

fragment_base.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--- Your Common Elements Here --->

    <!--- Unique Elements will get inserted here --->

</LinearLayout>

fragment_nav_unique1.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--- Unique Elements For the Nav View Item --->

</LinearLayout>

BaseFragment.java

public abstract class BaseFragment extends Fragment {

    // Get Layout resource id of the Individual Fragments
    protected abstract int layoutResourceId();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // Inflate the base layout
        LinearLayout view = (LinearLayout) inflater.inflate(R.layout.fragment_base, container, false);

        // Inflate the unique layouts and add them to the view    
        LinearLayout uniqueView = (LinearLayout) inflater.inflate(layoutResourceId(), null);
        view.addView(uniqueView);

        // Return view
        return view;
    }

    //--- Manage all the Common Elements here ---

}

FirstFragment.java

public class FirstFragment extends BaseFragment {
    @Override
    protected int layoutResourceId() {
        // Return the resource of the unique layout
        return R.layout.fragment_nav_unique1;
    }

    //--- Manage all the Unique Elements here ---

}