Operation of removeViewAt and addView

0

I'm trying to replace a button / object, I have it inside a LinearLayout. Then I use those methods to eliminate the one that is and add in the position that the other was the new one, but do what I want or change in the endings (it's a 3x3 of buttons in the LinearLayout), could someone help me?

tv.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                   hasClick(((TileView) view).x, ((TileView) view).y,((TileView) view).topElements,((TileView) view).index, l2, ((TileView) view).getId());
                }
            });


protected void hasClick(int x, int y,int elements,int index, LinearLayout l2, int eliminar)
{
    TileView tv = new TileView(this,x,y,elements,index,pictures[index]);
    index = tv.getNewIndex();

    TextView tvNumberOfClicks = (TextView) findViewById(R.id.clicksTxt);
    contador++;
    for (int i = 0; i < topTileY; i++) {
        for (int j = 0; j < topTileX; j++) {
            if(j == x && i == y)
            {
                tv = new TileView(this,x,y,elements,index,pictures[index]);
                tv.setHeight(height);
                tv.setWidth(width);
                l2.removeViewAt(eliminar);
                l2.addView(tv,eliminar);
            }

        }
    }
    //compruebo que obtengo la posicion del los objetos en el Linear
    tvNumberOfClicks.setText(String.valueOf(eliminar));
}

TileView

package com.example.killtrols.casillas;

import android.content.Context;
import android.widget.Button;
import android.support.v7.widget.AppCompatButton;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.button;

/**
 * Created by killtrols on 15/11/2017.
 */

public class TileView extends AppCompatButton {
    // coordenadas
    public int x = 0;
    public int y = 0;
    // trama a mostrar
    public int index = 0;
    //max tramas
    public int topElements = 0;
    public TileView(Context context, int x, int y, int topElements, int index, int
            background) {
        super(context);
        this.x = x; //coordenada X
        this.y = y; //coordenada Y
        this.topElements = topElements; //max tramas
        this.index = index; //índice de trama
        this.setBackgroundResource(background);
    }
    public void setBackground(int id)
    {
        setBackgroundResource(id);
    }
    public int getNewIndex(){
        index ++;
        //controlar si necesitamos volver a comenzar el ciclo de tramas
        if (index == topElements)index = 0;
        return index;
    }
}
    
asked by Alexis 18.11.2017 в 19:54
source

1 answer

0

When you are adding your tileview, you have to put the layout params.

tv = new TileView(this,x,y,elements,index,pictures[index]);
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
                                    LayoutParams.WRAP_CONTENT));
    
answered by 20.11.2017 в 00:27