【问题标题】:Android Google maps fragment in RecyclerView.ViewHolderRecyclerView.ViewHolder 中的 Android 谷歌地图片段
【发布时间】:2015-01-16 11:02:47
【问题描述】:

如何从 RecyclerView.ViewHolder 访问 GoogleMap

我有:

public class MapRowHolder extends RecyclerView.ViewHolder
{
 public MapRowHolder(View view) {
        super(view);
 }
}

xml:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.v7.cardview="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android.support.v7.cardview:cardBackgroundColor="@color/blue"
android.support.v7.cardview:cardCornerRadius="@dimen/card_corner"
android.support.v7.cardview:cardElevation="@dimen/card_elevation">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="right|center_vertical"
    android:gravity="center_vertical|right"
    android:layout_width="fill_parent">


    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:tag="maps"

        android:id="@+id/map_view_fragment" tools:context=".Map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_centerInParent="true"/>

</LinearLayout>

适配器:

public class MapAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{


 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        RecyclerView.ViewHolder mh;


        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.map_row, viewGroup, false);
        mh = (RecyclerView.ViewHolder)new PodatkiZaMapRowHolder(v);

        return mh;
    }

 public void onBindViewHolder(RecyclerView.ViewHolder RowHolder, int polozaj) {

        MapRowHolder mapRowHolder = (MapRowHolder)RowHolder;


    }


}

它正确显示地图。它被缩小到整个地球。并定位到中心的 0,0。

如何获取地图片段(在 ViewHolder 中)?我想将相机移动到其他位置(我可以在获得 GoogleMap 对象后执行此操作)我应该如何以及在哪里获得 GoogleMap 对象?

如果我使用 MapView 代替地图片段它可以工作,但不会刷新地图。点击地图几次后地图刷新。 问候,艾尔

【问题讨论】:

  • 从您发布的 XML 编码中,您使用 cardview,因此您的 mapfragment 不在 recyclerview 内。另外,不刷新是什么意思?你可以在这里查看 recyclerview 的文档:developer.android.com/training/material/lists-cards.html
  • 问题是如何获取地图片段。如果我在 xml 中有 TextView(行的 xml),我可以在 RecyclerView.ViewHolder 作为 findViewById(R.id.my_text);然后我可以将值设置为 RowHolder.myText.setText("test");如何使用地图片段做到这一点?
  • 不刷新,我的意思是它处于某种暂停模式。它显示模糊的地图,然后当我点击时,它会将地图更新为更好的图像,再次点击它会更新一些标签,如果我点击更多次,它会一直更新。

标签: android google-maps android-fragments android-adapter android-viewholder


【解决方案1】:

我最终是将 SupportMapFragment 动态添加到项目布局内的 FrameLayout 中。您只需要创建具有唯一 ID 的额外 FrameLayout。

xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <TextView
        android:id="@+id/car_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Peugeot 407 2.0 HDI"
        android:textSize="20dp"
        android:gravity="center_horizontal"
        android:textColor="?attr/colorPrimary"
        android:textStyle="bold"
        android:layout_marginBottom="5dp"
        />

    <include layout="@layout/ride_item_summary" />

    <FrameLayout
        android:id="@+id/map_layout"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_marginBottom="10dp"
        />
</LinearLayout>

适配器:

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    super.onBindViewHolder(holder, position);
    switch (getItemViewType(position)) {
            case 1:
            if (first) {
                ... //check if polyline is not null, init other views

                    FrameLayout view = (FrameLayout) ((RideItemHolder) holder).mMapLayout;
                    FrameLayout frame = new FrameLayout(mContext);
                    frame.setId(1010101); //you have to set unique id

                    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 170, mContext.getResources().getDisplayMetrics());
                    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, height);
                    frame.setLayoutParams(layoutParams);

                    view.addView(frame);

                    GoogleMapOptions options = new GoogleMapOptions();
                    options.liteMode(true);
                    SupportMapFragment mapFrag = SupportMapFragment.newInstance(options);

                    //Create the the class that implements OnMapReadyCallback and set up your map
                    mapFrag.getMapAsync(new MapReadyCallback());

                    FragmentManager fm = fragment.getChildFragmentManager();
                    fm.beginTransaction().add(frame.getId(), mapFrag).commit();

                    first = false;
                }
            }
            break;
        case 2:
            ...

private class MapReadyCallback implements OnMapReadyCallback {
    @Override
    public void onMapReady(GoogleMap googleMap) {
        drawMap(googleMap);
    }
}

【讨论】:

  • 你的“片段”实例在哪里?
  • 每次滚动recyclerview,方法“view.addView(frame);”多次调用?
猜你喜欢
  • 1970-01-01
  • 2013-11-04
  • 1970-01-01
  • 2017-08-16
  • 1970-01-01
  • 1970-01-01
  • 2015-06-10
  • 1970-01-01
  • 2016-04-09
相关资源
最近更新 更多