web-dev-qa-db-fra.com

Supprimer / ajouter une contrainte par programme dans ConstraintLayout

Je veux supprimer et ajouter une contrainte par programme basée sur une condition. Voici les captures d'écran:

The button "create ad" has a top constraint

et je veux l'enlever comme ça mais en code:

here is button with removed constraint on top

donc le même effet à vouloir atteindre par programme

et voici le code que j'ai essayé:

    if (advertisements.size() > 0) { //my own condition
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();
        layoutParams.topToBottom = R.id.imvEmpty; //the imageview that is in center of the view
        btnCreateAd.setLayoutParams(layoutParams);
        recyclerView.setVisibility(View.VISIBLE);
        txvMyAdEmptyText.setVisibility(View.GONE);
        imvEmpty.setVisibility(View.GONE);
        adapter.setList(advertisements);
        adapter.notifyDataSetChanged();
    } else {
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();
        layoutParams.topToBottom = -1; //here i am trying to remove top constraint but it doesn't work
        btnCreateAd.setLayoutParams(layoutParams);

        recyclerView.setVisibility(View.GONE);
        txvMyAdEmptyText.setVisibility(View.VISIBLE);
        imvEmpty.setVisibility(View.VISIBLE);
        adapter.setList(new ArrayList<Advertisement>());
    }
    mConstraintView.invalidate(); //this is my constraint view

[~ # ~] éditer [~ # ~]

J'ai essayé d'utiliserConstraintSet également, mais le résultat était même différent. Mon RecyclerView (qui est défini sur les limites de la vue parent) disparaissait

 ConstraintSet set = new ConstraintSet();
    set.clone(parentView);

    if (advertisements.size() > 0) {

        recyclerView.setVisibility(View.VISIBLE);
        txvMyAdEmptyText.setVisibility(View.GONE);
        imvEmpty.setVisibility(View.GONE);
        adapter.setList(advertisements);
        adapter.notifyDataSetChanged();

    } else {

        set.connect(btnCreateAd.getId(), ConstraintSet.TOP, imvEmpty.getId(), ConstraintSet.BOTTOM, 0);

        recyclerView.setVisibility(View.GONE);
        txvMyAdEmptyText.setVisibility(View.VISIBLE);
        imvEmpty.setVisibility(View.VISIBLE);
        adapter.setList(new ArrayList<Advertisement>());
    }
    set.connect(btnCreateAd.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);
    set.connect(btnCreateAd.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0);
    set.connect(btnCreateAd.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);

    set.applyTo(parentView);
27
Damir Mailybayev

Je n'ai pas encore travaillé sur votre code, mais ce qui suit montre comment casser et créer la contrainte en utilisant ConstraintSet.

    ConstraintSet set = new ConstraintSet();
    ConstraintLayout layout;

    layout = (ConstraintLayout) findViewById(R.id.layout);
    set.clone(layout);
    // The following breaks the connection.
    set.clear(R.id.bottomText, ConstraintSet.TOP);
    // Comment out line above and uncomment line below to make the connection.
    // set.connect(R.id.bottomText, ConstraintSet.TOP, R.id.imageView, ConstraintSet.BOTTOM, 0);
    set.applyTo(layout);
79
Cheticamp