美文网首页
RecycleView多布局、单布局实现外部点击事件操作

RecycleView多布局、单布局实现外部点击事件操作

作者: 为自己代颜_ | 来源:发表于2020-03-09 16:10 被阅读0次

    前提使用此库继承里面的Adapter方式

    CymChad/BaseRecyclerViewAdapterHelper

    Add OnItemClickLister

    Use it item click ( Currently ,we support two way to setting click event ,you can choose any one )

    image

    一 adapter.setOnItemClickListener(listener)

    Use it item click

    1.  adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Log.d(TAG, "onItemClick: ");
                    Toast.makeText(ItemClickActivity.this, "onItemClick" + position, Toast.LENGTH_SHORT).show();
                }
            });
    
    

    Use it item long click

     2. adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                    Log.d(TAG, "onItemLongClick: ");
                    Toast.makeText(ItemClickActivity.this, "onItemLongClick" + position, Toast.LENGTH_SHORT).show();
                    return false;
                }
            });
    

    Use it item child click

    3.- first register child view id in adapter

     @Override
        protected void convert(BaseViewHolder viewHolder, Status item) {
            viewHolder.setText(R.id.tweetName, item.getUserName())
                    .setText(R.id.tweetText, item.getText())
                    .setText(R.id.tweetDate, item.getCreatedAt())
                    .setVisible(R.id.tweetRT, item.isRetweet())
                    .addOnClickListener(R.id.tweetAvatar)
                    .addOnClickListener(R.id.tweetName)
                    .linkify(R.id.tweetText);
    
        }
    
     adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public boolean onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    Log.d(TAG, "onItemChildClick: ");
                    Toast.makeText(ItemClickActivity.this, "onItemChildClick" + position, Toast.LENGTH_SHORT).show();
                    return false;
                }
            });
    

    Use it item child long click

      • first register child view id in adapter
     @Override
        protected void convert(BaseViewHolder helper, Status item) {
            helper.setText(R.id.tweetName, item.getUserName())
                    .setText(R.id.tweetText, item.getText())
                    .setText(R.id.tweetDate, item.getCreatedAt())
                    .setVisible(R.id.tweetRT, item.isRetweet())
                    .addOnLongClickListener(R.id.tweetText)
                    .linkify(R.id.tweetText);
    
        }
    
    
      adapter.setOnItemChildLongClickListener(new BaseQuickAdapter.OnItemChildLongClickListener() {
                @Override
                public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
                    Log.d(TAG, "onItemChildLongClick: ");
                    Toast.makeText(ItemClickActivity.this, "onItemChildLongClick" + position, Toast.LENGTH_SHORT).show();
                }
            });
    

    If you do not want to use the above method ,you can use other way, we alse support the recyclerView.addOnItemTouchListener( listener )


    二 mRecyclerView.addOnItemTouchListener ( listener)

    mRecyclerView.addOnItemTouchListener(new OnItemClickListener( ){
    
                @Override
                public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
    
                }
            });
    

    Use it item long click

     mRecyclerView.addOnItemTouchListener(new OnItemLongClickListener( ) {
                @Override
                public void onSimpleItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                    Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
    
                }
            });
    

    Use it item child click

    • first you should register child view id in adapter
     @Override
        protected void convert(BaseViewHolder viewHolder, Status item) {
            viewHolder.setText(R.id.tweetName, item.getUserName())
                    .setText(R.id.tweetText, item.getText())
                    .setText(R.id.tweetDate, item.getCreatedAt())
                    .setVisible(R.id.tweetRT, item.isRetweet())
                    .addOnClickListener(R.id.tweetAvatar)
                    .addOnClickListener(R.id.tweetName)
                    .linkify(R.id.tweetText);
    
        }
    
    
    • and then
      mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener( ) {
                @Override
                public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
    
                }
            });
    

    Use it item child long click

    • first register child view id in adapter
     @Override
        protected void convert(BaseViewHolder helper, Status item) {
            helper.setText(R.id.tweetName, item.getUserName())
                    .setText(R.id.tweetText, item.getText())
                    .setText(R.id.tweetDate, item.getCreatedAt())
                    .setVisible(R.id.tweetRT, item.isRetweet())
                    .addOnLongClickListener(R.id.tweetText)
                    .linkify(R.id.tweetText);
    
        }
    
    • and then
     mRecyclerView.addOnItemTouchListener(new OnItemChildLongClickListener( ) {
                @Override
                public void onSimpleItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
                    Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
                }
            });
    

    if you wish to implement various forms of click

    override differnet click

     mRecyclerView.addOnItemTouchListener(new SimpleClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                    Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
                    Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
                }
            });
    
    

    问题延伸1. LinearLayout 嵌套 RecyclerView 时,点击事件被RecyclerView抢占,导致外部LinearLayout拿不到事件,怎么实现LinearLayout的点击事件?如下已经解决方案
    kotlin代码

    recycleView?.setOnTouchListener { v, event ->
                                        if (event?.action == MotionEvent.ACTION_UP) {
                                            viewLinearLayout.performClick()
                                        }
                                        false
                                    }
    

    java代码:


    WechatIMG546.png

    相关文章

      网友评论

          本文标题:RecycleView多布局、单布局实现外部点击事件操作

          本文链接:https://www.haomeiwen.com/subject/qcmpdhtx.html