Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the bug that apdapter delay to call setOnItemClickListener() that… #2708

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

w-g-b
Copy link

@w-g-b w-g-b commented Apr 18, 2019

Some codes like this:

        //MyAdpater extent BaseQuickAdapter
        MyAdapter adapter = new MyAdapter(R.layout.item, list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                adapter.setOnItemClickListener((adapter12, view, position) -> Log.d("ApplicantActivity", list.get(position)));
                adapter.setOnItemLongClickListener((adapter1, view, position) -> {
                    Toast.makeText(ApplicantActivity.this, "long click: " + list.get(position), Toast.LENGTH_SHORT).show();
                    return true;
                });

            }
        }).start();

In this codes, the item click event will failure because item view didn't execute the setOnClickListener() function.
The codes are no meaning. But the bug will appear if the recycleview have loaded end that adpater call setOnItemClickListener().
I modify the logic of bindViewClickListener() function to solve this problem.

@AllenCoder
Copy link
Collaborator

this idea is good ,but in some case , someone wan't use setOnItemClickListener , they want to custom Touch event , we setOnClickListener on any case ,that is Incompatible,what should we do ?

@w-g-b
Copy link
Author

w-g-b commented Apr 19, 2019

Yes, you are right. Developer prefers to use addOnClickListener() through BaseViewHolder.
Others, setOnClickListener will make view clickable even it do nothing.
I think another way to solve this problem is store the viewhold in onCreateViewHolder() and when setOnItemClickListener, foreach and add the listener, like this:

    private List<K> mViewHolders;
    public BaseQuickAdapter(@LayoutRes int layoutResId, @Nullable List<T> data) {
        this.mData = data == null ? new ArrayList<T>() : data;
        if (layoutResId != 0) {
            this.mLayoutResId = layoutResId;
        }
        mViewHolders = new ArrayList<>();
    }
    @Override
    public K onCreateViewHolder(ViewGroup parent, int viewType) {
            ...
            default:
                baseViewHolder = onCreateDefViewHolder(parent, viewType);
                bindViewClickListener(baseViewHolder);
                mViewHolders.add(baseViewHolder);
           ...     
    }
    public void setOnItemClickListener(@Nullable OnItemClickListener listener) {
        mOnItemClickListener = listener;
        for (BaseViewHolder b : mViewHolders) {
            bindViewClickListener(b);
        }
    }
    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        mOnItemLongClickListener = listener;
        for (BaseViewHolder viewHolder : mViewHolders) {
            bindViewClickListener(viewHolder);
        }
    }

Could it feasible?

@w-g-b
Copy link
Author

w-g-b commented Apr 20, 2019

Add the judge to avoid null pointer exception

    private void bindViewClickListener(final BaseViewHolder baseViewHolder) {
        ...
        if (getOnItemClickListener() != null) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getOnItemClickListener() != null) {
                        setOnItemClick(v, baseViewHolder.getLayoutPosition() - getHeaderLayoutCount());
                    }
                }
            });
        }
        if (getOnItemLongClickListener() != null) {
            view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (getOnItemLongClickListener() != null)
                        return setOnItemLongClick(v, baseViewHolder.getLayoutPosition() - getHeaderLayoutCount());
                    return false;
                }
            });
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants