【问题标题】:Single Selection item with Recycler Grid View [duplicate]带有 Recycler Grid View 的单选项目 [重复]
【发布时间】:2019-10-15 05:08:16
【问题描述】:

我有一个问题,我用Recycler Gridview 一次选择一个项目,当我点击另一个项目时,最后一个项目应该被取消选择

【问题讨论】:

  • 请分享您的代码
  • 你可以对物品使用模型类吗?如果是,则显示它
  • 不,我没有使用模型类@Niceumang
  • 然后你可以使用布尔标志处理这个我的意思是用真假值处理它
  • 如果可能的话,使用模型类太容易处理了!

标签: android gridview


【解决方案1】:

这是满足您要求的解决方案

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.ViewHolder> {
        private int selected_position = -1;

        @Override
        public void onBindViewHolder(AdapterClass.ViewHolder holder, final int position) {
            if (selected_position == position) {
                // do your stuff here like
                //Change selected item background color and Show sub item views

            } else {
                  // do your stuff here like
                  //Change  unselected item background color and Hide sub item views
            }
  // rest of the code here

    holder.linelayout.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if(selected_position==position){
                        selected_position=-1;
                        notifyDataSetChanged();
                        return;
                    }
                    selected_position = position;
                    notifyDataSetChanged();

            }
        });

    //rest of the code here

     }


}

【讨论】:

  • 谢谢,它对我有用。
【解决方案2】:

创建一个变量:

private var lastSub: View? = null

在你的 recyclerview 项目的 onClickListener 上使用如下:

override fun onClick(view: View) {
   //val subjects = view.tag as Subject
   //subId = subjects.id
   if (lastSub != view && lastSub != null) {
     lastSub!!.background = ResourcesCompat.getDrawable(resources, R.color.white, null)
     view.background = ResourcesCompat.getDrawable(resources, R.drawable.card_border, null)
     lastSub = view
   } else {
     view.background = ResourcesCompat.getDrawable(resources, R.drawable.card_border, null)
     lastSub = view
   }


}

【讨论】:

    【解决方案3】:
                class AdapterClass extends RecyclerView.Adapter<AdapterClass.ViewHolder> {
                 private int currentPosition = -1;
                    private int previousPosition = -1;
                 @Override
                    public void onBindViewHolder(AdapterClass.ViewHolder holder, final int position) {
        holder.currentPos = position;
    
    
                if(currentPosition == -1) {
                        previousPosition = position;
                    } else {
    
                    }
                } else {
                    if(previousPosition == position) {
    
                    }
                    if(currentPosition == position) {
                        previousPosition = currentPosition;
                    } else {
    
                    }
                }
            }
                }
    
    // in view holder onClick you can notify previous and current
    itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        notifyItemChanged(previousPosition);
                        currentPosition = currentPos;
                        notifyItemChanged(currentPosition);
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多