Java 当在片段中时,如何在recyclerview中获取所选项目?
从当前片段中,我想获取所选项目在recyclerview中的位置,但不在适配器中 现在我有两个回收视图。 单击一个我想将数据发送到第二个,但按当前片段发送,因为这将生成一个单独的数据。 首先,重要的是创建一个包含函数的接口,并在类片段中重写该函数 例 Cintasadapter[适配器] Cargas_detalle_previo[片段] 在适配器中创建一个接口,例如 例 CintasAdapter.javaJava 当在片段中时,如何在recyclerview中获取所选项目?,java,android,android-recyclerview,Java,Android,Android Recyclerview,从当前片段中,我想获取所选项目在recyclerview中的位置,但不在适配器中 现在我有两个回收视图。 单击一个我想将数据发送到第二个,但按当前片段发送,因为这将生成一个单独的数据。 首先,重要的是创建一个包含函数的接口,并在类片段中重写该函数 例 Cintasadapter[适配器] Cargas_detalle_previo[片段] 在适配器中创建一个接口,例如 例 CintasAdapter.java /// The interface public interface getsele
/// 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());
}
}
//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();
}
}
/// 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());
}
}
//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();
}
}