RecyclerView Adapter:为什么onLongClick方法与onClick冲突?

时间:2016-08-01 01:31:12

标签: android onclick android-recyclerview

在我的RecyclerView适配器文件中,我在onCreateViewHolder方法中设置了OnClickListener。它工作正常,即在单击RecyclerView列表中的项目时启动新活动。我正在尝试为将运行不同方法的项目上的LongClick添加OnLongClickListener。

当我添加下面显示的setOnLongClickListener部分和onLongClick方法时,RecyclerView列表中的项目不能正确显示,因此视图中的某些内容必须不正确。我在这里缺少什么?

适配器文件:

public class ContactListAdapter extends RecyclerView.Adapter<ContactListAdapter.ContactHolder>{

    private RecyclerItemClickListener recyclerItemClickListener;
    ...
    @Override
    public ContactHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_contact_item, parent, false);

    final ContactHolder contactHolder = new ContactHolder(view);

    contactHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int adapterPos = contactHolder.getAdapterPosition();
            if (adapterPos != RecyclerView.NO_POSITION) {
                if (recyclerItemClickListener != null) {
                    recyclerItemClickListener.onItemClick(adapterPos, contactHolder.itemView);
                }
            }
        }
    });

    contactHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            int adapterPos = contactHolder.getAdapterPosition();
            if (adapterPos != RecyclerView.NO_POSITION) {
                if (recyclerItemClickListener != null) {
                    recyclerItemClickListener.onItemLongClick(adapterPos, contactHolder.itemView);
                }
            }
            return false;
        }
    });

    return contactHolder;
}

public void setOnItemClickListener(RecyclerItemClickListener recyclerItemClickListener) {
    this.recyclerItemClickListener = recyclerItemClickListener;
}

监听器文件:

public interface RecyclerItemClickListener {
    void onItemClick(int position, View view);
    void onItemLongClick(int position, View v);
}

活动文件:

...
@Override
public void onItemClick(int position, View view) {
    ActActivity.start(this, contactListAdapter.getItem(position));
}

@Override
public void onItemLongClick(int position, View v) {
    Toast toast = Toast.makeText(this, "Test", Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.show();
}

1 个答案:

答案 0 :(得分:0)

在bindViewHolder方法中实现onClickListener和onLongClickListener,或者可选择在定义的视图持有者类中实现它。它应该解决你的问题。

    public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

           public CustomViewHolder(View itemView) {
                super(itemView);

                itemView.setOnLongClickListener(this);
                itemView.setOnClickListener(this);
            }

            @Override
            public boolean onLongClick(View view) {

                int adapterPos = getAdapterPosition();
            if (adapterPos != RecyclerView.NO_POSITION) {
                if (recyclerItemClickListener != null) {
                    recyclerItemClickListener.onItemLongClick(adapterPos, contactHolder.itemView);
                }
            }
                return false;
            }

            @Override
            public void onClick(View view) {

               int adapterPos = getAdapterPosition();
            if (adapterPos != RecyclerView.NO_POSITION) {
                if (recyclerItemClickListener != null) {
                    recyclerItemClickListener.onItemClick(adapterPos, contactHolder.itemView);
                }
            }
                }
            }
        }