Update layout from a button

Asked

Viewed 758 times

0

On the main screen of my app there are two buttons, an arrow to the right and another to the left symbolizing the previous and next months, in them I call an index to reload the page with the values of the previous/next month, but it doesn’t look cool the whole screen including the actionbar being called again, I would like to somehow reload only the view/xml. I have tried the invalidate() but without success. Currently my buttons onclicks are like this:

 mesProx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                proximoMes();

                Intent intent = getActivity().getIntent();
                getActivity().finish();
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);    

            }
        });

        mesAnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                anteriorMes();
                Intent intent = getActivity().getIntent();
                getActivity().finish();
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
            }
        });

And this is the main screen of the app

Tela home

Method of screen creation

@Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             final Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_main, container, false);


        DespesaDAO despesaDAO = new DespesaDAO(getActivity());
        ReceitaDAO receitaDAO = new ReceitaDAO(getActivity());

        GraphView graphView = (GraphView) rootView.findViewById(R.id.graph);
        TextView desprog = (TextView) rootView.findViewById(R.id.tvDespPrev);
        TextView despesas = (TextView) rootView.findViewById(R.id.tvsomaDesp);
        TextView recprog = (TextView) rootView.findViewById(R.id.tvRecPrev);
        TextView receitas = (TextView) rootView.findViewById(R.id.tvSomaRec);
        TextView mesano = (TextView) rootView.findViewById(R.id.tvMesAno);
        TextView saldo = (TextView) rootView.findViewById(R.id.tvSaldo);
        TextView saldoprev = (TextView) rootView.findViewById(R.id.tvSaldoPrev);
        TextView acumulado = (TextView) rootView.findViewById(R.id.tvAcumulado);
        TextView nada = (TextView) rootView.findViewById(R.id.tvSL);
        ImageButton mesAnt = (ImageButton) rootView.findViewById(R.id.btAnt);
        ImageButton mesProx = (ImageButton) rootView.findViewById(R.id.btProx);


        Float frcta = receitaDAO.somaReceita(calendar);
        Float frctapg = receitaDAO.somaReceitaRecebido(calendar);
        Float fdspsa = despesaDAO.somaDespesa(calendar);
        Float fdspsapg = despesaDAO.somaDespesaPago(calendar);
        Float sldopg = frcta - fdspsa;
        Float sldo = frctapg - fdspsapg;
        Float maxValue = 0.0F;
        Float minValue = 0.0F;
        Float acmldo = receitaDAO.somaReceitaAcumulado(calendar) - despesaDAO.somaDespesaAcumulado(calendar);
        Float dia1 = receitaDAO.diaUmAcumulado("01", calendar) - despesaDAO.diaUmAcumulado("01", calendar);
        if(dia1 > maxValue)
            maxValue = dia1;
        if(dia1 < minValue)
            minValue = dia1;
        Float dia5 = receitaDAO.diaUmAcumulado("05", calendar) - despesaDAO.diaUmAcumulado("05", calendar);
        if(dia5 > maxValue)
            maxValue = dia5;
        if(dia5 < minValue)
            minValue = dia5;
        Float dia10 = receitaDAO.diaUmAcumulado("10", calendar) - despesaDAO.diaUmAcumulado("10", calendar);
        if(dia10 > maxValue)
            maxValue = dia10;
        if(dia10 < minValue)
            minValue = dia10;
        Float dia15 = receitaDAO.diaUmAcumulado("15", calendar) - despesaDAO.diaUmAcumulado("15", calendar);
        if(dia15 > maxValue)
            maxValue = dia15;
        if(dia15 < minValue)
            minValue = dia15;
        Float dia20 = receitaDAO.diaUmAcumulado("20", calendar) - despesaDAO.diaUmAcumulado("20", calendar);
        if(dia20 > maxValue)
            maxValue = dia20;
        if(dia20 < minValue)
            minValue = dia20;
        Float dia25 = receitaDAO.diaUmAcumulado("25", calendar) - despesaDAO.diaUmAcumulado("25", calendar);
        if(dia25 > maxValue)
            maxValue = dia25;
        if(dia25 < minValue)
            minValue = dia25;
        Float diaMax = receitaDAO.diaUmAcumulado(String.valueOf(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)), calendar) -
                despesaDAO.diaUmAcumulado(String.valueOf(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)), calendar);
        if(diaMax > maxValue)
            maxValue = diaMax;
        if(diaMax < minValue)
            minValue = diaMax;

        final StaticLabelsFormatter labelsFormatter = new StaticLabelsFormatter(graphView);

        if(dia1 == 0 && dia5 == 0 && dia10 == 0 && dia15==0 && dia20 == 0 && dia25==0 && diaMax == 0){
            nada.setVisibility(View.VISIBLE);
        }else
        nada.setVisibility(View.GONE);

        String upperString = new SimpleDateFormat("MMMM").format(calendar.getTime()).substring(0, 1).toUpperCase() + new SimpleDateFormat("MMMM").format(calendar.getTime()).substring(1);
        mesano.setText(upperString + "/" + new SimpleDateFormat("yyyy").format(calendar.getTime()));


        labelsFormatter.setHorizontalLabels(new String[]{"1", "5", "10", "15", "20",
                "25", String.valueOf(calendar.getActualMaximum(Calendar.DAY_OF_MONTH))});
        graphView.getViewport().setYAxisBoundsManual(true);
        graphView.getViewport().setMinY(minValue == 0? minValue : minValue-100 );
        graphView.getViewport().setMaxY(maxValue + 100);


        LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{

                new DataPoint(0, Double.parseDouble(dia1.toString())),
                new DataPoint(1, Double.parseDouble(dia5.toString())),
                new DataPoint(2, Double.parseDouble(dia10.toString())),
                new DataPoint(3, Double.parseDouble(dia15.toString())),
                new DataPoint(4, Double.parseDouble(dia20.toString())),
                new DataPoint(5, Double.parseDouble(dia25.toString())),
                new DataPoint(6, Double.parseDouble(diaMax.toString()))
        });
        graphView.getGridLabelRenderer().setLabelFormatter(labelsFormatter);
        series.setColor(getResources().getColor(R.color.linha_graph));
        graphView.addSeries(series);


        NumberFormat nf = NumberFormat.getCurrencyInstance();

        desprog.setText(nf.format(Double.parseDouble(Float.toString(fdspsa))));
        despesas.setText(nf.format(Double.parseDouble(Float.toString(fdspsapg))));
        recprog.setText(nf.format(Double.parseDouble(Float.toString(frcta))));
        receitas.setText(nf.format(Double.parseDouble(Float.toString(frctapg))));
        saldo.setText(nf.format(Double.parseDouble(Float.toString(sldopg))));
        saldoprev.setText(nf.format(Double.parseDouble(Float.toString(sldo))));
        acumulado.setText(nf.format(Double.parseDouble(Float.toString(acmldo))));

        mesProx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                proximoMes();

                Intent intent = getActivity().getIntent();
                getActivity().finish();
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);

            }
        });

        mesAnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                anteriorMes();
                Intent intent = getActivity().getIntent();
                getActivity().finish();
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
            }
        });

        return rootView;
    }
  • Not knowing how the screen is built/updated makes it difficult to help.

  • Have you tried using as a ViewPager?

  • @ramaral inserted the method

  • @sicachester as it would be this, has some example?

