我正在开发一个 Android 地图应用程序,该应用程序允许用户在 EditText 框中键入搜索参数,然后使用 MapView 上的图钉绘制与查询匹配的最近目的地。
单击图钉将提供有关位置的信息,但我想预先向用户提供更多信息。我认为 SlidingDrawer 可以填充并打开以向用户显示目的地的名称/距离。
在这方面花了很多时间,读了很多书,但进展甚微,我现在向社区寻求帮助。
我遇到的问题是我无法弄清楚如何在主要活动中填充 SlidingDrawer 的内容。我能够启动一项新活动,但这会使我的 MapView 离开屏幕。我需要能够看到地图以及 SlidingDrawer。如果没有将其设置为 Activity,我无法弄清楚如何填充 SlidingDrawer。
(而且我还应该说,它不一定需要是包含目的地列表的 SlidingDrawer。我可以修改我的布局以制作一个小的(最多 3 个项目)列表,如果那样的话,可以拿走一些地图的房地产更容易,但我也不知道该怎么做。)
这是调用我的 SlidingDrawer 的代码:
//call SlidingDrawer
Intent drawerIntent = new Intent(this, SlidingDrawerFinal.class);
drawerIntent.putExtra("lat", lat);
drawerIntent.putExtra("lon", lon);
int numberOfTargetsForList = numberOfLoops;
drawerIntent.putExtra("numberOfTargetsForList", numberOfTargetsForList);
for(int i = 0; i < target.length; i++){
drawerIntent.putExtra("target" + Integer.toString(i), target[i]);
}
this.startActivity(drawerIntent);
这是填充抽屉内列表的 SlidingDrawer 类(扩展 Activity)中的代码:
View inflatedDrawerLayout = getLayoutInflater().inflate(R.layout.drawer_main, null);
int width = getWindow().getAttributes().width;
int height = 300;
LayoutParams params = new LayoutParams(width, height);
getWindow().addContentView(inflatedDrawerLayout, params);
// add some data
ArrayList<MyData> myDataList = new ArrayList<MyData>();
myData = new MyData[numberOfTargets];
for(int i = 0; i < numberOfTargets; i++){
String lineTwo = "";
if(target[i].getRating().equals("Not Yet Rated")){
lineTwo = " " + target[i].getRating() + " " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
else{
lineTwo = " rating: " + target[i].getRating() + "/5 stars " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
String lineOne = (Integer.toString(i + 1) + ": " + target[i].getName() + ", " + target[i].getVicinity()) + "\n" + lineTwo;
myData[i] = new MyData(lineOne, i);
myDataList.add(myData[i]);
}
mListView = (ListView) findViewById(R.id.listview_);
mListView.setAdapter(new MyListAdapter(this, R.layout.drawer_row, myDataList));
drawer = (SlidingDrawer) findViewById(R.id.drawer);
handleButton = (Button) findViewById(R.id.handle);
drawer.animateOpen();
drawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
public void onDrawerOpened() {
handleButton.setBackgroundResource(R.drawable.handle_down_white);
}
});
我的布局文件:
主.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip">
<EditText
android:id="@+id/geocode_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/placeName"
android:inputType="text"
android:lines="1" />
<Button
android:id="@+id/geocode_button"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/goLabel" />
</LinearLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<view android:id="@+id/mv"
class="com.google.android.maps.MapView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:clickable="true"
android:apiKey="my_API_Key"/>
</LinearLayout>
<include layout="@layout/drawer_main"/>
</FrameLayout>
</LinearLayout>
最后,drawer_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer"
android:handle="@+id/handle"
android:content="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@null"
android:layout_gravity="bottom">
<LinearLayout
android:id="@id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold|italic"
android:background="@android:color/white"
android:textColor="@android:color/black"
android:text="@string/top_search_results" >
</TextView>
<View
android:background="@android:drawable/divider_horizontal_bright"
android:layout_width="fill_parent"
android:layout_height="2dp" >
</View>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listview_"
android:background="@android:color/black"
android:textColor="@android:color/white"
android:divider="@android:color/transparent"
android:dividerHeight="2dp" >
</ListView>
</LinearLayout>
<Button
android:id="@id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/handle_down_white">
</Button>
</SlidingDrawer>
</FrameLayout>
对不起,这太长了。任何帮助将不胜感激。