带有构造函数的 ViewHolder,它采用视图回收器视图
ViewHolder with a constructor which takes a view recycler view
嗨,朋友们,我从 2 天起就遇到了这个问题,我不知道这是什么问题,我正在使用 mikepenz 的 Fast Adapter
,我在 this.Here 中有两个回收站视图,我正在这个错误
java.lang.RuntimeException: You have to provide a ViewHolder with a constructor which takes a view!
at com.mikepenz.fastadapter.items.AbstractItem$ReflectionBasedViewHolderFactory.create(AbstractItem.java:349)
我认为我已经将所有设置正确,因为我在其他 class 中也使用相同的格式,但我只面临这个问题 class 而且我没有激活程序,请查找下面的代码
主要Class:
public class Incoming_request extends Fragment {
@BindView(R.id.incoming_req_recycler_view)
RecyclerView incoming_req_recycler_view;
@BindView(R.id.people_recycler_view)
RecyclerView people_recycler_view;
FastItemAdapter<Incoming_Request_adapter> incoming_req_adapter = new FastItemAdapter<>();
FastItemAdapter<People_Request_adapter> people_req_adapter = new FastItemAdapter<>();
@BindView(R.id.frd_req_no)
AppCompatTextView frd_req_no;
@BindView(R.id.namesearch)
AutoCompleteTextView namesearch;
ArrayList<Search_Pojo> searchlist = new ArrayList<>();
Search_Adapter search_adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.incoming_req, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
incoming_req_recycler_view.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
people_recycler_view.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
incoming_requests("");
incoming_req_recycler_view.setAdapter(incoming_req_adapter);
people_recycler_view.setAdapter(people_req_adapter);
search_adapter=new Search_Adapter(getActivity(),R.layout.search_adapter_item,searchlist);
namesearch.setThreshold(1);
namesearch.setAdapter(search_adapter);
search_adapter.notifyDataSetChanged();
namesearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Search_Pojo search = (Search_Pojo) parent.getItemAtPosition(position);
namesearch.setText(search.getUsername());
Intent otherprofileintent = new Intent(getActivity(), Other_Profile.class);
otherprofileintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
otherprofileintent.putExtra("frd_status", search.getFriend_status());
otherprofileintent.putExtra("frd_id", search.getUserid());
startActivity(otherprofileintent);
}
});
people_req_adapter.withOnClickListener(new FastAdapter.OnClickListener<People_Request_adapter>() {
@Override
public boolean onClick(View v, IAdapter<People_Request_adapter> adapter, People_Request_adapter item, int position) {
Intent otherprofileintent = new Intent(getActivity(), Other_Profile.class);
otherprofileintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
otherprofileintent.putExtra("frd_status", item.getFriend_status());
otherprofileintent.putExtra("frd_id", item.getTo_user_id());
startActivity(otherprofileintent);
return true;
}
});
people_req_adapter.withItemEvent(new ClickEventHook<People_Request_adapter>() {
@Override
public void onClick(View v, int position, FastAdapter<People_Request_adapter> fastAdapter, People_Request_adapter item) {
acceptrequest(item.getTo_user_id(), position);
}
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof Incoming_Request_adapter.FriendRequest_ViewHolder) {
return viewHolder.itemView.findViewById(R.id.accept);
}
return null;
}
});
people_req_adapter.withItemEvent(new ClickEventHook<People_Request_adapter>() {
@Override
public void onClick(View v, int position, FastAdapter<People_Request_adapter> fastAdapter, People_Request_adapter item) {
deleterequest(item.getTo_user_id(), position);
}
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof Incoming_Request_adapter.FriendRequest_ViewHolder) {
return viewHolder.itemView.findViewById(R.id.cancel);
}
return null;
}
});
people_req_adapter.withItemEvent(new ClickEventHook<People_Request_adapter>() {
@Override
public void onClick(View v, int position, FastAdapter<People_Request_adapter> fastAdapter, People_Request_adapter item) {
sendrequest(item.getTo_user_id(), position);
}
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof Incoming_Request_adapter.FriendRequest_ViewHolder) {
return viewHolder.itemView.findViewById(R.id.sendrequest);
}
return null;
}
});
incoming_req_adapter.withOnClickListener(new FastAdapter.OnClickListener<Incoming_Request_adapter>() {
@Override
public boolean onClick(View v, IAdapter<Incoming_Request_adapter> adapter, Incoming_Request_adapter item, int position) {
Intent otherprofileintent = new Intent(getActivity(), Other_Profile.class);
otherprofileintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
otherprofileintent.putExtra("frd_status", item.getFriend_status());
otherprofileintent.putExtra("frd_id", item.getTo_user_id());
startActivity(otherprofileintent);
return true;
}
});
incoming_req_adapter.withItemEvent(new ClickEventHook<Incoming_Request_adapter>() {
@Override
public void onClick(View v, int position, FastAdapter<Incoming_Request_adapter> fastAdapter, Incoming_Request_adapter item) {
acceptrequest(item.getTo_user_id(), position);
}
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof Incoming_Request_adapter.FriendRequest_ViewHolder) {
return viewHolder.itemView.findViewById(R.id.accept);
}
return null;
}
});
incoming_req_adapter.withItemEvent(new ClickEventHook<Incoming_Request_adapter>() {
@Override
public void onClick(View v, int position, FastAdapter<Incoming_Request_adapter> fastAdapter, Incoming_Request_adapter item) {
deleterequest(item.getTo_user_id(), position);
}
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof Incoming_Request_adapter.FriendRequest_ViewHolder) {
return viewHolder.itemView.findViewById(R.id.cancel);
}
return null;
}
});
incoming_req_adapter.withItemEvent(new ClickEventHook<Incoming_Request_adapter>() {
@Override
public void onClick(View v, int position, FastAdapter<Incoming_Request_adapter> fastAdapter, Incoming_Request_adapter item) {
sendrequest(item.getTo_user_id(), position);
}
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof Incoming_Request_adapter.FriendRequest_ViewHolder) {
return viewHolder.itemView.findViewById(R.id.sendrequest);
}
return null;
}
});
/*incoming_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadrefreshcomplete = 1;
incoming_req_adapter.clear();
incoming_requests("");
}
});*/
}
private void incoming_requests(String value) {
if (value.isEmpty()) {
Constant.showloader(getActivity());
}
String outgoing_requesturl = Constant.psurl + "myrequests&userid=" + Session.getUserID(getActivity());
Constant.l(outgoing_requesturl);
AndroidNetworking.get(outgoing_requesturl).setOkHttpClient(Constant.okClient()).build().getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.has("friendrequests")) {
JSONArray userlistarray = response.getJSONArray("friendrequests");
for (int i = 0; i < userlistarray.length(); i++) {
JSONObject frdsarrayobj = userlistarray.getJSONObject(i);
Incoming_Request_adapter incomingadapter = new Incoming_Request_adapter();
Search_Pojo search_pojo = new Search_Pojo();
search_pojo.setUserid(frdsarrayobj.getString("userid"));
search_pojo.setUsername(frdsarrayobj.getString("username"));
search_pojo.setEmail(frdsarrayobj.getString("email"));
search_pojo.setPhone(frdsarrayobj.getString("phone"));
search_pojo.setCity(frdsarrayobj.getString("city"));
search_pojo.setUserstatus(frdsarrayobj.getString("user_status"));
search_pojo.setUser_profile_pic(frdsarrayobj.getString("userprofile"));
search_pojo.setFriend_status(frdsarrayobj.getString("friend_status"));
incomingadapter.setTo_user_id(frdsarrayobj.getString("userid"));
incomingadapter.setUsername(frdsarrayobj.getString("username"));
incomingadapter.setEmail(frdsarrayobj.getString("email"));
incomingadapter.setPhone(frdsarrayobj.getString("phone"));
incomingadapter.setCity(frdsarrayobj.getString("city"));
incomingadapter.setUserstatus(frdsarrayobj.getString("user_status"));
incomingadapter.setUser_profile_pic(frdsarrayobj.getString("userprofile"));
incomingadapter.setFriend_status(frdsarrayobj.getString("friend_status"));
incoming_req_adapter.add(incomingadapter);
searchlist.add(search_pojo);
}
} else {
frd_req_no.setVisibility(View.VISIBLE);
incoming_req_recycler_view.setVisibility(View.GONE);
}
if (response.has("userslist")) {
JSONArray frdlistarray = response.getJSONArray("userslist");
for (int j = 0; j < frdlistarray.length(); j++) {
JSONObject userarrayobj = frdlistarray.getJSONObject(j);
Search_Pojo search_pojo = new Search_Pojo();
search_pojo.setUserid(userarrayobj.getString("userid"));
search_pojo.setUsername(userarrayobj.getString("username"));
search_pojo.setEmail(userarrayobj.getString("email"));
search_pojo.setPhone(userarrayobj.getString("phone"));
search_pojo.setCity(userarrayobj.getString("city"));
search_pojo.setUserstatus(userarrayobj.getString("user_status"));
search_pojo.setUser_profile_pic(userarrayobj.getString("userprofile"));
search_pojo.setFriend_status(userarrayobj.getString("friend_status"));
People_Request_adapter peoplgreqadapter = new People_Request_adapter();
peoplgreqadapter.setTo_user_id(userarrayobj.getString("userid"));
peoplgreqadapter.setUsername(userarrayobj.getString("username"));
peoplgreqadapter.setEmail(userarrayobj.getString("email"));
peoplgreqadapter.setPhone(userarrayobj.getString("phone"));
peoplgreqadapter.setCity(userarrayobj.getString("city"));
peoplgreqadapter.setUserstatus(userarrayobj.getString("user_status"));
peoplgreqadapter.setUser_profile_pic(userarrayobj.getString("userprofile"));
peoplgreqadapter.setFriend_status(userarrayobj.getString("friend_status"));
people_req_adapter.add(peoplgreqadapter);
searchlist.add(search_pojo);
}
}
} catch (JSONException e) {
Constant.l(e.toString());
Constant.dismissloader();
}
if (!value.isEmpty()) {
Constant.dismissloader();
}
/*if (loadrefreshcomplete == 1) {
loadrefreshcomplete = 0;
incoming_refresh.setRefreshing(false);
Constant.dismissloader();
}*/
}
@Override
public void onError(ANError anError) {
Constant.l(anError.toString());
Constant.dismissloader();
}
});
}
public void acceptrequest(String fid, final int position) {
Constant.showloader(getActivity());
String sendurl = Constant.psurl + "sendrequest&from_id=" + fid + "&to_id=" + Session.getUserID(getActivity());
Constant.l(sendurl);
AndroidNetworking.get(sendurl).build().getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.getString("status").equals("Success")) {
incoming_req_adapter.remove(position);
}
} catch (JSONException e) {
Constant.l(e.toString());
Constant.dismissloader();
}
Constant.dismissloader();
}
@Override
public void onError(ANError anError) {
Constant.l(anError.toString());
Constant.dismissloader();
}
});
}
public void deleterequest(String fid, final int position) {
Constant.showloader(getActivity());
String sendurl = Constant.psurl + "deleterequest&from_id=" + Session.getUserID(getActivity()) + "&to_id=" + fid;
Constant.l(sendurl);
AndroidNetworking.get(sendurl).build().getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.getString("status").equals("Success")) {
incoming_req_adapter.remove(position);
}
} catch (JSONException e) {
Constant.l(e.toString());
Constant.dismissloader();
}
Constant.dismissloader();
}
@Override
public void onError(ANError anError) {
Constant.l(anError.toString());
Constant.dismissloader();
}
});
}
public void sendrequest(String fid, final int position) {
Constant.showloader(getActivity());
String sendurl = Constant.psurl + "sendrequest&from_id=" + Session.getUserID(getActivity()) + "&to_id=" + fid;
Constant.l(sendurl);
AndroidNetworking.get(sendurl).build().getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.getString("status").equals("Success")) {
incoming_req_adapter.remove(position);
EventBus.getDefault().post(new Event_Bus("outgoing"));
}
} catch (JSONException e) {
Constant.l(e.toString());
Constant.dismissloader();
}
Constant.dismissloader();
}
@Override
public void onError(ANError anError) {
Constant.l(anError.toString());
Constant.dismissloader();
}
});
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void groupcreated(Event_Bus eventBus) {
if (eventBus.getEventname().equals("incoming")) {
incoming_req_adapter.clear();
incoming_requests("incoming");
Constant.dismissloader();
}
}
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
}
Adapter.class
public class Incoming_Request_adapter extends AbstractItem<Incoming_Request_adapter, Incoming_Request_adapter.FriendRequest_ViewHolder> {
@SerializedName("userid")
private String to_user_id;
@SerializedName("userprofile")
private String user_profile_pic;
@SerializedName("username")
private String username;
@SerializedName("email")
private String email;
@SerializedName("phone")
private String phone;
@SerializedName("city")
private String city;
@SerializedName("user_status")
private String userstatus;
@SerializedName("friend_status")
private String friend_status;
public String getFriend_status() {
return friend_status;
}
public void setFriend_status(String friend_status) {
this.friend_status = friend_status;
}
public String getTo_user_id() {
return to_user_id;
}
public void setTo_user_id(String to_user_id) {
this.to_user_id = to_user_id;
}
public String getUser_profile_pic() {
return user_profile_pic;
}
public void setUser_profile_pic(String user_profile_pic) {
this.user_profile_pic = user_profile_pic;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getUserstatus() {
return userstatus;
}
public void setUserstatus(String userstatus) {
this.userstatus = userstatus;
}
@Override
public int getType() {
return R.id.incoming_req_recycler_view;
}
@Override
public int getLayoutRes() {
return R.layout.incoming_request_item;
}
@Override
public void bindView(FriendRequest_ViewHolder holder, List<Object> payloads) {
super.bindView(holder, payloads);
holder.frd_req_item_name.setText(username.replace("$", " "));
holder.frd_req_msg.setText(userstatus.replace("$", " "));
Picasso.with(holder.itemView.getContext()).load(user_profile_pic).into(holder.frd_req_img);
if (friend_status.equals("1")) {
holder.linearLayout.setVisibility(View.VISIBLE);
holder.sendrequest.setVisibility(View.GONE);
} else if (friend_status.equals("0")) {
holder.linearLayout.setVisibility(View.GONE);
holder.sendrequest.setVisibility(View.VISIBLE);
}
}
public static class FriendRequest_ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.frd_req_img)
CircleImageView frd_req_img;
@BindView(R.id.frd_req_item_name)
TextView frd_req_item_name;
@BindView(R.id.frd_req_msg)
TextView frd_req_msg;
@BindView(R.id.accept)
FancyButton accept;
@BindView(R.id.cancel)
FancyButton cancel;
@BindView(R.id.linearLayout)
LinearLayout linearLayout;
@BindView(R.id.sendrequest)
FancyButton sendrequest;
@BindView(R.id.incoming_text)
AppCompatTextView incoming_text;
public FriendRequest_ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
据我所知,您使用的是 FastAdapter
的过时版本,它仍然启用了一项功能,可以自动检测所提供的 ViewHolder
。由于此功能有时会导致上述错误消息(如果与 proguard 一起使用),因此在较新的版本中已将其删除并替换为简单的 getViewHolder
方法。
@Override
public ViewHolder getViewHolder(View v) {
return new ViewHolder(v);
}
这种新方法是在 FastAdapter
v2.5.0 中引入的:https://github.com/mikepenz/FastAdapter/blob/develop/MIGRATION.md#v250
对于旧版本的 FastAdapter
,有一个 ViewHolderFactory
的概念,它将(作为自动检测的替代方案,它在您的情况下不起作用)提供 ViewHolder
到 FastAdapter
基本上你会实现 ViewHolderFactory
并在你的项目中定义 ViewHolderFactory
。
private static final ViewHolderFactory<? extends ViewHolder> FACTORY = new ItemFactory();
此外,您还需要:
/**
* our ItemFactory implementation which creates the ViewHolder for our adapter.
* It is highly recommended to implement a ViewHolderFactory as it is 0-1ms faster for ViewHolder creation,
* and it is also many many times more efficient if you define custom listeners on views within your item.
*/
protected static class ItemFactory implements ViewHolderFactory<ViewHolder> {
public ViewHolder create(View v) {
return new ViewHolder(v);
}
}
/**
* return our ViewHolderFactory implementation here
*
* @return
*/
@Override
public ViewHolderFactory<? extends ViewHolder> getFactory() {
return FACTORY;
}
你可以在这里看到全部内容:
https://github.com/mikepenz/FastAdapter/blob/v2.2.0/app/src/main/java/com/mikepenz/fastadapter/app/items/IconItem.java#L24
作为替代方案,调整 proguard 规则也可以。
我的建议是更新到最新的 FastAdapter
版本之一,因为这已大大简化和增强。
嗨,朋友们,我从 2 天起就遇到了这个问题,我不知道这是什么问题,我正在使用 mikepenz 的 Fast Adapter
,我在 this.Here 中有两个回收站视图,我正在这个错误
java.lang.RuntimeException: You have to provide a ViewHolder with a constructor which takes a view!
at com.mikepenz.fastadapter.items.AbstractItem$ReflectionBasedViewHolderFactory.create(AbstractItem.java:349)
我认为我已经将所有设置正确,因为我在其他 class 中也使用相同的格式,但我只面临这个问题 class 而且我没有激活程序,请查找下面的代码
主要Class:
public class Incoming_request extends Fragment {
@BindView(R.id.incoming_req_recycler_view)
RecyclerView incoming_req_recycler_view;
@BindView(R.id.people_recycler_view)
RecyclerView people_recycler_view;
FastItemAdapter<Incoming_Request_adapter> incoming_req_adapter = new FastItemAdapter<>();
FastItemAdapter<People_Request_adapter> people_req_adapter = new FastItemAdapter<>();
@BindView(R.id.frd_req_no)
AppCompatTextView frd_req_no;
@BindView(R.id.namesearch)
AutoCompleteTextView namesearch;
ArrayList<Search_Pojo> searchlist = new ArrayList<>();
Search_Adapter search_adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.incoming_req, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
incoming_req_recycler_view.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
people_recycler_view.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
incoming_requests("");
incoming_req_recycler_view.setAdapter(incoming_req_adapter);
people_recycler_view.setAdapter(people_req_adapter);
search_adapter=new Search_Adapter(getActivity(),R.layout.search_adapter_item,searchlist);
namesearch.setThreshold(1);
namesearch.setAdapter(search_adapter);
search_adapter.notifyDataSetChanged();
namesearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Search_Pojo search = (Search_Pojo) parent.getItemAtPosition(position);
namesearch.setText(search.getUsername());
Intent otherprofileintent = new Intent(getActivity(), Other_Profile.class);
otherprofileintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
otherprofileintent.putExtra("frd_status", search.getFriend_status());
otherprofileintent.putExtra("frd_id", search.getUserid());
startActivity(otherprofileintent);
}
});
people_req_adapter.withOnClickListener(new FastAdapter.OnClickListener<People_Request_adapter>() {
@Override
public boolean onClick(View v, IAdapter<People_Request_adapter> adapter, People_Request_adapter item, int position) {
Intent otherprofileintent = new Intent(getActivity(), Other_Profile.class);
otherprofileintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
otherprofileintent.putExtra("frd_status", item.getFriend_status());
otherprofileintent.putExtra("frd_id", item.getTo_user_id());
startActivity(otherprofileintent);
return true;
}
});
people_req_adapter.withItemEvent(new ClickEventHook<People_Request_adapter>() {
@Override
public void onClick(View v, int position, FastAdapter<People_Request_adapter> fastAdapter, People_Request_adapter item) {
acceptrequest(item.getTo_user_id(), position);
}
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof Incoming_Request_adapter.FriendRequest_ViewHolder) {
return viewHolder.itemView.findViewById(R.id.accept);
}
return null;
}
});
people_req_adapter.withItemEvent(new ClickEventHook<People_Request_adapter>() {
@Override
public void onClick(View v, int position, FastAdapter<People_Request_adapter> fastAdapter, People_Request_adapter item) {
deleterequest(item.getTo_user_id(), position);
}
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof Incoming_Request_adapter.FriendRequest_ViewHolder) {
return viewHolder.itemView.findViewById(R.id.cancel);
}
return null;
}
});
people_req_adapter.withItemEvent(new ClickEventHook<People_Request_adapter>() {
@Override
public void onClick(View v, int position, FastAdapter<People_Request_adapter> fastAdapter, People_Request_adapter item) {
sendrequest(item.getTo_user_id(), position);
}
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof Incoming_Request_adapter.FriendRequest_ViewHolder) {
return viewHolder.itemView.findViewById(R.id.sendrequest);
}
return null;
}
});
incoming_req_adapter.withOnClickListener(new FastAdapter.OnClickListener<Incoming_Request_adapter>() {
@Override
public boolean onClick(View v, IAdapter<Incoming_Request_adapter> adapter, Incoming_Request_adapter item, int position) {
Intent otherprofileintent = new Intent(getActivity(), Other_Profile.class);
otherprofileintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
otherprofileintent.putExtra("frd_status", item.getFriend_status());
otherprofileintent.putExtra("frd_id", item.getTo_user_id());
startActivity(otherprofileintent);
return true;
}
});
incoming_req_adapter.withItemEvent(new ClickEventHook<Incoming_Request_adapter>() {
@Override
public void onClick(View v, int position, FastAdapter<Incoming_Request_adapter> fastAdapter, Incoming_Request_adapter item) {
acceptrequest(item.getTo_user_id(), position);
}
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof Incoming_Request_adapter.FriendRequest_ViewHolder) {
return viewHolder.itemView.findViewById(R.id.accept);
}
return null;
}
});
incoming_req_adapter.withItemEvent(new ClickEventHook<Incoming_Request_adapter>() {
@Override
public void onClick(View v, int position, FastAdapter<Incoming_Request_adapter> fastAdapter, Incoming_Request_adapter item) {
deleterequest(item.getTo_user_id(), position);
}
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof Incoming_Request_adapter.FriendRequest_ViewHolder) {
return viewHolder.itemView.findViewById(R.id.cancel);
}
return null;
}
});
incoming_req_adapter.withItemEvent(new ClickEventHook<Incoming_Request_adapter>() {
@Override
public void onClick(View v, int position, FastAdapter<Incoming_Request_adapter> fastAdapter, Incoming_Request_adapter item) {
sendrequest(item.getTo_user_id(), position);
}
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof Incoming_Request_adapter.FriendRequest_ViewHolder) {
return viewHolder.itemView.findViewById(R.id.sendrequest);
}
return null;
}
});
/*incoming_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadrefreshcomplete = 1;
incoming_req_adapter.clear();
incoming_requests("");
}
});*/
}
private void incoming_requests(String value) {
if (value.isEmpty()) {
Constant.showloader(getActivity());
}
String outgoing_requesturl = Constant.psurl + "myrequests&userid=" + Session.getUserID(getActivity());
Constant.l(outgoing_requesturl);
AndroidNetworking.get(outgoing_requesturl).setOkHttpClient(Constant.okClient()).build().getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.has("friendrequests")) {
JSONArray userlistarray = response.getJSONArray("friendrequests");
for (int i = 0; i < userlistarray.length(); i++) {
JSONObject frdsarrayobj = userlistarray.getJSONObject(i);
Incoming_Request_adapter incomingadapter = new Incoming_Request_adapter();
Search_Pojo search_pojo = new Search_Pojo();
search_pojo.setUserid(frdsarrayobj.getString("userid"));
search_pojo.setUsername(frdsarrayobj.getString("username"));
search_pojo.setEmail(frdsarrayobj.getString("email"));
search_pojo.setPhone(frdsarrayobj.getString("phone"));
search_pojo.setCity(frdsarrayobj.getString("city"));
search_pojo.setUserstatus(frdsarrayobj.getString("user_status"));
search_pojo.setUser_profile_pic(frdsarrayobj.getString("userprofile"));
search_pojo.setFriend_status(frdsarrayobj.getString("friend_status"));
incomingadapter.setTo_user_id(frdsarrayobj.getString("userid"));
incomingadapter.setUsername(frdsarrayobj.getString("username"));
incomingadapter.setEmail(frdsarrayobj.getString("email"));
incomingadapter.setPhone(frdsarrayobj.getString("phone"));
incomingadapter.setCity(frdsarrayobj.getString("city"));
incomingadapter.setUserstatus(frdsarrayobj.getString("user_status"));
incomingadapter.setUser_profile_pic(frdsarrayobj.getString("userprofile"));
incomingadapter.setFriend_status(frdsarrayobj.getString("friend_status"));
incoming_req_adapter.add(incomingadapter);
searchlist.add(search_pojo);
}
} else {
frd_req_no.setVisibility(View.VISIBLE);
incoming_req_recycler_view.setVisibility(View.GONE);
}
if (response.has("userslist")) {
JSONArray frdlistarray = response.getJSONArray("userslist");
for (int j = 0; j < frdlistarray.length(); j++) {
JSONObject userarrayobj = frdlistarray.getJSONObject(j);
Search_Pojo search_pojo = new Search_Pojo();
search_pojo.setUserid(userarrayobj.getString("userid"));
search_pojo.setUsername(userarrayobj.getString("username"));
search_pojo.setEmail(userarrayobj.getString("email"));
search_pojo.setPhone(userarrayobj.getString("phone"));
search_pojo.setCity(userarrayobj.getString("city"));
search_pojo.setUserstatus(userarrayobj.getString("user_status"));
search_pojo.setUser_profile_pic(userarrayobj.getString("userprofile"));
search_pojo.setFriend_status(userarrayobj.getString("friend_status"));
People_Request_adapter peoplgreqadapter = new People_Request_adapter();
peoplgreqadapter.setTo_user_id(userarrayobj.getString("userid"));
peoplgreqadapter.setUsername(userarrayobj.getString("username"));
peoplgreqadapter.setEmail(userarrayobj.getString("email"));
peoplgreqadapter.setPhone(userarrayobj.getString("phone"));
peoplgreqadapter.setCity(userarrayobj.getString("city"));
peoplgreqadapter.setUserstatus(userarrayobj.getString("user_status"));
peoplgreqadapter.setUser_profile_pic(userarrayobj.getString("userprofile"));
peoplgreqadapter.setFriend_status(userarrayobj.getString("friend_status"));
people_req_adapter.add(peoplgreqadapter);
searchlist.add(search_pojo);
}
}
} catch (JSONException e) {
Constant.l(e.toString());
Constant.dismissloader();
}
if (!value.isEmpty()) {
Constant.dismissloader();
}
/*if (loadrefreshcomplete == 1) {
loadrefreshcomplete = 0;
incoming_refresh.setRefreshing(false);
Constant.dismissloader();
}*/
}
@Override
public void onError(ANError anError) {
Constant.l(anError.toString());
Constant.dismissloader();
}
});
}
public void acceptrequest(String fid, final int position) {
Constant.showloader(getActivity());
String sendurl = Constant.psurl + "sendrequest&from_id=" + fid + "&to_id=" + Session.getUserID(getActivity());
Constant.l(sendurl);
AndroidNetworking.get(sendurl).build().getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.getString("status").equals("Success")) {
incoming_req_adapter.remove(position);
}
} catch (JSONException e) {
Constant.l(e.toString());
Constant.dismissloader();
}
Constant.dismissloader();
}
@Override
public void onError(ANError anError) {
Constant.l(anError.toString());
Constant.dismissloader();
}
});
}
public void deleterequest(String fid, final int position) {
Constant.showloader(getActivity());
String sendurl = Constant.psurl + "deleterequest&from_id=" + Session.getUserID(getActivity()) + "&to_id=" + fid;
Constant.l(sendurl);
AndroidNetworking.get(sendurl).build().getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.getString("status").equals("Success")) {
incoming_req_adapter.remove(position);
}
} catch (JSONException e) {
Constant.l(e.toString());
Constant.dismissloader();
}
Constant.dismissloader();
}
@Override
public void onError(ANError anError) {
Constant.l(anError.toString());
Constant.dismissloader();
}
});
}
public void sendrequest(String fid, final int position) {
Constant.showloader(getActivity());
String sendurl = Constant.psurl + "sendrequest&from_id=" + Session.getUserID(getActivity()) + "&to_id=" + fid;
Constant.l(sendurl);
AndroidNetworking.get(sendurl).build().getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.getString("status").equals("Success")) {
incoming_req_adapter.remove(position);
EventBus.getDefault().post(new Event_Bus("outgoing"));
}
} catch (JSONException e) {
Constant.l(e.toString());
Constant.dismissloader();
}
Constant.dismissloader();
}
@Override
public void onError(ANError anError) {
Constant.l(anError.toString());
Constant.dismissloader();
}
});
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void groupcreated(Event_Bus eventBus) {
if (eventBus.getEventname().equals("incoming")) {
incoming_req_adapter.clear();
incoming_requests("incoming");
Constant.dismissloader();
}
}
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
}
Adapter.class
public class Incoming_Request_adapter extends AbstractItem<Incoming_Request_adapter, Incoming_Request_adapter.FriendRequest_ViewHolder> {
@SerializedName("userid")
private String to_user_id;
@SerializedName("userprofile")
private String user_profile_pic;
@SerializedName("username")
private String username;
@SerializedName("email")
private String email;
@SerializedName("phone")
private String phone;
@SerializedName("city")
private String city;
@SerializedName("user_status")
private String userstatus;
@SerializedName("friend_status")
private String friend_status;
public String getFriend_status() {
return friend_status;
}
public void setFriend_status(String friend_status) {
this.friend_status = friend_status;
}
public String getTo_user_id() {
return to_user_id;
}
public void setTo_user_id(String to_user_id) {
this.to_user_id = to_user_id;
}
public String getUser_profile_pic() {
return user_profile_pic;
}
public void setUser_profile_pic(String user_profile_pic) {
this.user_profile_pic = user_profile_pic;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getUserstatus() {
return userstatus;
}
public void setUserstatus(String userstatus) {
this.userstatus = userstatus;
}
@Override
public int getType() {
return R.id.incoming_req_recycler_view;
}
@Override
public int getLayoutRes() {
return R.layout.incoming_request_item;
}
@Override
public void bindView(FriendRequest_ViewHolder holder, List<Object> payloads) {
super.bindView(holder, payloads);
holder.frd_req_item_name.setText(username.replace("$", " "));
holder.frd_req_msg.setText(userstatus.replace("$", " "));
Picasso.with(holder.itemView.getContext()).load(user_profile_pic).into(holder.frd_req_img);
if (friend_status.equals("1")) {
holder.linearLayout.setVisibility(View.VISIBLE);
holder.sendrequest.setVisibility(View.GONE);
} else if (friend_status.equals("0")) {
holder.linearLayout.setVisibility(View.GONE);
holder.sendrequest.setVisibility(View.VISIBLE);
}
}
public static class FriendRequest_ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.frd_req_img)
CircleImageView frd_req_img;
@BindView(R.id.frd_req_item_name)
TextView frd_req_item_name;
@BindView(R.id.frd_req_msg)
TextView frd_req_msg;
@BindView(R.id.accept)
FancyButton accept;
@BindView(R.id.cancel)
FancyButton cancel;
@BindView(R.id.linearLayout)
LinearLayout linearLayout;
@BindView(R.id.sendrequest)
FancyButton sendrequest;
@BindView(R.id.incoming_text)
AppCompatTextView incoming_text;
public FriendRequest_ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
据我所知,您使用的是 FastAdapter
的过时版本,它仍然启用了一项功能,可以自动检测所提供的 ViewHolder
。由于此功能有时会导致上述错误消息(如果与 proguard 一起使用),因此在较新的版本中已将其删除并替换为简单的 getViewHolder
方法。
@Override
public ViewHolder getViewHolder(View v) {
return new ViewHolder(v);
}
这种新方法是在 FastAdapter
v2.5.0 中引入的:https://github.com/mikepenz/FastAdapter/blob/develop/MIGRATION.md#v250
对于旧版本的 FastAdapter
,有一个 ViewHolderFactory
的概念,它将(作为自动检测的替代方案,它在您的情况下不起作用)提供 ViewHolder
到 FastAdapter
基本上你会实现 ViewHolderFactory
并在你的项目中定义 ViewHolderFactory
。
private static final ViewHolderFactory<? extends ViewHolder> FACTORY = new ItemFactory();
此外,您还需要:
/**
* our ItemFactory implementation which creates the ViewHolder for our adapter.
* It is highly recommended to implement a ViewHolderFactory as it is 0-1ms faster for ViewHolder creation,
* and it is also many many times more efficient if you define custom listeners on views within your item.
*/
protected static class ItemFactory implements ViewHolderFactory<ViewHolder> {
public ViewHolder create(View v) {
return new ViewHolder(v);
}
}
/**
* return our ViewHolderFactory implementation here
*
* @return
*/
@Override
public ViewHolderFactory<? extends ViewHolder> getFactory() {
return FACTORY;
}
你可以在这里看到全部内容: https://github.com/mikepenz/FastAdapter/blob/v2.2.0/app/src/main/java/com/mikepenz/fastadapter/app/items/IconItem.java#L24
作为替代方案,调整 proguard 规则也可以。
我的建议是更新到最新的 FastAdapter
版本之一,因为这已大大简化和增强。