2 answers

1

Opa, my suggestion to help you is to create a method fillActivity(), for example, passing by parameter all the necessary data to popular the graphics and other view. Within this method you must load all the views, then you can call it on onCreate and on the buttons System.

mesProx.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            proximoMes();
            fillActivity();
        }
    });

    mesAnt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            anteriorMes();
            fillActivity();
        }
    });

As already commented, to improve the usability of this screen the ideial would be you put the code of this Activity within Fragment and use Viewpager to manage the months. Swype to change the month is much better than pressing a button. Follow a little guide on how to implement

http://developer.android.com/training/animation/screen-slide.html

1


Your problem exists because you are putting all the logic of your application into a method whose function is just to create the View of Fragment.

I suggest you do so:

Declare all variables representing the components of View as attributes of Fragment

private GraphView graphView;;
private TextView desprog;
private TextView despesas;
private TextView recprog;
private TextView receitas;
private TextView mesano;
private TextView saldo;
private TextView saldoprev;
private TextView acumulado;
private TextView nada;
private ImageButton mesAnt;
private ImageButton mesProx;

Define a method whose function will be to fill these attributes with data. Move their code, which is in the onCreatView, thither:

public void populateView(){

    Float frcta = receitaDAO.somaReceita(calendar);
    Float frctapg = receitaDAO.somaReceitaRecebido(calendar);
    Float fdspsa = despesaDAO.somaDespesa(calendar);
    Float fdspsapg = despesaDAO.somaDespesaPago(calendar);
    Float sldopg = frcta - fdspsa;
    Float sldo = frctapg - fdspsapg;
    Float maxValue = 0.0F;
    Float minValue = 0.0F;
    Float acmldo = receitaDAO.somaReceitaAcumulado(calendar) - despesaDAO.somaDespesaAcumulado(calendar);
    Float dia1 = receitaDAO.diaUmAcumulado("01", calendar) - despesaDAO.diaUmAcumulado("01", calendar);
    if(dia1 > maxValue)
        maxValue = dia1;
    if(dia1 < minValue)
        minValue = dia1;
    Float dia5 = receitaDAO.diaUmAcumulado("05", calendar) - despesaDAO.diaUmAcumulado("05", calendar);
    if(dia5 > maxValue)
        maxValue = dia5;
    if(dia5 < minValue)
        minValue = dia5;
    Float dia10 = receitaDAO.diaUmAcumulado("10", calendar) - despesaDAO.diaUmAcumulado("10", calendar);
    if(dia10 > maxValue)
        maxValue = dia10;
    if(dia10 < minValue)
        minValue = dia10;
    Float dia15 = receitaDAO.diaUmAcumulado("15", calendar) - despesaDAO.diaUmAcumulado("15", calendar);
    if(dia15 > maxValue)
        maxValue = dia15;
    if(dia15 < minValue)
        minValue = dia15;
    Float dia20 = receitaDAO.diaUmAcumulado("20", calendar) - despesaDAO.diaUmAcumulado("20", calendar);
    if(dia20 > maxValue)
        maxValue = dia20;
    if(dia20 < minValue)
        minValue = dia20;
    Float dia25 = receitaDAO.diaUmAcumulado("25", calendar) - despesaDAO.diaUmAcumulado("25", calendar);
    if(dia25 > maxValue)
        maxValue = dia25;
    if(dia25 < minValue)
        minValue = dia25;
    Float diaMax = receitaDAO.diaUmAcumulado(String.valueOf(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)), calendar) -
            despesaDAO.diaUmAcumulado(String.valueOf(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)), calendar);
    if(diaMax > maxValue)
        maxValue = diaMax;
    if(diaMax < minValue)
        minValue = diaMax;

    final StaticLabelsFormatter labelsFormatter = new StaticLabelsFormatter(graphView);

    if(dia1 == 0 && dia5 == 0 && dia10 == 0 && dia15==0 && dia20 == 0 && dia25==0 && diaMax == 0){
        nada.setVisibility(View.VISIBLE);
    }else
    nada.setVisibility(View.GONE);

    String upperString = new SimpleDateFormat("MMMM").format(calendar.getTime()).substring(0, 1).toUpperCase() + new SimpleDateFormat("MMMM").format(calendar.getTime()).substring(1);
    mesano.setText(upperString + "/" + new SimpleDateFormat("yyyy").format(calendar.getTime()));


    labelsFormatter.setHorizontalLabels(new String[]{"1", "5", "10", "15", "20",
            "25", String.valueOf(calendar.getActualMaximum(Calendar.DAY_OF_MONTH))});
    graphView.getViewport().setYAxisBoundsManual(true);
    graphView.getViewport().setMinY(minValue == 0? minValue : minValue-100 );
    graphView.getViewport().setMaxY(maxValue + 100);


    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{

            new DataPoint(0, Double.parseDouble(dia1.toString())),
            new DataPoint(1, Double.parseDouble(dia5.toString())),
            new DataPoint(2, Double.parseDouble(dia10.toString())),
            new DataPoint(3, Double.parseDouble(dia15.toString())),
            new DataPoint(4, Double.parseDouble(dia20.toString())),
            new DataPoint(5, Double.parseDouble(dia25.toString())),
            new DataPoint(6, Double.parseDouble(diaMax.toString()))
    });
    graphView.getGridLabelRenderer().setLabelFormatter(labelsFormatter);
    series.setColor(getResources().getColor(R.color.linha_graph));
    graphView.addSeries(series);


    NumberFormat nf = NumberFormat.getCurrencyInstance();

    desprog.setText(nf.format(Double.parseDouble(Float.toString(fdspsa))));
    despesas.setText(nf.format(Double.parseDouble(Float.toString(fdspsapg))));
    recprog.setText(nf.format(Double.parseDouble(Float.toString(frcta))));
    receitas.setText(nf.format(Double.parseDouble(Float.toString(frctapg))));
    saldo.setText(nf.format(Double.parseDouble(Float.toString(sldopg))));
    saldoprev.setText(nf.format(Double.parseDouble(Float.toString(sldo))));
    acumulado.setText(nf.format(Double.parseDouble(Float.toString(acmldo))));

}

In the method onCreateView only the code to initialize the components of the View, including the onClickListener:

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                         final Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    graphView = (GraphView) rootView.findViewById(R.id.graph);
    desprog = (TextView) rootView.findViewById(R.id.tvDespPrev);
    despesas = (TextView) rootView.findViewById(R.id.tvsomaDesp);
    recprog = (TextView) rootView.findViewById(R.id.tvRecPrev);
    receitas = (TextView) rootView.findViewById(R.id.tvSomaRec);
    mesano = (TextView) rootView.findViewById(R.id.tvMesAno);
    saldo = (TextView) rootView.findViewById(R.id.tvSaldo);
    saldoprev = (TextView) rootView.findViewById(R.id.tvSaldoPrev);
    acumulado = (TextView) rootView.findViewById(R.id.tvAcumulado);
    nada = (TextView) rootView.findViewById(R.id.tvSL);
    mesAnt = (ImageButton) rootView.findViewById(R.id.btAnt);
    mesProx = (ImageButton) rootView.findViewById(R.id.btProx);

    mesProx.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            proximoMes();
            populateView();
        }
    });

    mesAnt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            anteriorMes();
            populateView();
        }
    });

    populateView();
    return rootView;
}

In the method onClick call the method populateView().

The important thing here is to understand that the problem lies in wanting to do everything in just one method that has a well-defined responsibility: to create a View.

I suggest you review the method code populateView() so that their responsibility is only to fill in the views, passing the responsibility to make the calculations to a class, with a method that returns a class with the completed model.

  • very good, thanks for the tip, and if I want to make that move to the left or right that even Evernote doesn’t make when I choose dates, it’s like?

  • Yes. You can use the Viewpager as suggested in one of the comments to your question. However you will have to try to implement it and if you have any difficulty ask a new question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.