android 如何使RecyclerView的项目不可点击?

zu0ti5jz  于 2022-11-20  发布在  Android
关注(0)|答案(5)|浏览(424)

我尝试了以下代码:

view.setEnabled(false);
myReqRecyclerView.setEnabled(false);

第一行禁用当前单击的视图。单击一次后,我想将所有项目设置为不可单击,这样就不会发生双击。第二行似乎不起作用。

brccelvz

brccelvz1#

第一个简单选项

但实际上使用系统时间会很糟糕:

long mLastClickTime = 0;

    youradapter.setOnItemClickListener(new youradapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {

            if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
                return;
            }
            mLastClickTime = SystemClock.elapsedRealtime();

            ....//your code

        }
    });

但是对于干净的编程,我建议这样做:
"有点复杂,但同样简单"
在你的Adapter中引入一个布尔变量,请注意viewHolder()和onBindViewHolder()方法:

private boolean clickable = true;

@Override
public void onBindViewHolder(@NonNull myAdapter.ViewHolder viewHolder, int position) {

    if (!clickable){
        viewHolder.cardView.setClickable(false);
    } else {
        viewHolder.cardView.setClickable(true);
    }

    //....
}

public void setClickable(boolean clickable) {
    this.clickable = clickable;
    myadapter.notifyDataSetChanged();
}

public static class ViewHolder extends RecyclerView.ViewHolder {

    CardView cardView;

    public ViewHolder(View itemView, final myViewHolder.OnItemClickListener listener){
        super(itemView);
        //..

        cardView = itemView.findViewById(R.id.cardView);

        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        listener.onItemClick(position);
                    }
                }
            }
        });
    }
}

并根据需要使用myadapter.setClickable(状态)
就像这样:

youradapter.setOnItemClickListener(new youradapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {

            myadapter.setClickable(false);

            ....//your code

        }
    });

或者在onResume()中:

myadapter.setClickable(true);
a64a0gku

a64a0gku2#

您可以通过覆盖下面的方法并返回true来防止对viewGroup子项的触摸;

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return true;
}
c3frrgcw

c3frrgcw3#

(false);在您不想单击的视图上

jum4pzuy

jum4pzuy4#

在recyclerView适配器中,您将使用如下所示的方法:

@Override
public YourViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View partnerTypeView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
    return new YourViewHolder(partnerTypeView);
}

在其中添加以下行:

@Override
    public YourViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View partnerTypeView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
        partnerTypeView.setClickable(false);//This is line that make row non clickable
        return new YourViewHolder(partnerTypeView);
    }

如果此方法不起作用,请在下面进行注解

0yg35tkg

0yg35tkg5#

让列表项从适配器代码中的父项获取enabled参数:

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
    view.setEnabled(parent.isEnabled());
    return new MyViewHolder(view);
}

然后,您所要做的就是设置列表本身的值,并再次设置适配器:

recyclerView.setEnabled(false);
recyclerView.setAdapter(adapter);

相关问题