sinat_30858153
2017-02-04 09:48 阅读 2.1k

RecyclerView内存在多个EditText时,长按弹不出系统的复制/粘贴/全选等选择

在仿微博发布头条文章功能的时候出现了这么一个问题。

 public class NewsReleaseAdapter3 extends RecyclerView.Adapter {

    private Context context;
    private List<EditDetailBean> list;

    public NewsReleaseAdapter3(Context context, List<EditDetailBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        EditText editText = new EditText(context);
        editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return new TxtViewHolder(editText);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TxtViewHolder) {
            setTxtView((TxtViewHolder) holder, position);
        }
    }

    private void setTxtView(TxtViewHolder holder, final int position) {
        final EditDetailBean bean = getItem(position);
        if (bean != null) {
           _ holder.contentEt.setText("123321123");_
        }
    }


    private class TxtViewHolder extends RecyclerView.ViewHolder {

        EditText contentEt;

        private TxtViewHolder(View itemView) {
            super(itemView);
            contentEt = (EditText) itemView;
//            contentEt = (EditText) itemView.findViewById(R.id.contentEt);
        }

    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    private EditDetailBean getItem(int position) {
        return list.get(position);
    }

只要添加了“holder.contentEt.setText("123321123");”经过滑动,就会出现EditText长按没有震动反馈,也没有弹出菜单。但是能进入onLongClickListener.
跪求大神

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • caozhy 从今以后生命中的每一秒都属于我爱的人 2017-02-18 18:05
  • magicoolboy1984 吕树临疯1984 2019-12-16 14:15

    直接上代码,本人测试好用:

    holder.reportContent.setOnFocusChangeListener((v, hasFocus) -> {
                if (hasFocus) {
                    holder.reportContent.setTextIsSelectable(true);
                } else {
                    holder.reportContent.setTextIsSelectable(false);
                    holder.reportContent.setFocusableInTouchMode(true);
                    holder.reportContent.setFocusable(true);
                    holder.reportContent.setClickable(true);
                    holder.reportContent.setLongClickable(true);
                }
            });
    

    初步认为是setText覆盖了BufferType,导致弹不出复制粘贴功能,看下BufferType类型,应该是通过SPANNABLE类型来实现选中效果实现复制粘贴

    public enum BufferType {
            NORMAL, SPANNABLE, EDITABLE
        }
    
    点赞 1 评论 复制链接分享

相关推荐