如何在com.nightonke:boommenu库中的每个子菜单项上添加click Listener?

时间:2017-11-10 23:47:25

标签: android

我已经看过很多关于热潮的讨论。但是,在点击BoomMenuButton后显示以下菜单项的每次点击时,我都没有看到关于事件处理功能的任何地方。

enter image description here

我一直在阅读所有的解决方案,但没有一个可以帮助我。这是我的代码
activity_mail.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_simple_circle_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <com.nightonke.boommenu.BoomMenuButton
        android:id="@+id/bmb3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:bmb_normalColor="@android:color/holo_blue_dark"
        app:bmb_highlightedColor="@android:color/holo_blue_bright"
        app:bmb_buttonEnum="textOutsideCircle"
        app:bmb_piecePlaceEnum="piecePlace_share"
        app:bmb_buttonPlaceEnum="buttonPlace_custom">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/middle_big_button_trans"
            android:layout_gravity="center" />

    </com.nightonke.boommenu.BoomMenuButton>
</LinearLayout>


活动类是

public class MainActivity extends AppCompatActivity {
public static final String TAG = "MainActivity";
private BoomMenuButton boomMenuButton;

public MainActivity() {
    Log.d(TAG,"Inside MainActivity's default Constructor");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG,"Setting the layout before it's in inflates");
    setContentView(R.layout.activity_main);
    Log.d(TAG,"Before initializing Bmb3");
    initializeBmb3();
    Log.d(TAG,"After initializing Bmb3");
}

private void initializeBmb3() {
    Log.d(TAG,"Inside initializeBmb3()");
    BoomMenuButton bmb = (BoomMenuButton) findViewById(R.id.bmb3);
    for (int i = 0; i < 3; i++) {
        Log.d(TAG,"Inside for loop -> Before adding builder");
        bmb.addBuilder(BuilderManager.getTextOutsideCircleButtonBuilderWithDifferentPieceColor());
        Log.d(TAG,"Inside for loop -> After adding builder");
    }


    float w = Util.dp2px(80);
    float h = Util.dp2px(96);
    float h_0_5 = h / 2;
    float h_1_5 = h * 1.5f;
    float hm = bmb.getButtonHorizontalMargin();
    float vm = bmb.getButtonVerticalMargin();
    float vm_0_5 = vm / 2;
    float vm_1_5 = vm * 1.5f;

    bmb.getCustomButtonPlacePositions().add(new PointF(-w - hm, +h_0_5 + vm_0_5));
    bmb.getCustomButtonPlacePositions().add(new PointF(+w + hm, +h_0_5 + vm_0_5));
    bmb.getCustomButtonPlacePositions().add(new PointF(      0, +h_1_5 + vm_1_5));

    for (int i = 0; i < bmb.getPiecePlaceEnum().pieceNumber(); i++) {
       bmb.getBuilder(i)
                .listener(new OnBMClickListener() {
                    @Override
                    public void onBoomButtonClick(int index) {
                        // When the boom-button corresponding this builder is clicked.
                        Toast.makeText(MainActivity.this, "Clicked " + index, Toast.LENGTH_SHORT).show();
                    }
                });
    }

    Log.d(TAG,"At the end of initializeBmb3");
}

}

如何在展示广告中的每个项目上添加点击功能?

2 个答案:

答案 0 :(得分:1)

请查看文档here

摘自文件:

  

BMB的听众

     

如果您想管理方法中的所有点击事件,您可以使用   OnBoomListener / OnBoomListenerAdapter:

// Use OnBoomListener to listen all methods
bmb.setOnBoomListener(new OnBoomListener() {
    @Override
    public void onClicked(int index, BoomButton boomButton) {
        // If you have implement listeners for boom-buttons in builders,
        // then you shouldn't add any listener here for duplicate callbacks.
    }

    @Override
    public void onBackgroundClick() {
        textViewForAnimation.setText("Click background!!!");
    }

    @Override
    public void onBoomWillHide() {
        textViewForAnimation.setText("Will RE-BOOM!!!");
    }

    @Override
    public void onBoomDidHide() {
        textViewForAnimation.setText("Did RE-BOOM!!!");
    }

    @Override
    public void onBoomWillShow() {
        textViewForAnimation.setText("Will BOOM!!!");
    }

    @Override
    public void onBoomDidShow() {
        textViewForAnimation.setText("Did BOOM!!!");
    }
});

答案 1 :(得分:0)

在for循环中构建菜单时我所做的是:

    for (int i = 0; i < HamBmb.getPiecePlaceEnum().pieceNumber(); i++) {
        HamButton.Builder builder = new HamButton.Builder().rotateImage(true);

        switch (i) {
            case 0:
                builder.normalImageRes(R.drawable.ic_launcher_foreground)
                        .normalText("Menu 1")
                        .listener(new OnBMClickListener() {
                            @Override
                            public void onBoomButtonClick(int index) {
                                HandleBoomMenuClick(index);
                            }
                        });
                break;
            case 1:
                builder.normalImageRes(R.drawable.ic_launcher_foreground)
                        .normalText("Menu 2")
                        .listener(new OnBMClickListener() {
                            @Override
                            public void onBoomButtonClick(int index) {
                                HandleBoomMenuClick(index);
                            }
                        });
                break;
            case 2:
                builder.normalImageRes(R.drawable.ic_launcher_foreground)
                        .normalText("Menu 3")
                        .listener(new OnBMClickListener() {
                            @Override
                            public void onBoomButtonClick(int index) {
                                HandleBoomMenuClick(index);
                            }
                        });
                break;
            default:
        }

        HamBmb.addBuilder(builder);
    }

我有另一种方法,在我的情况下,打开另一个活动:

private void HandleBoomMenuClick(int index) {
    if (index < HamBmb.getPiecePlaceEnum().pieceNumber()) {
        Intent intent;

        switch (index) {
            case 0:
                intent = new Intent(this, YOURACTIVITY0.class);
                startActivity(intent);
                break;
            case 1:
                intent = new Intent(this, YOURACTIVITY1.class);
                startActivity(intent);
                break;
            case 2:
                intent = new Intent(this, YOURACTIVITY2.class);
                startActivity(intent);
                break;

            default:
        }
    }
}

很抱歉这么晚才来到这场比赛......