文字选择无法在自定义网络视图

时间:2015-08-04 05:14:53

标签: android android-webview android-actionmode

我希望在我的CustomWebView中突出显示所选文字,因为我实施Actionmode如下......

  public class CustomWebView extends WebView {


public ActionMode mActionMode;
public ActionMode.Callback mActionModeCallback;

private ActionMode.Callback mSelectActionModeCallback;


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

public CustomWebView(Context context) {
    super(context);

    this.context = context;
    sm = new ScreenManager((Activity) context);
}   



@Override
public ActionMode startActionMode(Callback callback) {

    ViewParent parent = getParent();
    if (parent == null) {
        return null;
    }
    String name = "";
    if (callback != null)
        name = callback.getClass().toString();
    if (name.contains("SelectActionModeCallback")) {
        mSelectActionModeCallback = callback;
    }
    System.out.println("startActionMode"+name);
    mActionModeCallback = new CustomActionModeCallback();

    return super.startActionModeForChild(this, mActionModeCallback);
}

private class CustomActionModeCallback implements ActionMode.Callback {


    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu)   
        mActionMode = mode;
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.reader_epub_menu, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false; // Return false if nothing is done
    }


    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_highlight_add:
            loadUrl(highlightSelection());
            break;
        case R.id.action_note_add:
            frag.call(spineIndex, tocIndex);
            break;
        }

        mActionMode.finish(); 
        return true;
    }


    @Override
    public void onDestroyActionMode(ActionMode mode) {

        frag.makeSwipeEnable();
        clearFocus(); // Remove the selection highlight and handles.


        if (mSelectActionModeCallback != null) {
            mSelectActionModeCallback.onDestroyActionMode(mode);
        }

        mActionMode = null;
    }
}



public ActionMode getActionMode() {
    return mActionMode;
}

and my in my reader fragment implemented Gesture listner as follows 


public class GestureListener extends
            GestureDetector.SimpleOnGestureListener {

 @Override
        public void onLongPress(MotionEvent e) {
            makeSwipeDisable();
            reader.startActionMode(reader.mActionModeCallback);
            reader.dispatchTouchEvent(e);
            super.onLongPress(e);
        }
}

我从以下内容中删除了onTouchEvent但它工作正常但webview上的onFling()丢失了,它只是水平滚动。

reader = new CustomWebView(getActivity()) {

        @Override
        protected void onScrollChanged(int l, int t, int oldl, int oldt) {
            // TODO Auto-generated method stub
            super.onScrollChanged(l, t, oldl, oldt);
            // checking whether the current page has been bookmarked and
            // animates the ribbon down and up
            if (isBookmarked(rOffset, currentContent))
                scaleDown();
            else
                scaleUp();
        }


        @Override
       public boolean onTouchEvent(MotionEvent event) {
            return (gestureDetector.onTouchEvent(event) || super
                   .onTouchEvent(event));
      }

此代码有什么问题吗?CAB可见且有效,但问题没有选择文字。

1 个答案:

答案 0 :(得分:2)

请告诉我您的真实问题:突出显示所选文字或页面翻转效果是否无法正常工作?

您的意思是,您使用的是https://github.com/openaphid/android-flip之类的其他库吗?它不起作用?

恕我直言,如何单独使用自己的手势探测器并返回超级onTouch事件? 对onTouchEvent返回true表示您需要消耗事件,并阻止传播到父级。

 @Override
       public boolean onTouchEvent(MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            return super.onTouchEvent(event);
      }

详见: http://developer.android.com/reference/android/view/View.OnTouchListener.html

" 返回如果侦听器已使用该事件,则为True,否则为false。"