简体   繁体   中英

How to Get the selected item in a recyclerview, when is within a fragment?

From the current fragment, I want to get the position of the selected item in recyclerview, but not in the adapter

now I have two recyclerview. Clicking one I want to send my data to the second, but by the current fragment, because this will generate adional data

Resolved. first of all it is important to create an interface that contains a function and overrrite this function in the class fragment.

Exemple

Cintasadapter [adapter] Cargas_detalle_previo [fragment]

create an interface in adapter for exemple EXEMPLE CintasAdapter.java

 /// The interface
public interface getselectpos
    {
        void onItemClick(CintasHolder holder, int posicion);
    }

private getselectpos interfaceclick;
//Declared a private variable to manage the interface



//in the constructor recover the interface, for exemple.

//CintasDataset is a class to manage the data for the adapter

 public CintasAdapter(List<CintasDataset> items, getselectpos Interfaceclick)
    {
        this.mitems = items; //the elements 
        this.interfaceclick=Interfaceclick; // the interface create from in the fragment
    }




/// Class holder for this class , and implements OnClickListener

public class CintasHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        public ImageView imagen; //an imagen
        public TextView modocorte;/a textview 

        public CintasHolder(View itemView)
        {
            super(itemView);
            itemView.setOnClickListener(this);//add the listener for an element
        }

        //add onClick and set the funcion onItemClick , declared in the interface
        @Override
        public void onClick(View view)
        {
            interfaceclick.onItemClick(this, getAdapterPosition());
        }
    }

Implements that interface in the fragment

EXEMPLE Cargas_detalle_previo.java

//Cargas_detalle_previo is the fragment class, the interface  "CintasAdapter.getselectpos "
public class Cargas_detalle_previo extends Fragment  implements  CintasAdapter.getselectpos
{
    //TODO CONTENT
    //exemple for onViewCreated
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        //TODO CONTENT
         List mitems2 = new ArrayList(); // for exemple 

        //declared the adapter
         final CintasAdapter adaptercintas = new CintasAdapter(mitems,this);
    }



    //and  override the function in the interface, in the case  onItemClick
    @Override
    public void onItemClick(CintasAdapter.CintasHolder holder, int posicion)
    {


        //and use the posicion 

        //exemple to show the posicion
        Toast.makeText(getContext(),posicion+"",Toast.LENGTH_LONG).show();


    }

}

And sorry for my bad english

I resolved this by a friend told me to check interfaces in java, I hope it can serve someone else

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM