简体   繁体   中英

How to call method from another Class in Android

I have a main layout with google maps and RecyclerView with locations. From my Recycler Adapter I have OnClickListener where I need to call zoom on map where is property from Main layout.

Recycle Adapter:

 viewHolder.btnMap.setOnClickListener(new View.OnClickListener() 
 {
            @Override
            public void onClick(View v) 
            {
                zoomMap(lat, lng);
            }
 }); 

Main layout:

public void zoomMap(double lat, double lng)
{
        //String to display current latitude and longitude
        String msg = lat + ", "+lng;

        //Creating a LatLng Object to store Coordinates
        LatLng latLng = new LatLng(lat, lng);

        //Moving the camera
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        //Animating the camera
        mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

        //Displaying current coordinates in toast
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

For calling method from another class you have to make object of that particular class.

ClassName className = new ClassName();
viewHolder.btnMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                className.zoomMap(lat, lng);

            }
        });

Happy coding.

使Main类成为第二个活动的对象,然后通过object.methed调用下一个活动。

I recommend you to use the observer pattern : Create an interface like MapZoomer, implement your interface with the mapview and call the interface method from the listview. In code it would look like:

interface MapZoomer{
    public void zoomMap(double lat, double lng);
}
...
class MapActivity extends AppCompatActivity implements MapZoomer{
    ...
    public void zoomMap(double lat, double lng){...}
}
...
class RecycleViewContainer{
    MapZoomer zoomer;

    public RecycleViewContainer(MapZoomer zoomer){
        this.zoomer = zoomer;
        ...
        viewHolder.btnMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ...
                zoomer.zoomMap(lat, lng);

            }
        }); 
    }
} 

Here is several ways: 1) you can make method that needed to call static then simple call with

public static void zoomMap(double lat, double lng)

then

ClassWhereThisMethod.zoomMap(lat, lng);

2) you can make interface in adapter then set implementer of it to "this" in activity and implement this method. Then you will be able to call it from adapter.

3) create local Runnable variable in adapter, then set it in constructor or make it public and set it when adapter initialized and pass runnable with runonuithread with zoomMap(lat, lng); in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM