Refresh layout from a button

0

On the main screen of my app there are two buttons, one to the right and the other to the left, symbolizing the previous and next months, in them I call an attempt to reload the page with the values of the previous / next month, but it does not look cool the entire screen including the actionbar being called again, I would like to somehow reload only the view / xml. I already tried invalidate (), but without success. Currently my buttons onclicks look 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 app's main screen

Screencreationmethod

@OverridepublicViewonCreateView(finalLayoutInflaterinflater,finalViewGroupcontainer,finalBundlesavedInstanceState){finalViewrootView=inflater.inflate(R.layout.fragment_main,container,false);DespesaDAOdespesaDAO=newDespesaDAO(getActivity());ReceitaDAOreceitaDAO=newReceitaDAO(getActivity());GraphViewgraphView=(GraphView)rootView.findViewById(R.id.graph);TextViewdesprog=(TextView)rootView.findViewById(R.id.tvDespPrev);TextViewdespesas=(TextView)rootView.findViewById(R.id.tvsomaDesp);TextViewrecprog=(TextView)rootView.findViewById(R.id.tvRecPrev);TextViewreceitas=(TextView)rootView.findViewById(R.id.tvSomaRec);TextViewmesano=(TextView)rootView.findViewById(R.id.tvMesAno);TextViewsaldo=(TextView)rootView.findViewById(R.id.tvSaldo);TextViewsaldoprev=(TextView)rootView.findViewById(R.id.tvSaldoPrev);TextViewacumulado=(TextView)rootView.findViewById(R.id.tvAcumulado);TextViewnada=(TextView)rootView.findViewById(R.id.tvSL);ImageButtonmesAnt=(ImageButton)rootView.findViewById(R.id.btAnt);ImageButtonmesProx=(ImageButton)rootView.findViewById(R.id.btProx);Floatfrcta=receitaDAO.somaReceita(calendar);Floatfrctapg=receitaDAO.somaReceitaRecebido(calendar);Floatfdspsa=despesaDAO.somaDespesa(calendar);Floatfdspsapg=despesaDAO.somaDespesaPago(calendar);Floatsldopg=frcta-fdspsa;Floatsldo=frctapg-fdspsapg;FloatmaxValue=0.0F;FloatminValue=0.0F;Floatacmldo=receitaDAO.somaReceitaAcumulado(calendar)-despesaDAO.somaDespesaAcumulado(calendar);Floatdia1=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;
    }
    
asked by anonymous 03.03.2015 / 21:48

2 answers

1

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

I suggest you do this:

Declare all the variables that represent View components as Fragment attributes

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 is to fill these attributes with data. Move its code, which is in onCreatView , over there:

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 onCreateView method, only the code to initialize the View components, including 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 method onClick call 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: create a View .

I suggest that you review the code of the populateView() method so that it is only your responsibility to fill in the views , passing on the responsibility of doing the calculations for a class, with a method that returns a class with the template filled in.

    
04.03.2015 / 15:34
1

Oops, my suggestion to help you is to create a fillActivity () method, for example, passing through all the necessary data parameters to popular graphics and other view. Within this method you must load all the views, so you can call it in the onCreate and the button listener.

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 mentioned, to improve the usability of this screen ideial would be to put the code of this activity within fragment and use ViewPager to manage the months. Swype to change the month is much better than pushing a button. Here is a short guide on how to implement

link

    
04.03.2015 / 02:13