我可以在自定义 InfoWindowAdapter 中使用 ViewPager 吗?
Can I use ViewPager inside custom InfoWindowAdapter?
我在 Google Maps API2 中使用 InfoWindowAdapter
。我的 XML 布局有一些简单的 TextView
和 ImageView
视图,但我还需要一个 ViewPager 来允许用户滑动一系列与地图上的标记关联的自定义布局(图像和一些文本)。我的信息窗口布局是:
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 的静态视图或由适配器设置的 TextView
和 ImageView
视图工作正常,一般布局也是如此。但是,我的 ViewPager(在此测试用例中应该在 TextViews
中有 10 个可滑动的国家/地区)是空白的。所有相关代码都在一个片段内处理,Android 监视器中没有错误。
知道我做错了什么吗?
I also need a ViewPager to allow the users to swipe a series of custom layouts associated with the marker on the map
那是不可能的。
Any ideas what I've done wrong?
View
你 return 从你的 InfoWindowAdapter
立即转换为 Bitmap
。 Bitmap
就是最终呈现在屏幕上的内容。因此,您不能拥有交互式小部件,例如 ViewPager
。它们不仅不会交互,而且任何异步呈现的内容(例如,ViewPager
中的页面)可能无法在创建 Bitmap
之前及时呈现。
或者,引用 the documentation:
Note: The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (for example, after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.
我在 Google Maps API2 中使用 InfoWindowAdapter
。我的 XML 布局有一些简单的 TextView
和 ImageView
视图,但我还需要一个 ViewPager 来允许用户滑动一系列与地图上的标记关联的自定义布局(图像和一些文本)。我的信息窗口布局是:
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 的静态视图或由适配器设置的 TextView
和 ImageView
视图工作正常,一般布局也是如此。但是,我的 ViewPager(在此测试用例中应该在 TextViews
中有 10 个可滑动的国家/地区)是空白的。所有相关代码都在一个片段内处理,Android 监视器中没有错误。
知道我做错了什么吗?
I also need a ViewPager to allow the users to swipe a series of custom layouts associated with the marker on the map
那是不可能的。
Any ideas what I've done wrong?
View
你 return 从你的 InfoWindowAdapter
立即转换为 Bitmap
。 Bitmap
就是最终呈现在屏幕上的内容。因此,您不能拥有交互式小部件,例如 ViewPager
。它们不仅不会交互,而且任何异步呈现的内容(例如,ViewPager
中的页面)可能无法在创建 Bitmap
之前及时呈现。
或者,引用 the documentation:
Note: The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (for example, after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.