以编程方式将视图添加到自定义布局时出现问题

时间:2019-04-26 11:50:29

标签: android

我已经实现了这样的自定义视图:

    <ir.myExample.myApp.classes.PurchaseBottomMenu
        android:id="@+id/purchaseBottomMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />

和我的PurchaseBottomMenu代码是这样的:

public class PurchaseBottomMenu extends FrameLayout {


  private int rotationAngle = 0;
    private View mMainLayout;
    private ViewGroup addView;
    private TextView mainTitle,mainPrice;

private void init() {
    mMainLayout = inflate(getContext(), R.layout.purchase_bottom_menu, this);
    addView = mMainLayout.findViewById(R.id.addView);

    mainTitle = mMainLayout.findViewById(R.id.mainTitle);
    mainPrice = mMainLayout.findViewById(R.id.mainPrice);

    FloatingActionButton fab = mMainLayout.findViewById(R.id.fab);
    arrowIcon = mMainLayout.findViewById(R.id.arrow);

    fab.setOnClickListener(v -> {
        rotationAngle = rotationAngle == 0 ? 180 : 0;
        arrowIcon.animate().rotation(rotationAngle).setDuration(500).start();
        addView.setVisibility(rotationAngle == 180 ? View.GONE : View.VISIBLE);
    });
}

private ImageView arrowIcon;
public PurchaseBottomMenu(Context context) {
    super(context);
    init();
}

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

public PurchaseBottomMenu(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public PurchaseBottomMenu(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
}

public void startAnimation(){
    new Handler().postDelayed(() -> {
        rotationAngle = 180;
        arrowIcon.animate().rotation(rotationAngle).setDuration(500).start();
        addView.setVisibility(View.GONE);
        invalidate();
        forceLayout();

    }, 2000);
}


 public void addItem(String title, int price){
        RelativeLayout relativeLayout = new RelativeLayout(getContext());
        relativeLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    relativeLayout.setPadding(0,3 * Utility.getDensity(getContext()),0,3 * Utility.getDensity(getContext()));

    RelativeLayout.LayoutParams titleViewParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    titleViewParams.addRule(RelativeLayout.ALIGN_PARENT_START);

    TextView titleView = new TextView(getContext());
    titleView.setText(title);
    relativeLayout.addView(titleView,titleViewParams);

    RelativeLayout.LayoutParams priceViewParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    priceViewParams.addRule(RelativeLayout.ALIGN_PARENT_END);

    TextView priceView = new TextView(getContext());
    priceView.setText(String.valueOf(price);

    relativeLayout.addView(priceView,priceViewParams);
    relativeLayout.invalidate();
    relativeLayout.requestLayout();

    addView.addView(relativeLayout);
    addView.invalidate();
    addView.requestLayout();

    invalidate();
    requestLayout();
}

}

如果我在init方法的末尾使用addItem和startAnimation方法,它将正常工作。但是,当我在外面购买PurchaseButtomMenu对象时使用它们时,它不会显示更改。我知道布局必须重新绘制并失效,正如您在这段代码中所看到的,我已经尝试过了,但是无论我尝试过什么,它似乎都没有效果,也没有任何变化。我很高兴有人帮忙...

0 个答案:

没有答案