Skip to content Skip to sidebar Skip to footer

Reusable Views?

I'm new to Android development, but I have quite some bit of Swing/WPF(C#) GUI experience. What I'm trying to do is the following: I have a BottomNavigationView with 3 separate vie

Solution 1:

You will have to define a separate XML layout for a View that you will use more than once and

In XML You will use the <include> Tag inside your layout like

 ...
<includelayout="@layout/your_frequent_used_layout"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/hello"android:padding="10dp" />
....

More information here in the documentation training!

In Java For Java side If you want to get a certain view and add to your current layout you will need a layout Inflater and then add to your View like this in your activity (In your fragment consider contexts if it throws any errors)

1st Option:

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

2nd Option:

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

Solution 2:

As per your code in the question, you are just showing/hiding the views present in the layout. So, firstly I would suggest using BottomNavigationView in conjunction with a ViewPager and creating independent Fragments for every navigation item. This article demonstrates how to get started http://droidmentor.com/bottomnavigationview-with-viewpager-android/

Secondly, for all the fragments that contain common items, you will create an abstract BaseFragment that will manage all the common logic. Extend this abstract fragment to create your individual fragments and Inflate the unique layouts.

fragment_base.xml

<LinearLayoutxmlns: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

<LinearLayoutxmlns: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

publicabstractclassBaseFragmentextendsFragment {

    // Get Layout resource id of the Individual FragmentsprotectedabstractintlayoutResourceId();

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

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

        // Return viewreturn view;
    }

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

}

FirstFragment.java

publicclassFirstFragmentextendsBaseFragment {
    @OverrideprotectedintlayoutResourceId() {
        // Return the resource of the unique layoutreturn R.layout.fragment_nav_unique1;
    }

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

}

Solution 3:

Using CustomView is the way to go for this. Not only you can use the same layout file in this but can also define similar interactions with this view in your custom view implementation.

Post a Comment for "Reusable Views?"