如何添加一个滑动抽屉多数民众赞成程序创建一个图形页面/查看 [英] How to add a sliding drawer a MapView/View that's created programmatically

查看:86
本文介绍了如何添加一个滑动抽屉多数民众赞成程序创建一个图形页面/查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着从不同的角度接近这个没有运气。也许问一个一般性的问题可以提供帮助。

I've tried to approach this from different angles without luck. Maybe asking a general question can help.

从技术上讲,我使用osmdroid的图形页面的实现,而不是谷歌的地图API,但我认为这个问题是一个更普遍的纲领性意见VS中的onCreate定义main_activity.xml意见。

Technically I'm using osmdroid's MapView implementation, not Google's Maps API, but I think this question is a more general programmatic Views vs main_activity.xml defined views in onCreate.

基本上在我的MainActivity,如果我的onCreate一个视图,如图形页面,然后将其设置为内容查看编程,我也必须编程方式添加任何其他意见我想在我的应用程序,以显示:

Basically in my MainActivity if I onCreate a View, like MapView, then set it as the ContentView programmatically, I have to also programmatically add in any other Views I want to display in my app:

public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.mapView = new MapView(this, 256);
    ...
    this.setContentView(this.mapView);
}

如果我尝试设置内容查看作为activity_main,的MapView无法的onCreate调整。也许我失去了一些东西:(请注意,我有一个手柄加载自定义离线瓷砖的设置方法,并在地图上的位置标记,等...)

If I attempt to set the ContentView as activity_main, the MapView can't be adjusted onCreate. Maybe I'm missing something: (note that I have methods that handle loading a custom offline tile set, and place markers on the map, etc...)

public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.mapView = (MapView)findViewById(R.id.mapview);
    ...
    this.intializeMapTiles();
    this.mapView.setBuiltInZoomControls(true);
    this.mapView.setMultiTouchControls(true);
    this.mapController.setCenter(new GeoPoint((int)(50.349622 * 1E6), (int)(-71.823700 *1E6)));
    ...
    this.mapView.invalidate();
}

下面是我在这种情况下activity_main.xml:

Here's my activity_main.xml in this case:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <org.osmdroid.views.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="false"/>

</RelativeLayout>

当试图从该内容查看(acitivty_main)的MapView,不关我的方法更改为它做任何影响。这是因为如果我没有获得多数民众赞成所呈现的具体图形页面。我试过无效我MapView类,但它并不重要。我得到一个默认的外观和行为图形页面。

When trying to get the MapView from the ContentView (acitivty_main), none of my method changes to it make any affect. It's as if I don't have access the exact MapView that's being rendered. I've tried invalidating my MapView but it doesn't matter. I get a default looking and behaving MapView.

究其原因我想解决这个问题,自然是我在寻找我的应用程序,包括一个以上的图形页面。我想包括SlidingDrawer,或按钮显示视图的一些方法,这只是地图上的标记显示,当你长时间preSS。 (请注意,我有被显示在地图上的标记长presses吐司弹出窗口,所以我好这方面的)

