RecyclerView图标是无状态的,当转到其他活动时,它将返回默认状态

时间:2016-05-24 06:01:28

标签: android

我的

的每一行都有一个ImageView(带有边框心形图标)

recyclerview。我使用此图标添加到收藏夹列表。当我按下这个

图片查看它更改为其他图标(完整的心形图标)。一切都好 ,但是当我去其他地方时

活动它返回默认图标(边框心形图标)。我用旗帜做这项工作。

  

这是我的RecyclerView适配器(图片onClick事件):

   //============== IMG ADD TO FAVORITE CLICK LISTENER ======================
        holder.imgAddFav.setOnClickListener(new View.OnClickListener() {
            boolean flag = false;

            @Override
            public void onClick(View v) {

                QuestionDatabaseAdapter databaseAdapter = new QuestionDatabaseAdapter(v.getContext());

                if (!flag) {

                    ModelQuestion question = new ModelQuestion();


                    question.setQuestionTitle(questionha.get(position).getQuestionTitle());
                    question.setQuestionDesc(questionha.get(position).getQuestionDesc());
                    question.setQuestionDate(questionha.get(position).getQuestionDate());
                    question.setQuestionAuthorName(questionha.get(position).getQuestionAuthorName());
                    question.setQuestionAuthorPic(questionha.get(position).getQuestionAuthorPic());
                    question.setQuestionDownLink(questionha.get(position).getQuestionDownLink());

                    databaseAdapter.saveQuestion(question);

                    Toast.makeText(v.getContext(), "Added !", Toast.LENGTH_SHORT).show();
                    holder.imgAddFav.setImageResource(R.drawable.ic_favorite_red_700_24dp);
                    flag = true;
                } else {
                    Toast.makeText(v.getContext(), "Removed !", Toast.LENGTH_SHORT).show();
                    holder.imgAddFav.setImageResource(R.drawable.ic_favorite_border_red_a700_24dp);
                    flag = false;
                }
            }
        });

    }

这是我的偶像。 enter image description here

2 个答案:

答案 0 :(得分:1)

要维护每个项目的状态,在模型类中为ex:" isClicked"创建一个布尔值,默认情况下将其设为false,当他在适配器onclick中单击使其成为true时,你需要基于此显示项目" isClicked"属性。

if(isClicked){
//show filled heart

 }else{
 //show empty heart

}

答案 1 :(得分:0)

它将返回holder.imageView.setImageResource();的默认图标 要将此商店添加到收藏夹列表中,然后将图片置于正确的位置。

//this is a simple code to check whather its in favourite
//itemMap is a static HashMap<Integer,Boolean>
// make a statement like isFavorite=itemMap !=null && itemMap.get(new Integer(position) to be used in if else statement
//use the hashmap for current use. If hasgmap is null retirieve it from a database.
//you should probably do that at the constructor of the adapter
if(isInFavourites){
   holder.imageView.setImageResource(R.drawable.favouriteImage);
}else{
   holder.imageView.setImageResource(R.drawable.addFavImage);
}

使用新线程将最喜欢的项目存储在哈希映射和数据库中的onClick上总是很好的做法,以防止可能的延迟。

按要求说明

  • 您的回收视图适配器中有onBIndViewHolder()方法,它包含用于添加收藏边框图像的方法holder.imgAddFav.setImageResource()
  • 只要创建了recylcle视图项并且结果设置了边框图像,适配器就会调用该函数。
  • 因此,在imgAddFav onClick上,您将新添加的项目保存到您已声明为实例变量的静态hashMap中的收藏夹。

    //consider the hashMap as favMap;
    HashMap<Integer,Boolean> favMap;
    
    //at the constructor of the recylcer view you've to check whether you have items that you previously added to the favorite list
    public YourAdapter(/*parameters*/){
       if(favMap==null){
          favMap=new HashMap<>();
       }
       //now fill the map with stored data
       //You can use sharedpreferences or a datase
       //for an example think that postio 2 is in favorites;
       favMap.put(new Integer(2),true);
    }
    
  • 现在回到getItemHolder()。在设置imgAddFav图像之前,先从hashmap检查特定位置是否在收藏夹中。

        //set a boolean to check whether this item is in favorites
        boolean isInFavorites;
        if(favMap.contains(new Integer(position)) && favMap.get(new Integer(position){
          //the item is in the favourite list
          //set the full colored image
          holder.imgAddFav.setImageResource(R.id.coloredHeart);
          isInFavorites=true;
       }else{
          holder.imgAddFav.setImageResource(R.id.borderedHeart);
          isInFavorites=false;
       }
    
  • 现在在onclick上不仅可以更改图像,还可以存储结果

    holder.imgAddFav.setOnClickListener(new View.OnClickListener{
         @Overrride
         public void onClick(View v){
            if(isInFavorites){
               favMap.put(new Integer(position),false);
               //save this same thing in a database or sharedPreference.Do it in a new thread.
               //if you want to save to a database
               new Thread(new Runnable{
                   @Override
                   public void run(){
                      //insert into database
                   }
               }).start();
              //or else if you want save to sharedPreference you can use edit.apply() method
              //now change the image or simply call notify datasetChanged
              notifyDatasetChanged();  
            }
         }
    });