Android在抽屉布局中添加片段

3

我在尝试使用导航抽屉和碎片时遇到了一些困难。

main.xml

    <LinearLayout 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=".GoogleMap" >

    <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="#666"
        android:dividerHeight="1dp"
        android:background="#333"
        android:paddingLeft="15sp"
        android:paddingRight="15sp"
        />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

我面临的问题是导航抽屉只在我将设备从纵向旋转到横向或者反过来时显示内容,但只会持续很短时间。如果我不倾斜设备,则导航抽屉将保持为空。该片段基本上是所谓的div用于展示地图。然后当我点击导航抽屉时,它会位于地图片段的顶部。我想知道我的布局哪个部分出了问题,导致这个问题发生。感谢你的帮助。
<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">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

<!-- 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"/>

</android.support.v4.widget.DrawerLayout>

GoogleMap.java

public class GoogleMap extends FragmentActivity {

    // Google Map
    private com.google.android.gms.maps.GoogleMap map;

    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private ActionBarDrawerToggle actionBarDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

        // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);
        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));

        // App Icon 
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                drawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                );

        // Set actionBarDrawerToggle as the DrawerListener
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true); 

        // just styling option add shadow the right edge of the drawer
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        drawerListView.setOnItemClickListener(new DrawerItemClickListener());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

         // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
        // then it has handled the app icon touch event
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
         actionBarDrawerToggle.syncState();
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            Toast.makeText(GoogleMap.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();
            //Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr=1.3564 ,103.8311&daddr=1.3426 ,103.9254"));
            //startActivity(i);
            drawerLayout.closeDrawer(drawerListView);

        }
    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (map == null) {
            map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(1.32814,103.80679) , 11.0f) );

            // check if map is created successfully or not
            if (map == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
}
3个回答

1

您的布局有一个问题(我不知道其他的!),就是在LinearLayout内使用了DrawerLayoutDrawerLayout应该是您的布局文件中的根视图


所以基本上我应该将drawerLayout设置为根视图。 然后在linearLayout内,我应该放置fragment和listView? - user2424370
@20Cents:是的,你可以这样做,但也可以在DrawerLayout中使用FrameLayout,并动态地创建你的Fragment。 - Arash GM
我已经修改了。请检查我的编辑部分,但问题仍然存在。 - user2424370

0

你的drawerlayout里直接有3个视图。抽屉里只能有2个视图。我建议将fragment和framelayout包装在linearlayout或relativelayout中。或者将fragment嵌套在framelayout中。


所以基本上我应该将drawerLayout设置为根视图。然后在linearLayout内,我应该放置fragment和listView? - user2424370

-1
你应该这样编码:
<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">            
 <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"/>        
</android.support.v4.widget.DrawerLayout>    

编辑: 在你的Activity中设置这个FrameContent中的片段。

在你的onCreate()中添加这一行

如果(savedInstanceState == null) {
initilizeMap();
}


请查看默认NavigationDrawer的示例代码链接 - Ved
在我的Activity中,将片段设置为什么?您能否根据我的代码提供一些解决方案?我会更新问题。 - user2424370
在你的Activity中创建一个FragmentActivity类,在FragmentActivityonCreateView()方法中,将你的Fragment填充到FrameLayout中。 - Ved
我是这样做的:LinearLayout linearLayout; public View onCreateView(LayoutInflater inflater, ViewGroup container,                          Bundle savedInstanceState) {    View rootView = inflater.inflate(R.layout.fragment1, container, false);    linearLayout =(LinearLayout)rootView.findViewById(R.id.linearlayout);     return rootView; },但它不起作用。你能提供一个解决方案吗?因为我真的很迷茫。 - user2424370
很遗憾,它没有解决那个问题。内容仍然只有在我旋转屏幕后才能显示。 - user2424370

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接