禁用RecyclerView按钮

时间:2018-09-27 09:36:18

标签: android recyclerview-layout

RecyclerView layout

enter image description here

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view=inflater.inflate(R.layout.container_fish, viewGroup,false);
    MyHolder holder=new MyHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {

    final MyHolder myHolder= (MyHolder) viewHolder;
    final DataFish current=data.get(i);

    myHolder.textFishName.setText(current.fishName);
    myHolder.textSize.setText("Size: " + current.sizeName);
    myHolder.textType.setText("Category: " + current.catName);
    myHolder.btn.setOnClickListener( new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.N)
        @Override
        public void onClick(View v) {

          //code here

        }
    } );

如果在视图中单击Button,则应禁用其视图中的所有其他按钮。这就是我的情况,任何人都可以建议该怎么做? 在此先感谢

2 个答案:

答案 0 :(得分:0)

在适配器中声明一个公共布尔变量,并将其默认值设置为false;

public boolean clicked = false

onBindViewHolder内检查if (clicked) myHolder.btn.setEnabled(false); else myHolder.btn.setEnabled(true)

,然后在您的点击方法中设置clicked = true并调用notifyDataSetChanged()

这将禁用所有按钮,

如果要禁用所有其他按钮(此按钮除外),请使用由布尔值public Integer clickedPosition = null插入的整数变量

并检查if (clickedPosition != null && clickedPosition != getAdapterPosition) myHolder.btn.setEnabled(false); else myHolder.btn.setEnabled(true);

,然后在您的点击方法中设置clickedPosition = getAdapterPosition并调用notifyDataSetChanged()

public  Integer clickedPosition;

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view=inflater.inflate(R.layout.container_fish, viewGroup,false);
    MyHolder holder=new MyHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {

    final MyHolder myHolder= (MyHolder) viewHolder;
    final DataFish current=data.get(i);

    myHolder.textFishName.setText(current.fishName);
    myHolder.textSize.setText("Size: " + current.sizeName);
    myHolder.textType.setText("Category: " + current.catName);
    if(clickedPosition!=null && clickedPosition!=getAdapterPosition()) myHolder.btn.setEnabled(false);
    else myHolder.btn.setEnabled(false);
    myHolder.btn.setOnClickListener( new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.N)
        @Override
        public void onClick(View v) {
            clickedPosition = getAdapterPosition();
            notifyDatasetChanged();
          //code here

        }
    } );

 myHolder.btn1.setOnClickListener( new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {

                //code here

            }
        } );
        myHolder.btn2.setOnClickListener( new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {
            clickedPosition = null;
            notifyDatasetChanged();
           //code here

            }
        } );

答案 1 :(得分:0)

遍历recyclerview项并禁用每个按钮,但禁用单击的按钮,如下所示:(在我的情况下,recycler视图是一个公共静态属性)

//exempleActivity.java

public static RecyclerView yourRecyclerView;
...
yourRecyclerView = view.findViewById(R.id.your_recycleriew);

//myRecyclerViewAdapter.java

import yourPackage.yourActivity.yourRecyclerView;
for (int j = 0; j < yourRecyclerView.getChildCount(); j++) {
     YourViewHolder holder = (YourViewHolder) yourRecyclerView.findViewHolderForAdapterPosition(j);
     if(i != currentPosition)
          holder.btn.setEnabled(false);
}