【问题标题】:FrameLayout click event is not firingFrameLayout 点击事件未触发
【发布时间】:2013-06-03 09:26:00
【问题描述】:

我已将 Framelayour 用于单击事件,并且在 2 天前工作正常,但不知道发生了什么,现在它无法正常工作。
请有人帮助我。
我的代码如下: 设计:

<FrameLayout
        android:id="@+id/flWebpre"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <WebView
            android:id="@+id/wvWebsite"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <ProgressBar
            android:id="@+id/pbWebsite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:layout_gravity="center_horizontal" />
    </FrameLayout>

代码:

FrameLayout flWebPre = (FrameLayout) findViewById(R.id.flWebpre);
    flWebPre.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (isExpanded) {
                isExpanded = false;

                new CollapseAnimation(slidingPanel, panelWidth,
                        TranslateAnimation.RELATIVE_TO_SELF, 0.70f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f,
                        0, 0.0f);
            }
        }
    });

【问题讨论】:

  • 我知道您已经做了一些事情,但是您忘记将“可点击”作为参数放在 FrameLayout 中。

标签: android android-framelayout


【解决方案1】:

一种简单的方法是拦截所有触摸事件。默认情况下,ViewGroup#onInterceptTouchEvent 返回false

您可以创建自定义布局:

public class ClickableFrameLayout extends FrameLayout {
    private OnClickListener mOnClickListener;

    @Override
    public void setOnClickListener(OnClickListener l) {
        super.setOnClickListener(l);
        mOnClickListener = l;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mOnClickListener != null;
    }


    // Standard constructors — just pass everything
    public ClickableFrameLayout(final Context context) {
        super(context);
    }

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

    public ClickableFrameLayout(final Context context, final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ClickableFrameLayout(final Context context, final AttributeSet attrs, final int defStyleAttr, final int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
}

【讨论】:

  • 是的,它正在工作,但是您忘记了使用 AtributeSet 的第二个构造函数...
  • 哇,多么完美的解决方案..!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多