单击按钮事件不会被提升

时间:2012-09-13 15:05:02

标签: android button onclick horizontal-scrolling

我正在努力制作类似自定义快速徽章的东西,上面有一些按钮。我已经成功地设计了它,但当我点击这些按钮时它什么也没做。谁能告诉我哪里出错了。

demo_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/likemenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="30dp"
    android:text="Button" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</TextView>

<Button
    android:id="@+id/likequickaction"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="30dp"
    android:text="Button" />

</LinearLayout>

popup_grid_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<FrameLayout
    android:id="@+id/header2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10.0dip"
    android:background="@drawable/quickaction_top_frame" />

<ImageView
    android:id="@+id/arrow_up"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:src="@drawable/quickaction_arrow_up" />

<HorizontalScrollView
    android:id="@+id/scroll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/header2"
    android:background="@drawable/quickaction_slider_background"
    android:fadingEdgeLength="0.0dip"
    android:paddingLeft="1.0dip"
    android:scrollbars="none" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:background="@drawable/quickaction_slider_grip_left" />

        <include
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            layout="@layout/api_buttons" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:background="@drawable/quickaction_slider_grip_right" />
    </LinearLayout>
</HorizontalScrollView>

<FrameLayout
    android:id="@+id/footer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/scroll"
    android:background="@drawable/quickaction_bottom_frame" />

</RelativeLayout>

api_buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/quickaction_slider_background" >

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/one"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minHeight="80dp"
        android:minWidth="80dp"
        android:text="One" />

    <Button
        android:id="@+id/two"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minHeight="80dp"
        android:minWidth="80dp"
        android:text="Two" />

    <Button
        android:id="@+id/three"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minHeight="80dp"
        android:minWidth="80dp"
        android:text="Three" />

    <Button
        android:id="@+id/four"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minHeight="80dp"
        android:minWidth="80dp"
        android:text="Four" />

    <Button
        android:id="@+id/five"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minHeight="80dp"
        android:minWidth="80dp"
        android:text="Five" />

    <Button
        android:id="@+id/six"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minHeight="80dp"
        android:minWidth="80dp"
        android:text="Six" />
</TableRow>

</HorizontalScrollView>

BetterPopupWindow.java

public class BetterPopupWindow {
protected final View anchor;
private final PopupWindow window;
private View root;
private Drawable background = null;
private final WindowManager windowManager;

public BetterPopupWindow(View anchor) {
    this.anchor = anchor;
    this.window = new PopupWindow(anchor.getContext());

    // when a touch even happens outside of the window
    // make the window go away
    this.window.setTouchInterceptor(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                BetterPopupWindow.this.window.dismiss();
                return true;
            }
            return false;
        }
    });

    this.windowManager = (WindowManager) this.anchor.getContext()
            .getSystemService(Context.WINDOW_SERVICE);
    onCreate();
}

protected void onCreate() {
}

protected void onShow() {
}

private void preShow() {
    if (this.root == null) {
        throw new IllegalStateException(
                "setContentView was not called with a view to display.");
    }
    onShow();

    if (this.background == null) {
        this.window.setBackgroundDrawable(new BitmapDrawable());
    } else {
        this.window.setBackgroundDrawable(this.background);
    }

    this.window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    this.window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    this.window.setTouchable(true);
    this.window.setFocusable(true);
    this.window.setOutsideTouchable(true);

    this.window.setContentView(this.root);
}

public void setBackgroundDrawable(Drawable background) {
    this.background = background;
}

public void setContentView(View root) {
    this.root = root;
    this.window.setContentView(root);
}

public void setContentView(int layoutResID) {
    LayoutInflater inflator = (LayoutInflater) this.anchor.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.setContentView(inflator.inflate(layoutResID, null));
}

public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
    this.window.setOnDismissListener(listener);
}

public void showLikePopDownMenu() {
    this.showLikePopDownMenu(0, 0);
}

