意图来自fragment和LinearLayout的另一个活动

时间:2014-01-30 11:06:12

标签: android google-maps android-intent android-fragments android-xml

我的应用中有谷歌地图v2。当我触摸标记时,它会显示一个LinearLayout,其中包含来自此标记的信息,当我点击另一个标记时,此相对布局会更新。所有这一切都很好,但是我希望我点击LinearLayout,它会向另一个活动启动一个意图,其中包含来自地图片段的更多信息。我的问题是没有调用该活动。

这是应用程序结构:

enter image description here

我的fragment_mapa.xml是这样的:

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

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:gravity="center" />

    <LinearLayout
        android:id="@+id/container_slide"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:animateLayoutChanges="true"
        android:layout_alignParentBottom="true"
        android:clickable="true"
        android:onClick="goToAttract" >

        <View
            android:id="@+id/transparentView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <RelativeLayout
            android:id="@+id/sliding_up"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="vertical" 
            android:layout_alignParentBottom="true"
            android:clickable="true"
            android:onClick="onClickSlide()"
            android:focusable="false"
            android:animateLayoutChanges="true"
            android:visibility="gone" >

            <ImageView
                android:id="@+id/sombra"
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:layout_alignParentTop="true"
                android:background="@drawable/degradado_mapa" />

            <RelativeLayout
                android:id="@+id/sliding_up"
                android:layout_width="match_parent"
                android:layout_height="95dp"
                android:background="#fff"
                android:orientation="vertical" 
                android:layout_alignParentBottom="true"
                android:clickable="true"
                android:focusable="false" >

                <!-- Icono de categoria o imagen propia -->
                <LinearLayout
                    android:id="@+id/iconMap"
                    android:layout_width="54dp"
                    android:layout_height="54dp"
                    android:layout_alignParentLeft="true"
                    android:layout_marginRight="6dip"
                    android:layout_marginLeft="15dp"
                    android:layout_marginTop="15dp"
                    android:background="#ffa95f" >

                    <com.applidium.shutterbug.FetchableImageView
                        android:id="@+id/imgMap"
                        android:layout_width="54dp"
                        android:layout_height="54dp" />
                </LinearLayout>


                <!-- Nombre y descripcion -->
                <LinearLayout
                    android:id="@+id/container_nombre"
                    android:layout_width="fill_parent"
                    android:layout_height="?android:attr/listPreferredItemHeight"
                    android:orientation="vertical"
                    android:layout_marginTop="15dp"
                    android:layout_toRightOf="@+id/iconMap" >

                    <TextView
                        android:id="@+id/titulo_in_map"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:text="Example application"
                        android:textSize="20sp" />

                    <TextView
                        android:id="@+id/descripcion_in_map"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:text="Este lugar no tiene descripción asociada."
                        android:singleLine="true"
                        android:layout_marginRight="15dp"
                        android:textSize="14sp" />
                </LinearLayout> 

                <!-- Estrellas, categoria y distancia -->
                <ImageView
                    android:id="@+id/rating_map"
                    android:layout_width="54dp"
                    android:layout_height="15dp"
                    android:layout_marginRight="6dip"
                    android:layout_marginLeft="15dp"
                    android:layout_alignParentBottom="true"
                    android:src="@drawable/stars_25"
                    android:layout_below="@+id/iconMap" />

                <TextView
                    android:id="@+id/categoria_map"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="14sp"
                    android:textStyle="bold"
                    android:layout_alignParentBottom="true"
                    android:layout_toRightOf="@id/iconMap"
                    android:layout_marginBottom="3dp" />

                <TextView
                    android:id="@+id/distance_map"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="200m"
                    android:textSize="14sp"
                    android:layout_marginRight="15dp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_marginBottom="3dp"  />

                <ImageView
                    android:id="@+id/place_icon_map"
                    android:layout_width="10dp"
                    android:layout_height="10dp"
                    android:layout_marginRight="0dp"
                    android:src="@drawable/marker_distance"
                    android:layout_alignParentBottom="true"
                    android:layout_toLeftOf="@id/distance_map"
                    android:layout_below="@+id/container_nombre"
                    android:paddingBottom="4dp"
                    android:paddingRight="2dp"  />
            </RelativeLayout>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

我的DondeFragment.java:

public class DondeFragment extends ListFragment implements OnMapClickListener,  OnCameraChangeListener, OnClickListener {




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_donde, container, false);

        if (container != null) {
            container.removeAllViews();
        }


        // Edito el mapa
        addMap();


        return rootView;
    }


    public void goToAttract(View v) {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
    }

    public void addMap() {
        // obtengo el mapa       
        global.mapa = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        // Move the camera instantly to my position with a zoom of 15.
        LatLng miPosicion = new LatLng(main.obtLatitud, main.obtLongitud);
        global.mapa.moveCamera(CameraUpdateFactory.newLatLngZoom(miPosicion , 17) );

        // Zoom in, animating the camera.
        global.mapa.animateCamera(CameraUpdateFactory.zoomTo(17), 2000, null);        
        global.mapa.setOnCameraChangeListener(this);

        global.mapa.setOnMapClickListener(this);
    }


    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onMapClick(LatLng arg0) {
        hideSlideUp();
        Log.v("EY", "You clicked the map ");
    }


    public void hideSlideUp() { 
        RelativeLayout slideLayout;
        slideLayout = (RelativeLayout) rootView.findViewById(R.id.sliding_up);
        if (slideLayout.getVisibility() != View.INVISIBLE) {
            slideLayout.setVisibility(View.INVISIBLE);
        }
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();

        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

        if (f != null) {
            getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();
        }
    }


    @Override
    public void onCameraChange(CameraPosition arg0) {
        // TODO Auto-generated method stub
        Log.v("ONCAMERACHANGE","CHANGE: ha cambiado la camara");

        LatLng centerMap = global.mapa.getCameraPosition().target;
        Log.v("Datos Mapa","CHANGE: Centro Mapa: " + centerMap);
        VisibleRegion widthPoint = global.mapa.getProjection().getVisibleRegion();
        Log.v("Datos Mapa","CHANGE: Visible: " + widthPoint);

        LatLng topRight = widthPoint.latLngBounds.northeast;
        LatLng bottonLeft = widthPoint.latLngBounds.southwest;

        double lat1 = topRight.latitude;
        double lon1 = topRight.longitude;
        double lat2 = bottonLeft.latitude;
        double lon2 = bottonLeft.longitude;

        double R = 6378.137; // km
        double dLat = Math.toRadians(lat2-lat1);
        double dLon = Math.toRadians(lon2-lon1);

        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                Math.sin(dLon/2) * Math.sin(dLon/2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double d = R * c;

        Log.v("Datos Mapa","CHANGE: Distancia total: " + d);
        ((MainActivity) getActivity()).addItemsToMap(d, centerMap);
    }




}

更新:我在goToAttract方法中添加了一个带有'Log.v'的行,它没有显示任何内容。我认为问题在于布局。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

将此方法替换为goToAttract -

     public void goToAttract(View v) {
     int view_id = view.getId();

    switch (view_id) {

    Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        break;

}

答案 1 :(得分:0)

那么,为什么不将此添加到onCreateView

LinearLayout layout = (LinearLayout) rootview.findViewById(R.id.container_slide);
layout.setOnClickListner(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goToAttract();
                }
            });

您需要添加其他重要内容,但这应该可行。

在第一行,如果这种情况不起作用,可以使用rootview.findViewById切换getActivity().findViewById,但我不确定您的其余活动和项目是如何设置的。