从当前片段,我想在recyclerview中获取所选项目的位置,但不是在适配器中
现在我有两个recyclerview。 单击一个我想将数据发送到第二个,但是按当前片段,因为这将生成adional数据
答案 0 :(得分:0)
解决。 首先,创建一个包含函数的接口并在类片段中覆盖此函数非常重要。
为例
Cintasadapter [适配器] Cargas_detalle_previo [片段]
在适配器中为例子创建一个接口 为例 的 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());
}
}
在片段中实现该接口
为例 的 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();
}
}
抱歉我的英文不好
我解决了这个问题,朋友告诉我检查java中的接口,我希望它能为别人服务