繁体   English   中英

我可以在自定义InfoWindowAdapter中使用ViewPager吗?

[英]Can I use ViewPager inside custom InfoWindowAdapter?

我正在Google Maps API2中使用InfoWindowAdapter 我的XML布局具有一些简单的TextViewImageView视图,但是我还需要一个ViewPager,以允许用户滑动与地图上的标记关联的一系列自定义布局(图像和一些文本)。 我的InfoWindow布局是:

spot_window_complex.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/transparent"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/lin1"
    android:layout_width="275.0dip"
    android:layout_height="140.0dip"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="275.0dip"
        android:layout_height="85.0dip"
        android:background="@drawable/round" />

    <LinearLayout
        android:layout_width="275.0dip"
        android:layout_height="40.0dip"
        android:background="#ffeeeeee"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingBottom="10.0dip"
        android:paddingTop="10.0dip">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10.0dip"
            android:paddingRight="3.0dip"
            android:src="@drawable/clock" />
        <ImageButton
            android:id="@+id/imgThumbsUp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ffeeeeee"
            android:paddingRight="3.0dip"
            android:src="@drawable/thumbsup" />
        <TextView
            android:id="@+id/txtPve"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="50.0dip"
            android:textColor="#ff29c200"
            android:textStyle="bold" />
        <ImageButton
            android:id="@+id/imgThumbsDown"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ffeeeeee"
            android:paddingRight="3.0dip"
            android:src="@drawable/thumbsdown" />
        <TextView
            android:id="@+id/txtNve"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffc91c11"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>
</RelativeLayout>

我创建了一个简单的XML布局,以尝试使ViewPager在InfoWindowAdapter工作:

viewpager_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp" >

<TextView
    android:id="@+id/countrylabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Country: " />
<TextView
    android:id="@+id/country"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/countrylabel" />
</RelativeLayout>

我的ViewPagerAdapter看起来很标准:

class ViewPagerAdapter extends PagerAdapter {
    LayoutInflater inflater;
    String[] country = new String[]{"China", "India", "United States","Indonesia",
            "Brazil", "Pakistan", "Nigeria", "Bangladesh", "Russia", "Japan"};

    @Override
    public int getCount() {
        return country.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        inflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.viewpager_item, container,
                false);

        TextView txtcountry = (TextView) itemView.findViewById(R.id.country);
        txtcountry.setText(country[position]);
        ((ViewPager) container).addView(itemView);
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);
    }
}

而且我的InfoWindowAdapter似乎也可以:

public class SimpleInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
    public SimpleInfoWindowAdapter() {
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        View v;
        MySimpleMarker myMarker = mSimpleMarkersHashMap.get(marker);

        v = getActivity().getLayoutInflater().inflate(R.layout.spot_window_complex, null);
        TextView txtPve = (TextView) v.findViewById(R.id.txtPve);
        TextView txtNve = (TextView) v.findViewById(R.id.txtNve);
        txtPve.setText("0"); //test string only
        txtNve.setText("0"); //test string only

        ViewPager vPager = (ViewPager) v.findViewById(R.id.pager);
        vPager.setAdapter(new ViewPagerAdapter());

        return v;
    }
}

从XML静态或由适配器设置的TextViewImageView视图可以正常工作,就像常规布局一样。 但是,我的ViewPager(在此测试用例中应在TextViews具有10个可滑动的国家/地区)为空白。 所有相关代码都在一个片段内处理,Android Monitor中没有错误。 静态内容上方的空白ViewPager

有任何想法我做错了吗?

我还需要一个ViewPager,以允许用户滑动与地图上的标记相关联的一系列自定义布局

这是不可能的。

有任何想法我做错了吗?

InfoWindowAdapter返回的View将立即转换为Bitmap Bitmap最终在屏幕上呈现。 因此,您不能拥有交互式小部件,例如ViewPager 它们不仅不具有交互性,而且异步创建的任何内容(例如ViewPager中的ViewPager )都可能无法在创建Bitmap之前及时呈现。

或者,引用文档

注意:绘制的信息窗口不是实时视图。 该视图在返回时呈现为图像(使用View.draw(Canvas))。 这意味着对视图的任何后续更改都不会在地图上的信息窗口中反映出来。 要稍后更新信息窗口(例如,在加载图像之后),请调用showInfoWindow()。 此外,信息窗口将不考虑正常视图的任何交互性,例如触摸或手势事件。 但是,您可以按照以下部分中的说明在整个信息窗口上侦听一般的click事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM