Example usage for android.support.v4.widget ExploreByTouchHelper ExploreByTouchHelper

List of usage examples for android.support.v4.widget ExploreByTouchHelper ExploreByTouchHelper

Introduction

In this page you can find the example usage for android.support.v4.widget ExploreByTouchHelper ExploreByTouchHelper.

Prototype

public ExploreByTouchHelper(View view) 

Source Link

Usage

From source file:org.connectbot.views.CheckableMenuItem.java

public CheckableMenuItem(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    mRootView = inflate(context, R.layout.view_checkablemenuitem, this);

    mTitle = (TextView) mRootView.findViewById(R.id.title);
    mSummary = (TextView) mRootView.findViewById(R.id.summary);
    mSwitch = (SwitchCompat) findViewById(R.id.checkbox_switch);

    setFocusable(true);/*from w w w .  j  av a2  s .  c o m*/

    mAccessHelper = new ExploreByTouchHelper(this) {
        private final Rect mTmpRect = new Rect();

        @Override
        protected int getVirtualViewAt(float x, float y) {
            return HOST_ID;
        }

        @Override
        protected void getVisibleVirtualViews(List<Integer> virtualViewIds) {
        }

        @Override
        protected void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEvent event) {
            if (virtualViewId != HOST_ID) {
                // TODO(kroot): remove this when the bug is fixed.
                event.setContentDescription(PLACEHOLDER_STRING);
                return;
            }

            event.setContentDescription(mTitle.getText() + " " + mSummary.getText());
            event.setClassName(ACCESSIBILITY_EVENT_CLASS_NAME);
            event.setChecked(isChecked());
        }

        @Override
        protected void onPopulateNodeForVirtualView(int virtualViewId, AccessibilityNodeInfoCompat node) {
            if (virtualViewId != HOST_ID) {
                // TODO(kroot): remove this when the bug is fixed.
                node.setBoundsInParent(mPlaceHolderRect);
                node.setContentDescription(PLACEHOLDER_STRING);
                return;
            }

            mTmpRect.set(0, 0, CheckableMenuItem.this.getWidth(), CheckableMenuItem.this.getHeight());
            node.setBoundsInParent(mTmpRect);

            node.addAction(AccessibilityNodeInfoCompat.ACTION_CLICK);
            node.setClassName(ACCESSIBILITY_EVENT_CLASS_NAME);
            node.setCheckable(true);
            node.setChecked(isChecked());

            node.addChild(mTitle);
            node.addChild(mSummary);
        }

        @Override
        protected boolean onPerformActionForVirtualView(int virtualViewId, int action, Bundle arguments) {
            if (virtualViewId != HOST_ID) {
                return false;
            }

            if (action == AccessibilityNodeInfoCompat.ACTION_CLICK) {
                mSwitch.toggle();
                sendAccessibilityEvent(mRootView, AccessibilityEventCompat.CONTENT_CHANGE_TYPE_UNDEFINED);
                return true;
            }

            return false;
        }
    };
    ViewCompat.setAccessibilityDelegate(mRootView, mAccessHelper);

    setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mSwitch.toggle();
        }
    });

    if (attrs != null) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CheckableMenuItem, 0, 0);

        @DrawableRes
        int iconRes = typedArray.getResourceId(R.styleable.CheckableMenuItem_android_icon, 0);
        @StringRes
        int titleRes = typedArray.getResourceId(R.styleable.CheckableMenuItem_android_title, 0);
        @StringRes
        int summaryRes = typedArray.getResourceId(R.styleable.CheckableMenuItem_summary, 0);

        typedArray.recycle();

        ImageView icon = (ImageView) mRootView.findViewById(R.id.icon);
        mTitle.setText(titleRes);
        if (iconRes != 0) {
            Resources resources = context.getResources();
            Resources.Theme theme = context.getTheme();
            Drawable iconDrawable = VectorDrawableCompat.create(resources, iconRes, theme);

            icon.setImageDrawable(iconDrawable);
        }
        if (summaryRes != 0) {
            mSummary.setText(summaryRes);
        }
    }
}