The reason I'm trying to solve this is naturally I'm looking for my app to include more than a single MapView. I would like to include a SlidingDrawer, or some method of displaying a View with buttons that is only displayed when you long press on a map marker. (note that I have Toast pop ups being displayed on map marker long presses, so I'm good in this regard)

我不得不编程,而不是添加这些其他视图(SlideingDrawer,等...)从main_activity.xml。即使是有一个catch-22,其中SlidingDrawer构造函数需要一个AttributeSet中的XML这是痛苦的打造自己。 (我想),那么你也不必担心布局为好。

I'd have to add these other Views (SlideingDrawer, etc...) programmatically and not from the main_activity.xml. Even that has a catch-22, where the SlidingDrawer constructor needs an AttributeSet from xml that's painful to build yourself. (I tried) Then you also have to worry about the layout as well.

任何人有什么建议?通用或以其他方式?谢谢!

Anyone have any suggestions? General or otherwise? Thanks!

推荐答案

这实际上可能对别人有用的一些原因。

This might actually be useful to others for a number of reasons.

如果你坚持不必使用只能被配置为通过其方案的构造需要的方式查看(例如,你可以只包括从activity_main.xml查看,但查看是不是你需要它是除非你自己构建它,就像使用OSMDroid的MapView的离线地图瓦),那么你就完蛋了扩展的视图和执行视图的包含的AttributeSet构造。 AttributeSet中基本上是从activity_main.xml XML该视图解析的结构。该构造函数将在活动自动被调用时,你this.setContentView(R.layout.activity_main)从的onCreate()。因此,任何自定义构造函数的东西需要去在构造你的扩展视图。

If you're stuck having to use a View that can only be configured the way to need via its programmatic constructor (e.g. you could just include the View from your activity_main.xml, but the View isn't what you need it to be unless you construct it yourself, like with offline tile maps using OSMDroid's MapView) then you're stuck extending that View and implementing that View's constructor that includes the AttributeSet. The AttributeSet is basically a structure parsed from the activity_main.xml xml for that view. That constructor will be called automatically in Activity when you this.setContentView(R.layout.activity_main) from onCreate(). So any custom constructor stuff needs to go in that constructor for your extended View.

例如,我不得不延长OSMDroid图形页面,然后实现我从超完全离线地图瓦片源。注意,你必须超()在扩展的构造函数中的第一行,因为对象的方法不可用,直到后继承的构造是完整的,所以任何超()方法调用必须是静态的方法。

For example, I had to extend the OSMDroid MapView, then implement my offline map tile source from the super entirely. NOTE you have to super() the 1st line in an extended constructor because object methods aren't available until after the inherited constructor is complete, so any super() method calls have to be to static methods.

public class FieldMapView extends MapView {
    public FieldMapView(Context context, AttributeSet attrs) throws Exception {
        super(
            context, 
            256, 
            new DefaultResourceProxyImpl(context), 
            FieldMapView.getOfflineMapProvider(context, MainActivity.mapTileArchiveFilename),
            null,
            attrs); 
        this.setUseDataConnection(false);
        this.setBuiltInZoomControls(false);
        this.setMultiTouchControls(true);
    }

然后在我的activity_main.xml我指向视图的扩展版本:(例如FieldMapView)

Then in my activity_main.xml I point to the extended version of the View: (e.g. FieldMapView)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.test.FieldMapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="false"/>

所以,现在我有我的扩展视图照顾任何编程风格的要求,怎么我才拿到SlidingDrawer的工作呢?我创建了一个SlidingDrawer与0dip高度查看手柄。然后我加入了一个LinearLayout中包含的按钮,任何你想要的,等...

So now I have my extended View taking care of any programmatic style requirements, how did I get a SlidingDrawer working with it? I created a SlidingDrawer with a 0dip height View as the handle. I then included a LinearLayout that contains buttons, whatever you want, etc...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.test.FieldMapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="false"/>

    <SlidingDrawer
        android:layout_width="wrap_content"
        android:id="@+id/slidingDrawerActions"
    android:content="@+id/action_content"
    android:padding="10dip"
    android:layout_height="75dip"
        android:handle="@+id/handle2"
        android:layout_alignBottom="@id/mapview"
    android:orientation="vertical"
    android:clickable="false">

        <LinearLayout
            android:layout_width="wrap_content"
            android:id="@+id/action_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:padding="10dip"
            android:background="#FF999999"
            android:layout_height="wrap_content"
            android:clickable="false">

            <View
                android:id="@id/handle2"
                android:layout_width="0dip"
                android:layout_height="0dip" />
            <ImageButton
                android:id="@+id/chatActionButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:src="@drawable/ic_action_edit"
                android:text="Chat">
            </ImageButton>
    </LinearLayout>
    </SlidingDrawer>
</RelativeLayout>

在我MainActivity的onCreate()我只是找到了SlidingDrawer查看,并相应地分配任何侦听器。对于打开时长pressing在MapView的标记(这是越来越OSMDroid具体的现在)的抽屉我自然有一个OnItemGestureListener打开抽屉:

In my MainActivity onCreate() I just find the SlidingDrawer View and assign any listeners accordingly. For opening the drawer when long pressing a Marker in the MapView (this is getting more OSMDroid specific now) I naturally have an OnItemGestureListener to open the drawer:

class NodeGestureListener implements OnItemGestureListener<NodeOverlayItem> {
    @Override
    public boolean onItemLongPress(int index, NodeOverlayItem node) {
        if(slidingDrawerActions.isOpened() || slidingDrawerActions.isMoving()) {
            return false;
        }
        slidingDrawerActions.animateOpen();
        return false;
    }

最棘手的部分是我想通过在图形页面点击关闭(不通过触摸一个关闭按钮,占用空间),所以我不得不来分配SlidingDrawer.OnDrawerOpenListener和OnDrawerCloseListener类。他们只是翻转一个布尔值,指示抽屉是打开或关闭。然后,我设置了一个简单onClickListener该关闭的抽屉,如果它是开放的基础上,isActionDrawerOpen由SlidingDrawer听众设置的MapView。

The tricky part is I wanted to close it via a click on the MapView (not by touching a close button that takes up space) so I had to assign SlidingDrawer.OnDrawerOpenListener and OnDrawerCloseListener classes. They simply flipped a boolean indicating if the drawer was open or closed. I then set a simple onClickListener for the MapView that closed the drawer if it was open based on the isActionDrawerOpen set by the SlidingDrawer listeners.

public void onCreate(final Bundle savedInstanceState) {
    ...
    this.mapView.setOnClickListener(new MapViewClickListener());
    ...
    this.slidingDrawerActions = (SlidingDrawer)findViewById(R.id.slidingDrawerActions);
    this.slidingDrawerActions.setOnDrawerOpenListener(new SlidingDrawerOpenListener());
    this.slidingDrawerActions.setOnDrawerCloseListener(new SlidingDrawerCloseListener());
    ...
}

...

private boolean isActionDrawerOpen = false;
class SlidingDrawerOpenListener implements SlidingDrawer.OnDrawerOpenListener {
    @Override
    public void onDrawerOpened() {
        isActionDrawerOpen = true;
    }
}
class SlidingDrawerCloseListener implements SlidingDrawer.OnDrawerCloseListener {
    @Override
    public void onDrawerClosed() {
        isActionDrawerOpen = false;
    }
}

private boolean skippedMapViewClickListener = false;
class MapViewClickListener implements OnClickListener {
    public void onClick(View view) {
        if(isActionDrawerOpen) {
            if(skippedMapViewClickListener) {
                slidingDrawerActions.animateClose();
                skippedMapViewClickListener = false;
            } else {
                skippedMapViewClickListener = true;
            }
        }
    }
}

请注意的skippedMapViewClickListener布尔。我的问题是,MapView的OnClickListener将是SlidingDrawer监听器后,立即打电话时长pressing的标记。这意味着长preSS将被视为一个图形页面点击,再加上长preSS本身就打开抽屉OnClickListener调用之前,所以OnClickListener总是看到抽屉开放,并会关闭它。我所做的是有效地跳过1日的onClick这样,所以抽屉将继续开放,直到你点击的MapView。似乎工作太棒了。

Note the skippedMapViewClickListener boolean. The problem I had was that the MapView OnClickListener would be called immediately after the SlidingDrawer listener when long pressing the Marker. Meaning the long press would be considered a MapView click, plus the long press itself would open the drawer before OnClickListener was called, so OnClickListener would always see the drawer as open, and would close it. What I did was effectively skip the 1st onClick this way, so the drawer would stay open until you clicked on the MapView. Seems to work great.

我希望这可以帮助别人。还有像4问题我解决了这个办法。

I hope this helps someone. There are like 4 problems I solved with this approach.

这篇关于如何添加一个滑动抽屉多数民众赞成程序创建一个图形页面/查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