单个viewStub中的多个布局

时间:2018-02-22 15:11:00

标签: android performance layout viewstub

我有一个有苹果,梨和柠檬的食物。我为苹果,梨和柠檬做了单独的布局。

如果选择任何元素,我想获取该元素的布局。我尝试使用 var arr={ c1:{val: 9, x: 2, y: 0} c2:{val: 1, x: 3, y: 0} c3:{val: 6, x: 4, y: 0} } 函数根据所选布局将正确的布局放到viewStub。但是,每次使用inflate()定义布局的性能都很耗尽,有时会导致错误的工作。

请帮帮我。

我的常规视图代码:

inflate()

我的一般观点xml:

    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();
        }
     }
  }

LemonView.java:

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

lemon_view:

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_item xml:

<?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>

1 个答案:

答案 0 :(得分:1)

一次创建所有布局,然后使用.setVisibility(View.VISIBLE) / .setVisibility(View.GONE)显示/隐藏所需的布局变体。