public void showLikePopDownMenu(int xOffset, int yOffset) {
    this.preShow();

    this.window.setAnimationStyle(R.style.Animations_PopDownMenu);

    this.window.showAsDropDown(this.anchor, xOffset, yOffset);
}

public void showLikeQuickAction() {
    this.showLikeQuickAction(0, 0);
}

public void showLikeQuickAction(int xOffset, int yOffset) {
    this.preShow();

    this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);

    int[] location = new int[2];
    this.anchor.getLocationOnScreen(location);

    Rect anchorRect = new Rect(location[0], location[1], location[0]
            + this.anchor.getWidth(), location[1] + this.anchor.getHeight());

    this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    int rootWidth = this.root.getMeasuredWidth();
    int rootHeight = this.root.getMeasuredHeight();

    int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
    int screenHeight = this.windowManager.getDefaultDisplay().getHeight();

    int xPos = ((screenWidth - rootWidth) / 2) + xOffset;
    int yPos = anchorRect.top - rootHeight + yOffset;

    // display on bottom
    if (rootHeight > anchorRect.top) {
        yPos = anchorRect.bottom + yOffset;
        this.window.setAnimationStyle(R.style.Animations_GrowFromTop);
    }

    this.window.showAtLocation(this.anchor, Gravity.NO_GRAVITY, xPos, yPos);
}

public void dismiss() {
    this.window.dismiss();
}
}

LikeQuickActionDemo.java

public class LikeQuickActionsDemo extends Activity {
private Button likemenuButton;
private Button likequickactionButton;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.demo_layout);

    this.likemenuButton = (Button) this.findViewById(R.id.likemenu);
    this.likemenuButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            DemoPopupWindow dw = new DemoPopupWindow(v);
            dw.showLikePopDownMenu();
        }
    });

    this.likequickactionButton = (Button) this
            .findViewById(R.id.likequickaction);
    this.likequickactionButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            DemoPopupWindow dw = new DemoPopupWindow(v);
            dw.showLikePopDownMenu(0, 200);
        }
    });

}

private static class DemoPopupWindow extends BetterPopupWindow implements
        OnClickListener {
    public DemoPopupWindow(View anchor) {
        super(anchor);
    }

    protected void onCreate() {
        // inflate layout
        LayoutInflater inflater = (LayoutInflater) this.anchor.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        ViewGroup root = (ViewGroup) inflater.inflate(
                R.layout.popup_grid_layout, null);

        // setup button events
        for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
            View v = root.getChildAt(i);

            if (v instanceof TableRow) {
                TableRow row = (TableRow) v;

                for (int j = 0, jcount = row.getChildCount(); j < jcount; j++) {
                    View item = row.getChildAt(j);
                    if (item instanceof Button) {
                        Button b = (Button) item;
                        b.setOnClickListener(this);
                    }
                }
            }
        }

        // set the inflated view as what we want to display
        this.setContentView(root);
    }

    public void onClick(View v) {
        // we'll just display a simple toast on a button click
        Button b = (Button) v;
        Toast.makeText(this.anchor.getContext(), b.getText(),
                Toast.LENGTH_SHORT).show();
        this.dismiss();
    }
}
}

1 个答案:

答案 0 :(得分:2)

问题出在DemoPopupWindow.onCreate方法:

    ViewGroup root = (ViewGroup) inflater.inflate(
            R.layout.popup_grid_layout, null);
    for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
        View v = root.getChildAt(i);
        if (v instanceof TableRow) {
{p> root将在RelativeLayout中定义popup_grid_layout.xml。稍后,只有当子视图为TableRow时,才会遍历其所有子项并设置onclick侦听器,但事实并非如此。我猜你想要的是查找horizontalScrollView1视图并对其子项执行相同操作,例如:

View horizontalScrollView1 = findViewById(R.id.horizontalScrollView1);
for (int i = 0, icount = horizontalScrollView1.getChildCount(); i < icount; i++) {
        View v = horizontalScrollView1.getChildAt(i);
        if (v instanceof TableRow) {
...