Show Layout when clicking on RecyclerView item

0

I am using a RecyclerView to show a lista of usuarios . And I would like that when doing click in the items of the lista a layout is launched showing me the information that I have saved in my bd of usuario selected, without starting a new actividad . I share the code to see if you can help me.

MyDialog Box

public class MiCuadroDeDialogo extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_usuario, null);
    builder.setView(v);

    return builder.create();
}
}

Pending User

public class UsuarioPendiente extends AppCompatActivity {

Context context;
LinearLayout volver;
RecyclerView listaPendientes;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_usuario_pendiente);
    context = UsuarioPendiente.this;
    volver = (LinearLayout) findViewById(R.id.btnVolverPendientes);


    listaPendientes = (RecyclerView) findViewById(R.id.listaPendientes);
    listaPendientes.setLayoutManager(new LinearLayoutManager(context));
    listaPendientes.setItemAnimator(new DefaultItemAnimator());
    ControladorUsuario controlador = new ControladorUsuario(context);
    final UsuarioAdapter adapter = new UsuarioAdapter(context, controlador.listaUsuarios());
    listaPendientes.setAdapter(adapter);



    volver.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}
}

UserAdapter

public class UsuarioAdapter extends RecyclerView.Adapter<UsuarioHolder> implements Filterable {

Context c;
public ArrayList<Usuario> usuarios, listaFiltrada;
FiltroUsuarios filtro;



   public UsuarioAdapter(Context ctx, ArrayList<Usuario> usuarios){
   this.c=ctx;
   this.usuarios=usuarios;
   this.listaFiltrada=usuarios;


 }

@Override
public UsuarioHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lista_usuarios,null);

    //HOLDER
    UsuarioHolder holder=new UsuarioHolder(v);

    return holder;
}

@Override
public void onBindViewHolder(UsuarioHolder holder, final int position) {
    holder.nombre.setText(usuarios.get(position).getNombre());
    holder.apellido.setText(usuarios.get(position).getApellido());
    holder.bind(usuarios.get(position));

}

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

@Override
public Filter getFilter() {
    if (filtro==null){
        filtro = new FiltroUsuarios(listaFiltrada, this);
    }
    return filtro;
}

}

UserHolder

public class UsuarioHolder extends RecyclerView.ViewHolder {

public TextView nombre, apellido;
public LinearLayout linearLayoutItem;



public UsuarioHolder(View itemView) {
    super(itemView);
    this.apellido = (TextView) itemView.findViewById(R.id.nombreUsuario);
    this.nombre = (TextView) itemView.findViewById(R.id.apellidoUsuario);

    linearLayoutItem = (LinearLayout) itemView.findViewById(R.id.linearLayoutItem);

}

 public void bind(Usuario user) {
    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
       /* como lanzo el cuadro de dialogo correctamente aqui*/



        }
    });

}

dialog_user.xml

    
asked by Germanccho 07.12.2017 в 01:13
source

2 answers

1

Use an AlertDialog that is easier. For that you have to use AlertDialog.Builder and assign the parameters you need, in this case setView(view) and show () to show the dialog.

First you have to send the context to UsuarioHolder since AlertDialog.Builder requires the context in the constructor:

//...
@Override
public UsuarioHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lista_usuarios,null);

    // inicia el holder y envia el contexto en el constructor
    return new UsuarioHolder(v, this.c);;
}
//...

Now we must define that UsuarioHolder receive the context in the constructor:

public class UsuarioHolder extends RecyclerView.ViewHolder {

    //...
    private Context context;
    public UsuarioHolder(View itemView, Context context) {
        //...

        this.context = context;
    }

So now in the onClick method the code where you will show the dialog is placed:

itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // inflamos el layout   
        View view = getActivity()
        .getLayoutInflater()
        .inflate(R.layout.dialog_usuario, null);

        new AlertDialog.Builder(context)
        .setView(view)
        .show();

        //utilizando view, puedesa acceder a los controles del layout dialog_usuario
        TextView username = (TextView)view.findViewById(R.id.idDeUnControl);
    }
});
    
answered by 07.12.2017 / 13:45
source
0

You will open an AlertDialog with a personalized view that shows you this information, something like that.

First assign the onClick event to your elements in the recyclerview using a method in your holder, for example:

public void bind(Usuario user) { itemView.setOnClickListener(new OnClickListener() { /* aqui creas tu alertdialog */ });

This method is executed in the onBindViewHolder method

holder.bind(usuarios.get(position));

You pass the user object so that you retrieve that data in the bind method and you paint them in your alertdialog

    
answered by 07.12.2017 в 01:23