gpt4 book ai didi

java - 在recyclerview中设置onclicklistener

转载 作者:行者123 更新时间:2023-12-01 09:38:29 25 4
gpt4 key购买 nike

我的适配器类:

public class Adaptor extends RecyclerView.Adapter<Adaptor.Holder>{

private ArrayList<Winkel> winkels;
private LayoutInflater inflater;
private ImageView icon;
private ItemCLickCallback itemCLickCallback;

public interface ItemCLickCallback {
void onItemClick(int p);
void onSecItemClick(int p);

}

public void setItemCLickCallback(final ItemCLickCallback itemCLickCallback1){
;this.itemCLickCallback = itemCLickCallback;
}

public Adaptor (ArrayList<Winkel> winkels,Context c){
this.inflater = LayoutInflater.from(c);
this.winkels = winkels;
}


@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_item,parent,false);
return new Holder(view);
}

@Override
public void onBindViewHolder(Holder holder, int position) {
Winkel winkel = winkels.get(position);
holder.title.setText(winkel.getNaam());
if (winkel.isFavourtite()){
holder.icon.setImageResource(R.drawable.ic_star_black_18dp);
}
else{
holder.icon.setImageResource(R.drawable.ic_star_border_black_18dp);
}

}

@Override
public int getItemCount() {

return winkels.size();
}

class Holder extends RecyclerView.ViewHolder implements View.OnClickListener{

private TextView title;
private View container;
private ImageView icon;

public Holder(View itemView) {
super(itemView);

title = (TextView)itemView.findViewById(R.id.lbl_item_text);
container = itemView.findViewById(R.id.cont_item_root);
icon = (ImageView) itemView.findViewById(R.id.im_item_icon_secondary);
icon.setOnClickListener(this);
container.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if(v.getId()==R.id.cont_item_root){
itemCLickCallback.onItemClick(getAdapterPosition());

}
else{
itemCLickCallback.onSecItemClick(getAdapterPosition());
}
}
}
}


@Override
public void onClick(View v) {

}
}
}

实现它的代码(我的问题在adaptor.setItemClickCallback(context)

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Adaptor adaptor = new Adaptor(winkels,context);
recyclerview.setLayoutManager(new LinearLayoutManager(context));
recyclerview.addItemDecoration(new VerticalSpace(30));
recyclerview.setAdapter(adaptor);
adaptor.setItemCLickCallback(context);

}

从 fragment 概述中调用:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_overview, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycle);
ODKortrijkWebservice webs = new ODKortrijkWebservice(this.getActivity(),mRecyclerView);
webs.execute();
return view;
}

如何设置 ItemCLickCallback(this) 的上下文?它应该是上下文(至少我认为是这样?),因为这是从 fragment 传递的内容,但我似乎不知道如何让它工作,所以一些帮助将不胜感激。基本上我想为我的 recyclerview 中的每个项目添加 onclicklisteners,并且 recyclerview 是通过使用来自 Web 服务的数据创建的,因此为什么从我从 Web 服务获取数据的类调用适配器类,该类在我的分段。谢谢。

最佳答案

我建议您按照以下方式制作ODKortrijkWebservice.Callback

在您的 ODKortrijkWebservice 中:

public class ODKortrijkWebservice extends AsyncTask<...> {

private Callback mCallback;

public interface Callback {
void onSuccess(ArrayList<Winkel> winkels);
}

public ODKortrijkWebservice(Context context, Callback callback) {
...
mCallback = callback;
...
}

@Override
protected void onPostExecute(Void aVoid) {
...
mCallback.onSuccess(winkels);
}
}

在您的 fragment 中:

public class YourFragment extends Fragment implements ODKortrijkWebservice.Callback, Adaptor.ItemCLickCallback {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_overview, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycle);
ODKortrijkWebservice webs = new ODKortrijkWebservice(this.getActivity(), this);
webs.execute();

return view;
}

@Override
public void onSuccess(ArrayList<Winkel> winkels) {
Adaptor adaptor = new Adaptor(winkels, getContext());
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.addItemDecoration(new VerticalSpace(30));
mRecyclerView.setAdapter(adaptor);
adaptor.setItemCLickCallback(this);
}

@Override
public void onItemClick(int p) {

}

@Override
public void onSecItemClick(int p) {

}
}

关于java - 在recyclerview中设置onclicklistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38639256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com