Recyclerview - Is it possible to change the current view in the long click event?

Asked

Viewed 80 times

1

In a certain part of my app you will get a Recyclerview as a list of expenses as shown below:

Lista de despesas

The point is, I want the user to be able to edit expenses at any time just by having a long click, thus replacing them with editable widgets, and at the end of editing, go back to the default presentation view.

package br.com.ivesti.ivesmoney.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

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

import br.com.ivesti.ivesmoney.R;
import br.com.ivesti.ivesmoney.database.ExpensesTable;

public class ExpensesAdapter extends RecyclerView.Adapter<ExpensesAdapter.ExpensesHolder> {

    private Context mContext;
    private List<ExpensesTable> mData = new ArrayList<>();

    public ExpensesAdapter(@NonNull Context context, @NonNull List<ExpensesTable> dataSet){
        this.mContext = context;
        this.mData = dataSet;
    }

    @NonNull
    @Override
    public ExpensesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return new ExpensesHolder(inflater.inflate(R.layout.expenses_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ExpensesHolder holder, int position) {

        final ExpensesTable item = mData.get(position);
        final int type = item.type;



    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    protected class ExpensesHolder extends RecyclerView.ViewHolder{

        ImageView ivIcon;
        Spinner spType;
        EditText etValue;

        public ExpensesHolder(@NonNull View itemView) {
            super(itemView);

            ivIcon = (ImageView)itemView.findViewById(R.id.expItemExpIcon);
            spType = (Spinner)itemView.findViewById(R.id.expItemSpinner);
            etValue = (EditText)itemView.findViewById(R.id.expItemValue);

        }
    }

}

This is my Adapter, I left the View edition inflated as default, but I wish I could change it on onLongClick(View v) and then back to the default view

1 answer

1

You can create onLongItenClick both inside the adapter and onCreate this Listener will return the position of the object in the list and then you can pass this position to a method that will be responsible for opening a Dialog on the screen. This dialog can have the layout fully customized and after editing the user clicks ok and goes back to the screen where Recycler is.I will assume that it is OK to call onLongItemClick on onCreate:

1.no oncreate after creating your Recycler:

TuarecyclerView.addOnItemTouchListener(new 
 RecyclerItemClickListener(this,
TuarecyclerView, new 
 RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {



}

@Override
public void onLongItemClick(View view, int position) {
MetodoQueVaiCriarODialog( position);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

}
}));

Outside of onCreate you create the method Methodoquevaicreaodialog( position);

2.Create the layout of what you have called the widget in the way you want it to be presented to the user when it gives the long click. The layout should be created like any other there in res>layout>new layout resourse file

3.below the skeleton of the method that will be called when it has long click:

private void MetodoQueVaiCriarODialog (final int position) {
final Dialog  dialogPersonalizado= new Dialog(NomeDaActivityOndeEstaARecycler.this);
dialogPersonalizado.setCancelable(false);//serve para evitar que o dialog feche 
//clicando fora dele isso obriga o usuário a clicar em algum botão de ação .se não for 
//necessário é so remover
dialogPersonalizado.requestWindowFeature(Window.FEATURE_NO_TITLE);//remove titulo 
//padrao
dialogPersonalizado.setContentView(R.layout.layout_que_tu_criou);
// nesse ponto tu tem que inicializar os componentes do layout_que_tu_criou vou deixar 
// o 
// exemplo de um textView e de um Button
TextView  nome=dialogPersonalizado.findViewById(R.id.Id_do_textView);
nome.setText(lista_de_despesas_usadas_na_recycler.get(position).getNome());
Button cancelar =dialogPersonalizado.findViewById(R.id.id_do_button_cancelar);
Button salvar=dialogPersonalizado.findViewById(R.id. id_do_button_salvar);
cancelar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        dialogPersonalizado.dismiss;

    }
});
salvar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        /*execute aqui a ação necessária para salvar a alteração*/

    }
});

dialogPersonalizado.show();

}

Browser other questions tagged

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