就像不像按钮回收器一样
我有一个Android应用,用户可以在其中喜欢和喜欢图像。我使用的是recyclerView。我只需在用户点击后禁用按钮(像/不喜欢)。问题,当我单击按钮like时,应用程序转到主要活动,并且按钮Like不会更改为与我所做的不同:
I have an android app in which users can like and unlike an image.I'm using recyclerView.I Just disable the button(Like/Unlike) once user clicked. Problem, when I click on button like , the apps go to main activity and the button Like doesn't change to unlike What I have done :
1 ) layout that holds the each recycler view layout item
2 ) A view holder for creating each layout
3 ) A Model Class to holds the data
4 ) Recycler Adaptor which deals with the data for the Each Layout item
//Initializing Views
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
//textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
likeImageView = (ImageView) itemView.findViewById(R.id.likeImageView);
likeImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = (int)likeImageView.getTag();
if( id == R.drawable.ic_like){
likeImageView.setTag(R.drawable.ic_liked);
likeImageView.setImageResource(R.drawable.ic_liked);
}else{
likeImageView.setTag(R.drawable.ic_like);
likeImageView.setImageResource(R.drawable.ic_like);
}
}
});
我一遍又一遍地回答了这类问题。您没有进行足够的搜索,也不了解ListView或RecycleView的工作方式。更改视图的当前状态(例如更改TextView的文本或更改ImageView的资源)是错误的事情。您需要数据集(与ListView中的项目有关的列表),并且需要更改列表中的相应数据并调用适配器的notifyDataSetChanged()方法。
I answered this type of questions over and over. You didn't search enough and you didn't understand how ListView or RecycleView works. Changing current state of views ( such as changing text of TextView or changing resource of ImageView) is the wrong thing. You need data set (a list related to items in ListView) and you need to change corresponding data of the list and call notifyDataSetChanged() method of your adapter.
Don'不要忘记。每当列表的任何视图出现在屏幕上时,都会调用适配器的getView()方法,并且如果仅更新视图(而不是更改数据),则视图将显示item的过去值,因为数据没有更改。
Don't forget. getView() method of your adapter is called every time any view of your list become on the screen and if you update only the view (instead of change data) your view will show the past value of item because your data didn't changed.
查看下面的链接,搜索有关ListView和RecycleView工作原理的更多信息。
Look link below and search much more about how ListView and RecycleView works.