1 回答
TA贡献1876条经验 获得超6个赞
RecyclerView 的适配器不负责重定向到另一个片段。
像这样创建界面
public interface OnItemClickListener {
void onItemClicked(int position)
}
在 RecyclerView 的适配器中添加方法:
public class YourAdapterName extend RecyclerView.Adapter...
private OnItemClickListener onItemClickListener
void setOnItemClickListener(OnItemClickListener listener) {
onItemClickListener = listener
}
...
@Override
public void onBindViewHolder(@NonNull final CoordinatesViewHolder holder, int position) {
final Coordinates coord = mCoordinates.get(position);
holder.place_name.setText(coord.getmUPlaceName());
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onItemClickListener != null) {
onItemClickListener.onItemClicked(position)
}
}
});
}
在带有回收器的片段中,在设置适配器的地方添加代码:
YourAdapterClassName adapter = new YourAdapterClassName(...init adapter...)
adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClicked(int position) {
//Navigate here
}
})
yourRecyclerName.setAdapter(adapter)
希望它会有所帮助)
添加回答
举报