将值从回收器适配器传递到android中的片段
Posted
技术标签:
【中文标题】将值从回收器适配器传递到android中的片段【英文标题】:passing values from recycler adapter to fragment in android 【发布时间】:2018-09-09 11:47:14 【问题描述】:我有一个回收器适配器,我为适配器中的项目添加了setOnClickListener
。代码如下:
@Override
public void onBindViewHolder(final FiltersAdapter.MyViewHolder holder, final int position)
holder.mOrganizer.setText(filtersList.get(position));
holder.mLayout.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
try
//Log.e("message","ONclick FIlter");
if (holder.mOrganizerCheck.isChecked())
holder.mOrganizerCheck.setChecked(false);
else
holder.mOrganizerCheck.setChecked(true);
int pos = holder.getAdapterPosition();
filtersList.get(pos);
/*Bundle bundle = new Bundle();
bundle.putInt(pos, );*/
Log.d(TAG, "onClick: " +filtersList);
Log.d(TAG, "onClick of position: " +pos);
catch (Exception e)
e.printStackTrace();
);
这是处理从适配器到片段获取值的片段代码。
private void initView()
mrvFilterBySender = (RecyclerView) mFilterView.findViewById(R.id.rvFilterBySender);
mFiltersAdapter = new FiltersAdapter(getActivity(), mListOrganizer);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
mrvFilterBySender.setLayoutManager(mLayoutManager);
mrvFilterBySender.setAdapter(mFiltersAdapter);
mFiltersAdapter.notifyDataSetChanged();
mrvFilterBySender.setAdapter(mFiltersAdapter);
mrvFilterBySender.setRecyclerListener(new RecyclerView.RecyclerListener()
@Override
public void onViewRecycled(RecyclerView.ViewHolder holder)
holder.getAdapterPosition();
);
我正在尝试将pos
和具有pos
的值从适配器传递到片段。但是我的代码不起作用。我该如何解决?
【问题讨论】:
错误日志是什么? 也许你应该使用接口? ***.com/questions/46160295/… onViewRecycled 不会返回点击的位置,因为这个方法不是为了那个 你应该使用接口来获取项目的位置。 RecyclerView.Adapter 没有像 Listview 这样的 onItemClickListener。 【参考方案1】:将点击事件处理成recyclerview的接口.. 下面的代码用于处理点击事件。
onItemClickListner onItemClickListner;
public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner)
this.onItemClickListner = onItemClickListner;
public interface onItemClickListner
void onClick(String str);//pass your object types.
@Override
public void onBindViewHolder(ItemViewHolder holder, int position)
String data=mStringList.get(position); // if you pass object of class then create that class object.
holder.textView.setText(data);
holder.itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
onItemClickListner.onClick(data);
);
然后当你将适配器绑定到RecyclerView
调用下面的代码之后..
mRvData.setLayoutManager(new LinearLayoutManager(this));
mRvData.setAdapter(recyclerViewAdpater);
recyclerViewAdpater.notifyDataSetChanged();
recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner()
@Override
public void onClick(String str)
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
);
【讨论】:
【参考方案2】:在你的适配器中创建一个接口
public interface OnItemClickListener
void onItemClicked(int position, Object object);
在您的适配器调用中
Adapter adapter = new Adapter(context, list, new Adapter.OnItemClickListener()
@Override
public void onItemClicked(int position, Object object)
// Handle Object of list item here
);
在您的适配器上
private OnItemClickListener onItemClickListener; // Global scope
在构造函数调用中:
this.onItemClickListener = onItemClickListener;
关于您的项目点击事件:
itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
onItemClickListener.onItemClicked(position, _list.get(position));
);
【讨论】:
这行得通,谢谢...为了更清晰的解释 非常感谢!!快乐编码!【参考方案3】:请使用如下接口,将其传递到适配器的构造函数中,然后在您的活动/片段中使用它
public MyAdapter(Context context, List<Object> data, MyAdapterListener myAdapterListener)
this.context = context;
this.data = data;
this.myAdapterListener = myAdapterListener;
public interface MyAdapterListener
void onContainerClick(View view, int position);
public class ViewHolderItem extends RecyclerView.ViewHolder implements View.OnClickListener
public LinearLayout container;
public ImageView poster;
public ViewHolderItem(View v)
super(v);
container = (LinearLayout) v.findViewById(R.id.container);
poster = (ImageView) v.findViewById(R.id.poster);
container =setOnClickListener(this); /// this line must include
@Override
public void onClick(View v)
int id = v.getId();
if (id == R.id.container)
if (myAdapterListener != null)
myAdapterListener.onContainerClick(v,getAdapterPosition());
在您的片段/活动中像这样使用它
mAdapter = new MyAdapter(getApplicationContext() , data, new MyAdapter.MyAdapterListener()
@Override
public void onContainerClick(View v, int position)
Log.d(TAG, "iconTextViewOnClick at position "+position);
);
mRecycler.setAdapter(mAdapter);
【讨论】:
【参考方案4】:我觉得这对你有用
添加你的适配器
public interface ClickEvent
void clickEventItem(int position,String value);
ClickEvent clickevent;
public void setClickEvent(ClickEvent event)
this.clickevent = event;
itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
clickevent.clickEventItem(position, list.get(position));
);
在您的片段集界面中
adapter.setClickEvent(你的类上下文);
实现ClickEvent接口并生成@Override方法
@Override
public void clickEventItem(int position,String value)
//write your code here
【讨论】:
以上是关于将值从回收器适配器传递到android中的片段的主要内容,如果未能解决你的问题,请参考以下文章