【问题标题】:Android: Dynamically Populate a SlidingDrawer (or a small list) within the Main ActivityAndroid:在 Main Activity 中动态填充一个 SlidingDrawer(或一个小列表)
【发布时间】:2012-05-03 05:25:47
【问题描述】:

我正在开发一款 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);
                }
            });

我的布局文件:

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>

对不起,这太长了。任何帮助将不胜感激。

【问题讨论】:

    标签: android android-layout android-activity slidingdrawer


    【解决方案1】:

    您可以使用弹出窗口。

    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();
    

    【讨论】:

    • 感谢您的超快速响应。据我所知,PopupWindow 的问题似乎是,一旦 PopupWindow 被解除,它就无法被召回。我需要用户能够在任何给定时间查阅列表和地图。不过是个好主意。感谢您的努力。
    • 单击视图时调用它我曾经在按钮单击时调用它
    • 我会调查的。谢谢。在我换个方向之前,还有其他人对我的 SlidingDrawer 有什么想法吗?
    【解决方案2】:

    我选择的解决方案在编程上有点不理想,但布局效果很好。因为我知道我不会有超过 3 个结果,所以我只是将布局更改为在我的搜索栏下包含(最多)3 个小 TextView,并且(遗憾地)完全摆脱了 SlidingDrawer。当结果返回时,我会根据需要填充尽可能多的 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++;
    

    ...(类似的块处理 i = 1 和 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 路线来满足我的最后期限。也许当我有更多空闲时间时,我会看看我是否能解决这个问题......

    【讨论】:

    • 我想补充一点,如果有人对我最初的问题有明确的答案,我仍然想听听。 SlidingDrawer 可以在主活动中动态填充吗?
    猜你喜欢
    • 2012-01-30
    • 2015-12-10
    • 2018-11-20
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多