如何在recyclerview中获取所选项目,何时在片段内?
Posted
技术标签:
【中文标题】如何在recyclerview中获取所选项目,何时在片段内?【英文标题】:How to Get the selected item in a recyclerview, when is within a fragment? 【发布时间】:2016-05-06 03:11:07 【问题描述】:从当前fragment中,我想在recyclerview中获取选中项的位置,而不是在adapter中
现在我有两个recyclerview。 单击一个我想将我的数据发送到第二个,但是通过当前片段,因为这会生成附加数据
【问题讨论】:
【参考方案1】:已解决。 首先,重要的是创建一个包含函数的接口并在类片段中覆盖该函数。
示例
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_detelle_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();
https://dzone.com/articles/programming-when-use
http://beginnersbook.com/2013/05/java-interface/
抱歉我的英语不好
我的朋友告诉我检查java中的接口解决了这个问题,我希望它可以服务于其他人
【讨论】:
以上是关于如何在recyclerview中获取所选项目,何时在片段内?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 RecyclerView 中选择和取消选择项目?如何仅在回收站视图中突出显示所选项目?
如何知道何时显示布局中的特定视图在RecyclerView中