【问题标题】:Intent to another activity from fragment and LinearLayout从片段和线性布局到另一个活动的意图
【发布时间】:2014-01-30 11:06:12
【问题描述】:

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

这是应用结构:

我的 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”的行,但它什么也没显示。我认为问题出在布局上。

感谢您的帮助。

【问题讨论】:

  • 现在你的问题是什么?
  • 你的 goToAttract 是否被调用?
  • 试试getActivity().startActivity(intent);
  • 对不起,我没有指定错误。我的问题是我的 gotToAtract 没有被调用。我试过 getActivity().startActivity(intent);它仍然无法正常工作。
  • 让你检查你是否在你的片段 onViewCreated 方法中膨胀了正确的 .xml 文件?

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


【解决方案1】:

用这个替换你的方法goToAttract-

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

    switch (view_id) {

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

}

【讨论】:

  • 感谢您的帮助。问题是我想在单击线性布局而不是信息窗口标记时启动一个活动。
  • 它不工作。我在 goToAttract 方法中添加了一条带有“Log.v”的行,但它什么也没显示。我认为问题出在布局上。再次感谢您的帮助。
【解决方案2】:

那么,你为什么不把这个添加到你的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,但我不确定您的活动和项目的其余部分是如何设置的。

【讨论】:

    猜你喜欢
    • 2012-08-25
    • 1970-01-01
    • 2017-01-11
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    相关资源
    最近更新 更多