android - Multiple layouts inside the single viewStub


Keywords:android 


Question: 

I have an item which has apple, pear and lemon. I made separate layouts for apples, pears and lemons.

I want to get the layout of that element if any element is selected. I tried that put the correct layout to viewStub according to selected layout with inflate() function. But the performance of defining layouts with inflate() each time was exhausting and sometimes caused incorrect work.

Please help me.

My general view codes:

    public class MyView extends RelativeLayout  {
    private ViewStub viewStub;
    private int lemon = 1, apple = 2, pear = 3;

    public MyView(Context context) {
        this(context, null);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        this.viewStub = ViewUtil.findById(this, R.id.view_stub);
    }

    public void bindElements(int type){
    if (type == lemon) {castLemon(); return;} 
    if (type == apple ) {castApple(); return;} 
    if (type == pear) {castPear(); return;} 

     public void castLemon() {

            viewStub.setLayoutResource(R.layout.lemon_item);
            LemonView lemonView = (LemonView) viewStub.inflate();
        }
     }

    public void castApple() {

            viewStub.setLayoutResource(R.layout.apple_item);
            AppleView appleView = (AppleView) viewStub.inflate();
        }
     }

    public void castPear() {

            viewStub.setLayoutResource(R.layout.pear_item);
            PearView pearView = (PearView) viewStub.inflate();
        }
     }
  }

My general view xml:

<com.android.android.MyView
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout android:id="@+id/bodyLinear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

                       <ViewStub
                        android:id="@+id/view_stub"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="start|center_vertical"
                        android:gravity="start|center_vertical"
                        android:paddingBottom="5dp"/>

            </LinearLayout>

</com.android.android.MyView>

LemonView.java:

public class LemonView extends FrameLayout {

    private ImageView imageView;

    public LemonView(Context context) {
        this(context, null);
    }

    public LemonView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LemonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.lemon_view, this);

        imageView = findViewById(R.id.image);
    }

lemon_view:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.android.android.LemonView">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:contentDescription="@null"
        android:gravity="center"
        android:scaleType="centerCrop"/>
</merge>

lemon_item xml:

<?xml version="1.0" encoding="utf-8"?>
<com.android.android.LemonView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lemon_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

1 Answer: 

Create all the layouts at once, then just show / hide the needed layout variant with .setVisibility(View.VISIBLE) / .setVisibility(View.GONE).