Android 谷歌地图V2+;抽屉布局
我曾尝试将抽屉布局(来自android教程)添加到“hello world”谷歌地图应用程序中。问题是您无法单击滑动菜单上的项目。我认为xml中的片段部分存在问题。我试图找到一个解决办法,但没有一个奏效。有没有办法以不同的方式添加它以使其工作 以下是活动代码:Android 谷歌地图V2+;抽屉布局,android,android-layout,google-maps-android-api-2,Android,Android Layout,Google Maps Android Api 2,我曾尝试将抽屉布局(来自android教程)添加到“hello world”谷歌地图应用程序中。问题是您无法单击滑动菜单上的项目。我认为xml中的片段部分存在问题。我试图找到一个解决办法,但没有一个奏效。有没有办法以不同的方式添加它以使其工作 以下是活动代码: public class MainActivity extends FragmentActivity { private GoogleMap mMap; private SupportMapFragment fragment; priva
public class MainActivity extends FragmentActivity {
private GoogleMap mMap;
private SupportMapFragment fragment;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.menu_item);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.draw_item, mPlanetTitles));
// set up the drawer's list view with items and click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap != null) {
return;
}
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap == null) {
return;
}
// Initialize map options. For example:
// mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.setMyLocationEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
MarkerOptions marker = new MarkerOptions().position(
new LatLng(point.latitude, point.longitude)).title("New Marker");
mMap.addMarker(marker);
//System.out.println(point.latitude+"---"+ point.longitude);
}
});
//mMap.setOnMarkerClickListener((OnMarkerClickListener) this);
}
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
*/
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
public类MainActivity扩展了FragmentActivity{
私有谷歌地图;
私有支持图片段;
私人抽屉布局mDrawerLayout;
私有列表视图mDrawerList;
私有操作bardrawertoggle mDrawerToggle;
私有字符序列mDrawerTitle;
私有字符序列mTitle;
私有字符串[]mplanettiles;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口.功能\u操作\u栏);
setContentView(R.layout.activity_main);
setupmapifneed();
mTitle=mDrawerTitle=getTitle();
mPlanetTitles=getResources().getStringArray(R.array.menu项);
mDrawerLayout=(抽屉布局)findViewById(R.id.抽屉布局);
mDrawerList=(ListView)findViewById(R.id.left\u抽屉);
//设置抽屉打开时覆盖主要内容的自定义阴影
mDrawerLayout.setDrawerShadow(R.drawable.drawer\u shadow,GravityCompat.START);
mDrawerList.setAdapter(新阵列适配器,
R.layout.draw_项目,mPlanetTitles);
//使用项目设置抽屉的列表视图,然后单击listener
setOnItemClickListener(新的DrawerItemClickListener());
//启用ActionBar应用程序图标作为切换导航抽屉的操作
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
//ActionBarDrawerToggle将适当的交互连接在一起
//在滑动抽屉和操作栏应用程序图标之间
mDrawerToggle=新操作bardrawertoggle(
此,/*主机活动*/
mDrawerLayout,/*抽屉布局对象*/
R.drawable.ic_抽屉,/*导航抽屉图像替换“Up”插入符号*/
R.string.drawer\u open,/*“open drawer”描述,用于辅助功能*/
R.string.drawer\u close/*“close drawer”可访问性说明*/
) {
/**当抽屉处于完全关闭状态时调用*/
公共无效onDrawerClosed(视图){
super.onDrawerClosed(视图);
}
/**当抽屉处于完全打开状态时调用*/
打开图纸上的公共空白(视图抽屉视图){
super.onDrawerOpened(抽屉视图);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
如果(savedInstanceState==null){
选择项目(0);
}
}
@凌驾
受保护的void onResume(){
super.onResume();
setupmapifneed();
}
私有void setUpMapIfNeeded(){
如果(mMap!=null){
返回;
}
mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
如果(mMap==null){
返回;
}
//初始化映射选项。例如:
//mMap.setMapType(GoogleMap.MAP\u TYPE\u HYBRID);
mMap.setMyLocationEnabled(真);
mMap.setOnMapClickListener(新的GoogleMap.OnMapClickListener(){
@凌驾
公共空区(停车点){
MarkerOptions marker=新MarkerOptions().位置(
新板条(点纬度、点经度))。标题(“新标记”);
mMap.addMarker(marker);
//System.out.println(点纬度+“--”+点经度);
}
});
//setOnMarkerClickListener((OnMarkerClickListener)this);
}
/*
@凌驾
公共布尔onCreateOptions菜单(菜单){
MenuInflater充气机=getMenuInflater();
充气机。充气(右菜单。主菜单,菜单);
返回super.onCreateOptions菜单(菜单);
}
*/
/*调用InvalidateOptions菜单()时调用*/
@凌驾
公共布尔值OnPrepareOptions菜单(菜单){
//如果导航抽屉打开,则隐藏与内容视图相关的操作项
boolean-DrawerLayout=mDrawerLayout.isDrawerOpen(mDrawerList);
返回super.onPrepareOptions菜单(菜单);
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//如果返回,则将事件传递给ActionBarDrawerToggle
//则它已处理应用程序图标触摸事件
如果(MDRAWERTOGLE.onOptionsItemSelected(项目)){
返回true;
}
//处理其他操作栏项。。。
返回super.onOptionsItemSelected(项目);
}
/*在导航抽屉中单击ListView的listner*/
私有类DrawerItemClickListener实现ListView.OnItemClickListener{
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
选择项目(位置);
}
}
私有void selectItem(内部位置){
//通过替换片段更新主要内容
//更新所选项目和标题,然后关闭抽屉
mDrawerList.setItemChecked(位置,true);
mDrawerLayout.closeDrawer(mDrawerList);
}
@凌驾
后期创建时受保护的空(捆绑包savedInstanceState){
super.onPostCreate(savedInstanceState);
//在onRestoreInstanceState发生后同步切换状态。
mDrawerToggle.syncState();
}
@凌驾
公共无效OnConfiguration已更改(配置newConfig){
super.onConfigurationChanged(newConfig);
//将任何配置更改传递给抽屉toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
以及xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:mapType="normal"/>
</android.support.v4.widget.DrawerLayout>
绘制项目布局:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
请参阅下面的解决方案。尝试添加
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
到draw_item.xml
我也遇到了同样的问题,必须添加这些行,因为textview获得了焦点,而不是listview行。问题在于main_activity.xml中项目的顺序。以下是使其工作的代码:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- The navigation drawer -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:mapType="normal"/>
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
这是f
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_menu"
app:menu="@menu/activity_menu_drawer" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="br.com.turbi.turbi.view.MapsActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
map:layout_anchorGravity="bottom"
tools:context="br.com.turbi.turbi.view.MapsActivity"
tools:layout="@layout/activity">
<include
layout="@layout/app_bar_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />