我正在构建一个RecyclerView
来显示我的数组列表项,我确实做了点击监听器,但是当我实现了onClick
方法时,我无法从那里更新我的视图。|这里是我的RecyclerView
适配器
public class FeedRecyclerAdapter extends RecyclerView.Adapter<FeedRecyclerAdapter.ViewHolder> {
public static ArrayList<FeedObject> feedObjectArrayList;
@Override
public FeedRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.feeditemlayout, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
v.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
return viewHolder;
}
@Override
public void onBindViewHolder(final FeedRecyclerAdapter.ViewHolder holder, int position) {
final FeedObject feedObject = feedObjectArrayList.get(position);
holder.likeFeedItemTV.setText(feedObject.getNumAgree() + "");
holder.dislikeFeedItemTV.setText(feedObject.getNumDisAgree() + "");
holder.commentFeedItemTV.setText(feedObject.getNumComments() + "");
holder.mediaFeedItemTV.setText(feedObject.getMediaSize() + "");
}
@Override
public int getItemCount() {
return feedObjectArrayList.size();
}
public FeedRecyclerAdapter(ArrayList<FeedObject> feedObjectArrayList) {
this.feedObjectArrayList = feedObjectArrayList;
}
public static class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
private ImageView likeFeedItem, profileImageView, likeFeedItemIV, dislikeFeedItem, commentFeedItem, mediaFeedItem;
private TextView personName, personDetails, cat_Info_Feed_Item, descriptionFeedItemTV, likeFeedItemTV, dislikeFeedItemTV, commentFeedItemTV, mediaFeedItemTV, agreeFeed, disagreeFeed, commentFeed, mediaFeed;
private ImageButton moreOption;
private Button seeMore;
public ViewHolder(View itemView) {
super(itemView);
likeFeedItemTV = (TextView) itemView.findViewById(R.id.likeFeedItemTV);
dislikeFeedItemTV = (TextView) itemView.findViewById(R.id.dislikeFeedItemTV);
commentFeedItemTV = (TextView) itemView.findViewById(R.id.commnetFeedItemTV);
mediaFeedItemTV = (TextView) itemView.findViewById(R.id.mediaFeedItemTV);
likeFeedItem = (ImageView) itemView.findViewById(likeFeedItemIB);
dislikeFeedItem = (ImageView) itemView.findViewById(dislikeFeedItemIV);
commentFeedItem = (ImageView) itemView.findViewById(commentFeedItemIV);
mediaFeedItem = (ImageView) itemView.findViewById(mediaFeedItemIV);
agreeFeed = (TextView) itemView.findViewById(agreeFeedTV);
disagreeFeed = (TextView) itemView.findViewById(disagreeFeedTV);
commentFeed = (TextView) itemView.findViewById(commentFeedTV);
mediaFeed = (TextView) itemView.findViewById(commentFeedTV);
likeFeedItem.setOnClickListener(this);
dislikeFeedItem.setOnClickListener(this);
commentFeedItem.setOnClickListener(this);
mediaFeedItem.setOnClickListener(this);
agreeFeed.setOnClickListener(this);
disagreeFeed.setOnClickListener(this);
commentFeed.setOnClickListener(this);
mediaFeed.setOnClickListener(this);
seeMore = (Button) itemView.findViewById(seeMoreFeedItemTV);
moreOption = (ImageButton) itemView.findViewById(moreFeedItemIB);
seeMore.setOnClickListener(this);
moreOption.setOnClickListener(this);
itemView.setOnClickListener(this);
}
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
default:
return false;
}
}
@Override
public void onClick(View v) {
final FeedObject feedObject = feedObjectArrayList.get(getAdapterPosition());
switch (v.getId()) {
case moreFeedItemIB:
PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
MenuInflater menuInflater = popupMenu.getMenuInflater();
menuInflater.inflate(R.menu.feeditemmenu, popupMenu.getMenu());
popupMenu.show();
break;
case likeFeedItemIB:
case agreeFeedTV:
new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
Boolean b=feedObject.isAgree();
if (b != null ) {
if (b) {
feedObject.setAgree(null);
feedObject.setNumAgree(feedObject.getNumAgree() - 1);
} else {
feedObject.setAgree(true);
feedObject.setNumAgree(feedObject.getNumAgree() + 1);
feedObject.setNumDisAgree(feedObject.getNumDisAgree() - 1);
}
} else {
feedObject.setAgree(true);
feedObject.setNumAgree(feedObject.getNumAgree() + 1);
}
return null;
}
}.execute();
//here is my problem i can't update my view
break;
case dislikeFeedItemIV:
case disagreeFeedTV:
new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
Boolean c=feedObject.isAgree();
if (c != null ) {
if (c) {
feedObject.setAgree(false);
feedObject.setNumAgree(feedObject.getNumAgree() - 1);
feedObject.setNumDisAgree(feedObject.getNumDisAgree()+ 1);
} else {
feedObject.setAgree(null);
feedObject.setNumAgree(feedObject.getNumAgree() - 1);
}
} else {
feedObject.setAgree(false);
feedObject.setNumDisAgree(feedObject.getNumDisAgree() + 1);
}
return null;
}
}.execute();
break;
case seeMoreFeedItemTV:
case commentFeedItemIV:
case commentFeedTV:
v.getContext().startActivity( new Intent(v.getContext(),DetailedFeed.class));
break;
case mediaFeedItemIV:
case mediaFeedTV:
break;
}
}
}
}
答案 0 :(得分:1)
您是否有理由将ViewHolder设为静态?如果删除静态,则可以从适配器notifyItemChanged(getAdapterPosition());
内进行调用以更新视图。然后,通过删除ViewHolder上的静态,您也可以从feedObjectArrayList
删除它。
如果要保持静态,可以在适配器中创建一个实现OnClickListener
的新类,并将其传递给onBindViewHolder
中的ViewHolder。
作为旁注,我不得不说onClick方法是一个可读性的噩梦,我会亲自将它分解为每个案例的单独类。