android - 在同一 fragment 中合并 Map 和 ListView

标签 android google-maps listview android-fragments

我希望我的 Fragment 在其下方包含可交互的 map 和 ListView。但是,我在尝试合并这两个元素时遇到困难,因为 Map 似乎主导了 fragment (尽管我的 XML 代码很可能是错误的)。

另外,我偶尔会遇到这个错误:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f04003b, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment.

我的 XML 是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

<ListView
    android:id="@+id/map_frag_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

我的 fragment 代码是:

public class MapFrag extends SherlockFragment {
protected ListView assocListView;
protected View mapFragView;
protected String[] assocArrayItems; 
protected ArrayAdapter<String> mapAdapter;
protected List assocList;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    if (mapFragView != null) {
        ViewGroup parent = (ViewGroup) mapFragView.getParent();
        if (parent != null)
            parent.removeView(mapFragView);
    }
    try {
        mapFragView = inflater.inflate(R.layout.maptab, null, false);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }


    //Creating MapFragment from SharedPreferences recently stored information 
    SharedPreferences tmpManager = MainActivity.getInstance().prefs;

    System.out.println("now within the Map Fragment...");
    String recLatitude = tmpManager.getString("recentLatitude", "default");
    String recLongitude = tmpManager.getString("recentLongitude", "default");
    String recWordAssoc = tmpManager.getString("wordAssociations", "default");

    //Most Recent Items List
    String[] theList = null; 

    //TODO: ERROR HERE !!!!!!!!ClassCastException NoSaveStateFrameLayout 
    assocListView = (ListView) mapFragView.findViewById(R.id.map_frag_view);

    mapAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,  theList);

    assocListView.setAdapter(mapAdapter);
    ((BaseAdapter) assocListView.getAdapter()).notifyDataSetChanged();

    if(mapFragView != null) { return mapFragView;}

    ((ViewGroup) assocListView.getParent()).removeView(assocListView);

    container.addView(assocListView);
    container.addView(mapFragView);

    return container;
}
}

最佳答案

尝试

android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"

在两个元素上。

重复 ID 问题与使用嵌套 fragment 的不正确方式有关。

发件人:http://developer.android.com/about/versions/android-4.2.html#NestedFragments

Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically.

在这里查看我的回答:MapFragment in Fragment, alternatives?

关于android - 在同一 fragment 中合并 Map 和 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17176513/

相关文章:

android - 更新从广播接收器收到的短信的 Activity 用户界面

android - 在棉花糖中获取蓝牙本地mac地址

android - 如何在 Android 中实现 Google Places API?

安卓 : Centering Items in a ListVIew

java - 安卓打开失败: EACCES (Permission denied) when try to read a file in data/data

Android 更改要安装在主屏幕上的应用程序的名称

javascript - 谷歌地图 API V3 : How to get multiple path distances using direction from a point A to point B

google-maps - 在 google maps API v3 中切换多个 KML/KML 图层

android - ListView 行中的 TextView 在 Android 滚动时显示重复值?

VB.NET ListView 文本更改搜索