我正在开发一个Android映射应用程序,该应用程序允许用户在EditText框中键入搜索参数,然后匹配查询的最近目标是在MapView上用引脚绘制。Android:在主活动中动态填充SlidingDrawer(或小列表)
点击一个引脚将提供有关位置的信息,但我想提前向用户提供更多信息。我认为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);
}
});
个
我的布局文件:
的main.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>
对不起,这是如此漫长。任何援助将不胜感激。
我想补充一点,如果任何人对我原来的问题有一个明确的答案,我仍然希望听到它。 SlidingDrawer可以在主要活动内动态填充吗? – pjd