EditText给出错误:TextView不支持文本选择。选择取消

时间:2016-06-01 10:39:08

标签: android

我有RecyclerView。位置上的元素是EditText的标题,然后所有其他元素都是图像。长按EditText它会给出选项粘贴。这样可以正常工作。 但是,当我将回收器视图滚动到底部并再次到达顶部并长按时,它将不会显示粘贴选项并给出错误。 在滚动到底部并再次转到顶部时,请致电onBindViewHolder

TextViewTextView不支持文字选择。选择已取消。

public class Someclass extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {


    private static final int TYPE_EDIT = 0;
    private static final int TYPE_IMAGE = 1;
    List<String> msomelist = new ArrayList<String>();

    public void someMethod(List<String> somelist) {

        msomelist.clear();
        msomelist.addAll(somelist);
        notifyDataSetChanged();

    }




    public Someclass(Activity activity, List<String> somelist) {
        this.activity = activity;
        this.msomelist.clear();
        this.msomelist.addAll(somelist);
        mContext = activity;
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0)
            return TYPE_EDIT;

        return TYPE_IMAGE;
    }

    @Override
    public void onClick(View view) {
        int postition = (int) view.getTag();
        msomelist.remove(postition);
        notifyDataSetChanged();

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
       ImageView img;

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

            img = (ImageView) itemView.findViewById(R.id.image);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_IMAGE) {
            View view = LayoutInflater.from(mContext)
                    .inflate(somelayout, false);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        } else {
            View view = LayoutInflater.from(mContext)
                    .inflate(someotherlayout, false);


            return new OtherHolder(view);
        }
    }

    class OtherHolder extends RecyclerView.ViewHolder {
        EditText editText;

        public OtherHolder(View itemView) {
            super(itemView);
            editText = (EditText) itemView.findViewById(R.id.ediItext);
            editText.requestFocus();
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof ViewHolder) {
            some function.....
        } else if (holder instanceof OtherHolder) {
            some function
        }
    }
}

3 个答案:

答案 0 :(得分:15)

这是a known bug in the Android platform。我一开始并不相信,但发布的代码@ user2246055是一个有效的解决方法!

我更喜欢在我的适配器中添加它而不是继承TextView:

        @Override
        public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
            super.onViewAttachedToWindow(holder);

            // Bug workaround for losing text selection ability, see:
            // https://code.google.com/p/android/issues/detail?id=208169
            holder.textView.setEnabled(false);
            holder.textView.setEnabled(true);
        }

答案 1 :(得分:5)

使用以下MyEditText.java可以解决您的问题:

public class MyEditText extends EditText {
private boolean mEnabled; // is this edittext enabled

public MyEditText(Context context) {
    super(context);
}

public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    try {
        if (!mEnabled) return;
        super.setEnabled(false);
        super.setEnabled(mEnabled);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void setEnabled(boolean enabled) {
    this.mEnabled = enabled;
    super.setEnabled(enabled);
}}

答案 2 :(得分:3)

长按EditText会调用Editor.performLongClick()以显示选择弹出窗口。

getInsertionController().show();之前

它会检查mInsertionControllerEnabled,当您使用setText()时,TextView会调用Editor.prepareCursorControllers()重置mInsertionControllerEnabled,如下所示:

boolean windowSupportsHandles = false;

ViewGroup.LayoutParams params = mTextView.getRootView().getLayoutParams();
if (params instanceof WindowManager.LayoutParams) {
    WindowManager.LayoutParams windowParams = (WindowManager.LayoutParams) params;
    windowSupportsHandles = windowParams.type < WindowManager.LayoutParams.FIRST_SUB_WINDOW
            || windowParams.type > WindowManager.LayoutParams.LAST_SUB_WINDOW;
}

boolean enabled = windowSupportsHandles && mTextView.getLayout() != null;
mInsertionControllerEnabled = enabled && isCursorVisible();

如您所见,rootView必须是窗口类型,mInsertionControllerEnabled必须是false

但是当setText()位于onBindViewHolder()时,EditText还不是attachToWindow,所以我们应setEnable()EditText之后1}} attachToWindow强制编辑器重置mInsertionControllerEnabled值。然后选择将立即生效。