2012-05-03 88 views
1

我正在开发一个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> 

对不起,这是如此漫长。任何援助将不胜感激。

回答

0

我选择的解决方案在编程上稍微不理想,但布局效果很好。由于我知道我的结果不会超过3个,因此我只是将我的布局更改为在我的搜索栏中包含(最多)3个小型TextView,并且(可惜)完全没有了SlidingDrawer。当结果返回时,我根据需要填充尽可能多的TextView,每个TextView都带有一个onClickListener。它不会很好地扩展,并且一切都是硬编码的。但它的工作原理。随着我对Android的更多了解,我可能会尝试改进此解决方案。

新的布局:

<?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> 

    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="13dip" 
      android:textStyle="bold|italic" 
      android:background="@android:color/black" 
      android:textColor="@android:color/white" 
      android:id="@+id/search_results_header" > 
    </TextView> 

    <View android:id="@+id/bar_over_one"  
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="#DADADA" /> 

    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="11dip" 
      android:background="@android:color/black" 
      android:textColor="@android:color/white" 
      android:id="@+id/line_one" > 
    </TextView> 

    <View android:id="@+id/bar_over_two"  
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="#DADADA" /> 

    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="11dip" 
      android:background="@android:color/black" 
      android:textColor="@android:color/white" 
      android:id="@+id/line_two" > 
    </TextView> 

    <View android:id="@+id/bar_over_three"  
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="#DADADA" /> 

    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="11dip" 
      android:background="@android:color/black" 
      android:textColor="@android:color/white" 
      android:id="@+id/line_three" > 
    </TextView> 

    <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="0fmGmyFrFDUrPGrwXR_scZZxVx6CkSdK-sys-Qw"/> 
    </LinearLayout> 
</LinearLayout> 

新代码:

private void populateList(int numberOfTargetsForList){ 
    TextView header = (TextView) findViewById(R.id.search_results_header); 
    header.setText("Top Search Results"); 

    int i = 0; 
    //first line in list 
    barOverOne.setVisibility(View.VISIBLE); 
    String ratingAndDistance = ""; 
    if(target[i].getRating().equals("Not Yet Rated")){ 
     ratingAndDistance = "  " + target[i].getRating() + "       " + 
       Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles"; 
    } 
    else{ 
     ratingAndDistance = "  rating: " + target[i].getRating() + "/5 stars     " + 
       Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles"; 
    } 
    String detailsForLineOne = (Integer.toString(i + 1) + ": " + target[i].getName() + ", " + target[i].getVicinity()) + "\n" + ratingAndDistance; 
    TextView lineOne = (TextView) findViewById(R.id.line_one); 
    lineOne.setText(detailsForLineOne); 
    lineOne.setOnClickListener(this); 

    i++; 

...(相似的块处理= 1的i例且i = 2)

最后,在我的onClick()方法:

case R.id.line_one:{ 
    String locForURL = "origin=" + Double.toString(lat/1000000.0) + 
      "," + Double.toString(lon/1000000.0) + "&destination=" + 
      Double.toString(target[0].getLat()/1000000.0) + "," + 
      Double.toString(target[0].getLon()/1000000.0); 
    Intent intent = new Intent(this, ParsingXMLDirectionsWithListView.class); 
     intent.putExtra("dirTitle", target[0].getName()+ " (" + 
      Double.toString((Math.round(target[0].getDistance()*100.0))/100.0) + " miles)"); 
     intent.putExtra("locForURL", locForURL); 
    this.startActivity(intent); 
    break; 
}//line_one 

...

(对于线两个和三个相似的块)不完美,但很好。从用户的角度来看,至少看起来不错,而且我永远不会通过继续沿着SlidingDrawer路线来满足我的期限。也许当我有更多的空闲时间,我会看看如果我能弄清楚这一点....

+0

我想补充一点,如果任何人对我原来的问题有一个明确的答案,我仍然希望听到它。 SlidingDrawer可以在主要活动内动态填充吗? – pjd

0

您可以使用弹出窗口。

View popupView = layoutInflater.inflate(R.layout.popup_window, null); 

    final PopupWindow popupWindow = new PopupWindow(popupView, 
      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

    ImageButton btnCancel = (ImageButton) popupView 
      .findViewById(R.id.btnCancel); 


popupWindow.showAtLocation(LayoutView, Gravity.BOTTOM 
      | Gravity.CENTER_HORIZONTAL, (LayoutView.getWidth() - popupView 
      .getWidth())/2, LayoutView.getHeight() 
      - popupWindow.getHeight() - 10); 
    popupWindow.setAnimationStyle(Animation.RELATIVE_TO_SELF); 
    popupWindow.setFocusable(true); 
    popupWindow.update(); 
+0

感谢您的超快速响应。从我所知道的情况来看,PopupWindow的问题看起来像是一旦PopupWindow被解雇,它就无法被召回。我需要用户能够在任何给定时间查阅列表和地图。不过,一个好主意。谢谢你的努力。 – pjd

+0

通过点击一个视图调用它我曾经在按钮上调用它点击 –

+0

我会研究这个。谢谢。在我朝不同的方向前,其他人对我的SlidingDrawer有任何想法? – pjd