Uber RequestButton onClickListener

时间:2016-02-18 11:39:00

标签: android api uber-api

我在我的项目中使用Uber android-api。 单击RequestButton,它会将用户导航到Uber App,但我想在导航到Uber应用程序之前处理一些数据。所以为此,我想要RequestButton的onClickListener。

我已经尝试直接在requestButton的对象上提供onClickListener然后我得到了该事件,但它没有导航到Uber App。

requestButton.setOnClickListener(new View.OnClickListener() 
{
      @Override 
       public void onClick(View v) {

       }
});              

3 个答案:

答案 0 :(得分:2)

@bitek走在正确的轨道上,但这里是覆盖RideRequestButton的完整解决方案

public class CustomUberButton extends RideRequestButton implements 
 View.OnClickListener {
private View.OnClickListener internalListener;
private View.OnClickListener preClickListener;

public CustomUberButton(Context context) {
    super(context);
}

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

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

public void setPreClickListener(OnClickListener preClickListener) {
    this.preClickListener = preClickListener;
}

@Override
public void setOnClickListener(@Nullable OnClickListener l) {
    // redirect the listener to us but keep the original listener registered by the original button to reuse later.
    internalListener = l;
    super.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    preClickListener.onClick(view);
    internalListener.onClick(view);
}

}

上述类可用于xml

<com.example.android.takemehome.uber.CustomUberButton
    android:id="@+id/btn_uber"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    uber:ub__style="black" />

,活动看起来像

mUberButton = (CustomUberButton) findViewById(R.id.btn_uber);
mUberButton.setPreClickListener(this);

在这种情况下,活动也会实现View.OnClickListener

答案 1 :(得分:1)

为了能够在单击RequestButton 之前调用自己的自定义代码,即调用它的OnClickListener回调方法,您必须更改游乐设施内的RequestButton代码 - Android的SDK。

然后在您的活动中,您必须在RequestButton对象上调用setCustomPreOnClickListener:

public void rideRequestButtonWithCustomPreOnClickListener() {
        RequestButton uberButtonBlack = (RequestButton) findViewById(R.id.uber_button_black);
        RequestButton uberButtonWhite = (RequestButton) findViewById(R.id.uber_button_white);

        RideParameters rideParameters = new RideParameters.Builder()
                .setProductId(UBERX_PRODUCT_ID)
                .setPickupLocation(PICKUP_LAT, PICKUP_LONG, PICKUP_NICK, PICKUP_ADDR)
                .setDropoffLocation(DROPOFF_LAT, DROPOFF_LONG, DROPOFF_NICK, DROPOFF_ADDR)
                .build();

        uberButtonBlack.setRideParameters(rideParameters);
        uberButtonWhite.setRideParameters(rideParameters);

        uberButtonBlack.setCustomPreOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Custom code for black button called before UberRequestButton's onClick() listener");
            }
        });

        uberButtonWhite.setCustomPreOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Custom code for white button called before UberRequestButton's onClick() listener");
            }
        });
    }

另一种方法是创建自定义类:

package com.uber.sdk.android.rides.samples;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.uber.sdk.android.rides.RequestButton;

public class CustomUberRequestButton extends RequestButton implements View.OnClickListener {
    private static final String TAG = CustomUberRequestButton.class.getSimpleName();

    public CustomUberRequestButton(Context context) {
        super(context);
    }

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

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

    @Override
    public void onClick(View v) {
        Log.d(TAG, "Custom code before calling UberRequestButton's onClick() listener");

        // Explicitly need to call UberRequestButton onClick() listener
        super.onClick(v);
    }
}

你可以这样使用它:

/**
 * Please make sure you replace com.uber.sdk.android.rides.RequestButton
 * with com.uber.sdk.android.rides.samples.CustomUberRequestButton inside
 * the res/layout/activity_sample.xml file, otherwise you will get a ClassCastException.
 */
public void rideRequestButtonWithCustomSubclassOnClickListener() {
    CustomUberRequestButton uberButtonBlack = (CustomUberRequestButton) findViewById(R.id.uber_button_black);
    CustomUberRequestButton uberButtonWhite = (CustomUberRequestButton) findViewById(R.id.uber_button_white);

    RideParameters rideParameters = new RideParameters.Builder()
            .setProductId(UBERX_PRODUCT_ID)
            .setPickupLocation(PICKUP_LAT, PICKUP_LONG, PICKUP_NICK, PICKUP_ADDR)
            .setDropoffLocation(DROPOFF_LAT, DROPOFF_LONG, DROPOFF_NICK, DROPOFF_ADDR)
            .build();

    uberButtonBlack.setRideParameters(rideParameters);
    uberButtonWhite.setRideParameters(rideParameters);
}

使用自定义类,您还可以根据需要设置自定义preOnClickListener。请参阅上述提交中的rideRequestButtonWithCustomSubclassAndPreOnClickListener方法。

答案 2 :(得分:0)

如果您要处理RequestButton无法处理的内容,则需要制作自定义Uber SDK